Last active
December 10, 2020 09:27
-
-
Save batrudinych/9247dd08ee545c27044c4a05e067ffec to your computer and use it in GitHub Desktop.
sign transaction with eosjs (signle and double authorization)
This file contains 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
// npm i eosjs node-fetch | |
// node eosjs-tx-singing-example.js | |
'use strict' | |
const { Api, JsonRpc } = require('eosjs') | |
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig') | |
const fetch = require('node-fetch') | |
const { TextDecoder, TextEncoder } = require('util') | |
const privateKeys = ['_INSERT_PRIVATE_KEY_'] | |
const signatureProvider = new JsSignatureProvider(privateKeys) | |
const rpc = new JsonRpc('_INSERT_NODE_API_URL_', { fetch }) | |
async function signTransactionSingleAuth () { | |
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }) | |
// sign the transaction using private key passed earlier | |
const { serializedTransaction, signatures } = await api.transact({ | |
actions: [{ | |
account: 'eosfinextest', | |
name: 'validate', | |
authorization: [{ | |
actor: '_INSERT_ACCOUNT_NAME_', | |
permission: 'active' | |
}], | |
data: { | |
account: '_INSERT_ACCOUNT_NAME_' | |
} | |
}] | |
}, { | |
broadcast: false, // sign only | |
blocksBehind: 3, | |
expireSeconds: 7 * 24 * 60 * 60 // 7 days | |
}) | |
// get object representation | |
const deserializedTransaction = api.deserializeTransaction(serializedTransaction) | |
// get object representation including tx data if needed | |
// const deserializedTransaction = await api.deserializeTransactionWithActions(serializedTransaction) | |
deserializedTransaction.signatures = signatures | |
return deserializedTransaction | |
} | |
async function signTransactionDoubleAuth () { | |
const api = new Api({ | |
// use available keys to get partially signed transaction on the client side | |
authorityProvider: { | |
getRequiredKeys: () => signatureProvider.getAvailableKeys() | |
}, | |
rpc, | |
signatureProvider, | |
textDecoder: new TextDecoder(), | |
textEncoder: new TextEncoder() | |
}) | |
// sign the transaction using private key passed earlier | |
const { serializedTransaction, signatures } = await api.transact({ | |
actions: [{ | |
account: 'eosfinextest', | |
name: 'userkey', | |
authorization: [{ | |
actor: '_INSERT_ACCOUNT_NAME_', | |
permission: 'active' | |
}, { | |
actor: 'eosfinextest', | |
permission: 'active' | |
}], | |
data: { | |
account: '_INSERT_ACCOUNT_NAME_', | |
pubkey: '_INSERT_PUBLIC_KEY_' | |
} | |
}] | |
}, { | |
broadcast: false, // sign only | |
blocksBehind: 3, | |
expireSeconds: 60 * 60 // 1 hour | |
}) | |
// get object representation | |
const deserializedTransaction = api.deserializeTransaction(serializedTransaction) | |
// get object representation including tx data if needed | |
// const deserializedTransaction = await api.deserializeTransactionWithActions(serializedTransaction) | |
deserializedTransaction.signatures = signatures | |
return deserializedTransaction | |
} | |
async function main () { | |
console.log('signing transaction with single auth') | |
const txSingleAuthSigned = await signTransactionSingleAuth() | |
console.log(JSON.stringify(txSingleAuthSigned, null, 2)) | |
console.log('------------------------------------------------\n') | |
console.log('signing transaction with double auth') | |
const txDoubleAuthSigned = await signTransactionDoubleAuth() | |
console.log(JSON.stringify(txDoubleAuthSigned, null, 2)) | |
} | |
main() | |
.catch(console.error) | |
.finally(() => process.exit()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment