Last active
April 16, 2020 21:28
-
-
Save szymonlesisz/84050969133e3de99d6768f27046e38d to your computer and use it in GitHub Desktop.
TrezorConnect: node signTx using account info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TrezorConnect = require('trezor-connect').default; | |
TrezorConnect.manifest({ | |
email: '', | |
appUrl: '', | |
}); | |
TrezorConnect.on('UI_EVENT', event => { | |
console.log('UI_EVENT', event.type); | |
}); | |
TrezorConnect.on('DEVICE_EVENT', event => { | |
console.log('DEVICE_EVENT', event.type); | |
}); | |
const ACCOUNT = "m/49'/1'/0'"; | |
const COIN = 'test'; | |
const run = async () => { | |
// fetch account data | |
const account = await TrezorConnect.getAccountInfo({ | |
path: ACCOUNT, | |
coin: COIN, | |
details: 'tokenBalances', | |
}); | |
if (!account.success) { | |
console.error('Account info error', account.payload.error); | |
return; | |
} | |
console.log('Account info:', account.payload); | |
// use account data to precompose transaction, you can use multiple fee levels here. | |
// the rule is: 1 fee level = 1 transaction object in response | |
// it doesn't require device at this point | |
const precomposedTx = await TrezorConnect.composeTransaction({ | |
account: account.payload, | |
feeLevels: [{ feePerUnit: '1' }], | |
outputs: [{ | |
address: '2N4Q5FhU2497BryFfUgbqkAJE87aKHUhXMp', | |
amount: '2000', | |
}], | |
coin: COIN, | |
}); | |
if (!precomposedTx.success) { | |
console.error('Compose error', precomposedTx.payload.error); | |
return; | |
} | |
const transactionInfo = precomposedTx.payload[0]; // in this example only 1 fee level was used | |
if (transactionInfo.type !== 'final') { | |
// amount not set, not enough funds.... | |
console.error('Tx not final', transactionInfo); | |
return; | |
} | |
const signedTx = await TrezorConnect.signTransaction({ | |
outputs: transactionInfo.transaction.outputs, | |
inputs: transactionInfo.transaction.inputs, | |
coin: COIN, | |
// push: true | |
}); | |
console.log('Signed tx', signedTx.payload); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment