For your convenience, the JS SDK provides multiple functions, including getHash
, toDecimal
, toUtf8
, getKeyInfo
, and decryptAESWithPassword
. You can use these functions in the following two ways:
Use the global object named Chain or the initialized environment instance named chain.
Chain.utils.getHash('Tester001') // Calculate the account identity using the name of the account
Introduce the Java.util package individually.
const Util = require('@alipay/mychain/lib/ant3/util')
Util.getHash('Tester001')
getHash
Use the GetHash() function to return the hash for a string. This string is typically used as the account identity.
Request parameters
Parameter |
Required |
Type |
Description |
msg |
true |
string |
The target string used as the input of the hash function. |
not0x |
false |
bool |
The response is in the hexadecimal format. It indicates whether the string is prefixed with “0x”. A value of true indicates that the string is not prefixed with “0x”. A value of false indicates that the string is prefixed with “0x”. |
Response parameters
Response field |
Field type |
Description |
result |
string |
A hexadecimal string used to identify whether the string is prefixed with “0x”. |
Examples
const accountId = Chain.utils.getHash('Tester001')
toDecimal
Converts a hexadecimal string to a decimal string. The BigNumber
object can be passed in as the input parameter.
Request parameters
Parameter |
Required |
Type |
Description |
value |
true |
string/BigNumber |
The target parameter. |
Response parameters
Response field |
Field type |
Description |
result |
number |
A decimal number. |
Examples
const num = Chain.utils.toDecimal('0x64')
toUtf8
Converts a hexadecimal string to a UTF-8 string.
Request parameters
Name |
Required |
Type |
Description |
value |
true |
string |
A hexadecimal string. |
Response parameters
Response field |
Field type |
Description |
result |
string |
A UTF-8 string. |
Examples
const str = Chain.utils.toUtf8('0xe89a82e89a81e58cbae59d97e993be')
getKeyInfo
Obtains the account information.
Request parameters
Name |
Required |
Type |
Description |
value |
true |
string |
Reads the account file (user.pem ) or the hexadecimal private key string prefixed with “0x”. |
passphrase |
false |
string |
The password of the account file user.pem . If the value parameter is a hexadecimal private key string, you do not need to configure this password. |
Response parameters
Response field |
Field type |
Description |
result |
object |
The private key object. |
result.privateKey |
Buffer |
The private key buffer. |
result.publicKey |
Buffer |
The public key buffer. |
Examples
const result = Chain.utils.getKeyInfo('0x2768d0e80eb904560aa0a3ff4fc96b2f4d1e5d1fe293cc69c07808765ab20c29')
decryptAESWithPassword
The trusted execution environment (TEE) decryption method.
Request parameters
Name |
Required |
Type |
Description |
output |
true |
string |
The data to be decrypted. For example, the output returned by the contract. |
aesKey |
true |
string |
This parameter will be passed in as a password and used with the target encrypted transaction hash to generate a symmetric AES key. If the string type is used, this method will check whether the string is prefixed with ‘0x’. If it is prefixed with ‘0x’, it will be read as a hexadecimal string. Otherwise, it will be read as an encoded ASCII string. |
hash |
true |
string |
The transaction hash. |
Response parameters
Response field |
Field type |
Description |
result |
string |
The decrypted output. |
Examples
const result = Chain.utils.decryptAESWithPassword('0x83568fd7f92d39e42523d59305c383bb24fc7a404f37d0b95d622246fa198037f91ae16edb071dec3a96c919d714c7fe99cc6c6da025376e53aa946c', '0x1c4f2919963e8dc040cfddf7d27227de', '0xf91ae16edb071dec3a96c9196f8505f6d49f4ae79707c10ac73455a95e04e03d')
decryptTXWithAES
The blockchain based on TEE.
Request parameters
Name |
Required |
Type |
Description |
tx |
true |
string |
The transaction data to be decrypted. The data is in the hexadecimal format prefixed with ‘0x’. |
aesKey |
true |
string or buffer |
This parameter is passed in as a password and used with the target encrypted transaction hash to generate a symmetric AES key. If the string type is used, this method checks whether the string is prefixed with ‘0x’. If it is prefixed with ‘0x’, it is read as a hexadecimal string. Otherwise, it is read as an encoded ASCII string. |
hash |
true |
string |
The target transaction hash. |
Response parameters
Response field |
Field type |
Description |
result |
object |
The decrypted transaction object. |
Examples
const result = Chain.utils.decryptTXWithAES('0x00011660bbb61f5a66ab5cf78910831c41ed6b9eafb60fe3d14fa9a5a8fe4e800b30a3a8d8d6a4e8cbfc511e8f967509acf9d08c96c62da111b4886f0a3a0441dcc2887fd88243aa671a0f2fc08822d1c9d99db571b7d81207809e1f81629b38e9184221570bb1ef265d4f91d1967219fd51127712c60ae2990feaa2f4d021e71af5073de40a092de84609aead81882686595d4cf09672ef6aee28ed3894f6eb9d2b80000a4c02795d64785191bb7f076b9d2bb3552734fb0c2335f81424053f066a50242812aa92ecf82a212770d1adcb37717d01980bc7898258301d9b86344e5b338efa675e8ed7baaf6e03d464469eb24fb369444e29d08e573a287b02946e4c1dbd8e38a74a5c64f6c3e17bd8ac48626f4c240d96d5b7a1262f5034f838dd6811418cdf33eb6f1660664ea620318ec28f36b4860257a3d0b926788036e48a46aa84dbfe968e1def8a62170c286ceb5e4ef44856af6810678210ac7851f2e251aa28a4f0682d1880c27cdad697f53cc7a6d4acb1378e01b1aed9919a01f1b7c538441baaf4e1e90df9814f6da0c7989e9c56e218098f6700281bfc92f96f95c4f711c03174bbe7b0f0697ee70cbe772b53676b09d7f72547a81453021617c5e79e6484bd29c1304dcffece04cb1debfca235f513cf3f1a20326d8007038666f0f9259e5ec4af94b0cdebc1bd1f894e44eca006714cb2e2065979214f7223c6c5c9073350faa60dd6bdf3dae72c09358167adadb56cd7e0fd1d065a97d6a6c7c23132ab7357ff48e9ed913f95aff1442d0d1878', '0x1c4f2919963e8dc040cfddf7d27227de', '0xb9fc5d963d094e03072c5cad233efc80023a7a9bd68a4bca98e80c99214aa68e')
generateAESKey
For the blockchain based on TEE, the specified AES key is used with the specified transaction hash to generate a final AES key for the target transaction. Typically, it is used when sharing a single transaction.
Request parameters
Name |
Required |
Type |
Description |
aesKey |
true |
String or buffer |
This parameter is passed in as a password and used with the target encrypted transaction hash to generate a symmetric AES key. If the string type is used, this method checks whether the string is prefixed with ‘0x’. If it is prefixed with ‘0x’, it is read as a hexadecimal string. Otherwise, it is read as an encoded ASCII string. |
hash |
true |
string |
The transaction hash. |
Response parameters
Response field |
Field type |
Description |
result |
string |
The final AES key derived for the target transaction. |
Examples
// case-1 result(hex): b2bafab38c678a4519aa9822aa6c4e53
console.log('AES Key:', Chain.utils.generateAESKey('0x3b1fa6e5c8a3df729bdb6bf415150ddd', '0x6f3f709fcce4509ca4c2e009d9be668d9f0cc9c91247180f9b6d1011cb20291c').toString('hex'))
// case-2 result(hex): 0b985d88bb3b7c932028512bcd1eb10e
console.log('AES Key:', Chain.utils.generateAESKey('1234abc', '0x6f3f709fcce4509ca4c2e009d9be668d9f0cc9c91247180f9b6d1011cb20291c').toString('hex'))