Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
export new useful functions from internal helper methods (#821)
Browse files Browse the repository at this point in the history
* export new useful functions from internal helper methods
* add tests for browser
  • Loading branch information
emmanuelm41 authored Dec 5, 2022
1 parent 9972a8e commit 416a914
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
43 changes: 29 additions & 14 deletions examples/wasm_browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<h2>[wasm.key_derive]</h2>')
log(`<b>address </b> ${key.address}`)
Expand All @@ -29,7 +29,7 @@ log(`<b>private base64</b> ${key.private_base64}`)
/////////////////////////////////
// Recover key

let recovered_key = wasm.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7',"hex"))
let recovered_key = wasm.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7', 'hex'))

log('<h2>[wasm.key_recover]</h2>')
log(`<b>address </b> ${recovered_key.address}`)
Expand All @@ -44,16 +44,16 @@ log(`<b>private array</b> ${recovered_key.private_raw}`)
log('<h2>[wasm.sign_transaction]</h2>')

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():')
Expand All @@ -76,10 +76,25 @@ mnemonic = js.generateMnemonic()
log('<h2>[js.mnemonic_generate]</h2>' + mnemonic)
log('mnemonic')

/////////////////////////////////
// Validate Address

let isValid_1 = js.validateAddressAsString('t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy')
let isValid_2 = js.validateAddressAsString('t76uoq6tp427uzv7fztccsnn64iwotfrristwprzz')
log('<h2>[js.validateAddressAsString]</h2>')
log(`<b>t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy </b> ${isValid_1}`)
log(`<b>t76uoq6tp427uzv7fztccsnn64iwotfrristwprzz </b> ${isValid_2}`)

isValid_1 = js.validateAddressAsBytes(Buffer.from('01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855', 'hex'))
isValid_2 = js.validateAddressAsBytes(Buffer.from('07fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62822', 'hex'))
log('<h2>[js.validateAddressAsBytes]</h2>')
log(`<b>01fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855 </b> ${isValid_1}`)
log(`<b>07fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62822 </b> ${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('<h2>[js.key_derive]</h2>')
log(`<b>address </b> ${key.address}`)
Expand All @@ -93,7 +108,7 @@ log(`<b>private base64</b> ${key.private_base64}`)
/////////////////////////////////
// Recover key

recovered_key = js.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7',"hex"))
recovered_key = js.keyRecover(Buffer.from('6a1a68774457742a8bc69db5491df5ae7677687d49e1003a78e2d60959d5f7a7', 'hex'))

log('<h2>[js.key_recover]</h2>')
log(`<b>address </b> ${recovered_key.address}`)
Expand Down
26 changes: 26 additions & 0 deletions signer-npm/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 416a914

Please sign in to comment.