From 416a9144ca0eaff995478cc94388a62b3f3c6ee5 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Mon, 5 Dec 2022 16:10:05 -0300 Subject: [PATCH] export new useful functions from internal helper methods (#821) * export new useful functions from internal helper methods * add tests for browser --- examples/wasm_browser/index.js | 43 +++++++++++++++++++++++----------- signer-npm/js/src/index.ts | 26 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/examples/wasm_browser/index.js b/examples/wasm_browser/index.js index 220f0fae..8fc1bde9 100644 --- a/examples/wasm_browser/index.js +++ b/examples/wasm_browser/index.js @@ -15,7 +15,7 @@ log('mnemonic') ///////////////////////////////// // Derive key -let key = wasm.keyDerive(mnemonic, 'm/44\'/461\'/0/0/0', '') +let key = wasm.keyDerive(mnemonic, "m/44'/461'/0/0/0", '') log('

[wasm.key_derive]

') log(`address ${key.address}`) @@ -29,7 +29,7 @@ log(`private base64 ${key.private_base64}`) ///////////////////////////////// // Recover key -let recovered_key = wasm.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7',"hex")) +let recovered_key = wasm.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7', 'hex')) log('

[wasm.key_recover]

') log(`address ${recovered_key.address}`) @@ -44,16 +44,16 @@ log(`private array ${recovered_key.private_raw}`) log('

[wasm.sign_transaction]

') const unsigned_tx = { - 'to': 't17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy', - 'from': key.address, - 'nonce': 1, - 'value': '100000', - 'gasprice': '2500', - 'gaslimit': 25000, - 'gaspremium': '2500', - 'gasfeecap': '2500', - 'method': 4, - 'params': '', + to: 't17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy', + from: key.address, + nonce: 1, + value: '100000', + gasprice: '2500', + gaslimit: 25000, + gaspremium: '2500', + gasfeecap: '2500', + method: 4, + params: '', } console.log('About to call wasm.transactionSignLotus():') @@ -76,10 +76,25 @@ mnemonic = js.generateMnemonic() log('

[js.mnemonic_generate]

' + mnemonic) log('mnemonic') +///////////////////////////////// +// Validate Address + +let isValid_1 = js.validateAddressAsString('t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy') +let isValid_2 = js.validateAddressAsString('t76uoq6tp427uzv7fztccsnn64iwotfrristwprzz') +log('

[js.validateAddressAsString]

') +log(`t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy ${isValid_1}`) +log(`t76uoq6tp427uzv7fztccsnn64iwotfrristwprzz ${isValid_2}`) + +isValid_1 = js.validateAddressAsBytes(Buffer.from('01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855', 'hex')) +isValid_2 = js.validateAddressAsBytes(Buffer.from('07fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62822', 'hex')) +log('

[js.validateAddressAsBytes]

') +log(`01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855 ${isValid_1}`) +log(`07fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62822 ${isValid_2}`) + ///////////////////////////////// // Derive key -key = js.keyDerive(mnemonic, 'm/44\'/461\'/0/0/0', '') +key = js.keyDerive(mnemonic, "m/44'/461'/0/0/0", '') log('

[js.key_derive]

') log(`address ${key.address}`) @@ -93,7 +108,7 @@ log(`private base64 ${key.private_base64}`) ///////////////////////////////// // Recover key -recovered_key = js.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7',"hex")) +recovered_key = js.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7', 'hex')) log('

[js.key_recover]

') log(`address ${recovered_key.address}`) diff --git a/signer-npm/js/src/index.ts b/signer-npm/js/src/index.ts index 0c32b89c..8dd7a597 100644 --- a/signer-npm/js/src/index.ts +++ b/signer-npm/js/src/index.ts @@ -17,6 +17,32 @@ export function generateMnemonic(): string { return bip39.generateMnemonic(256) } +export function validateAddressAsString(address: string): boolean { + try { + addressAsBytes(address) + return true + } catch (error) { + return false + } +} + +export function validateAddressAsBytes(bytes: Buffer): boolean { + try { + bytesToAddress(bytes, false) + return true + } catch (error) { + return false + } +} + +export function parseAddress(address: string): Buffer { + return addressAsBytes(address) +} + +export function encodeAddress(bytes: Buffer, isTestnet: boolean): string { + return bytesToAddress(bytes, isTestnet) +} + export function keyDeriveFromSeed(seed: string | Buffer, path: string): ExtendedKey { if (typeof seed === 'string') { seed = Buffer.from(seed, 'hex')