Created
March 7, 2018 20:31
-
-
Save epexa/179ee43fb91ba27897b1418371263452 to your computer and use it in GitHub Desktop.
signTransaction and getTransaction on blockchain Golos on NodeJS
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
const golos = require('golos-js'); | |
const moment = require('moment'); | |
// switch to testnet | |
golos.config.set('websocket', 'wss://ws.testnet3.golos.io'); | |
golos.config.set('chain_id', '5876894a41e6361bde2e73278f07340f2eb8b41c2facd29099de9deef6cdb679'); | |
// private posting key | |
let wif = '5J...'; | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#vote | |
let operations = [ | |
// vote operation | |
['vote', { | |
// params for vote operation | |
'author': 'golos-test', | |
'permlink': '7apsm1-qwerty', | |
'voter': 'test', | |
'weight': 10000 | |
}] | |
]; | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#get-dynamic-global-properties | |
golos.api.getDynamicGlobalProperties(function(err, properties) { | |
console.log(err, 'getDynamicGlobalProperties: ', properties); | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#get-block | |
golos.api.getBlock(properties.head_block_number - 2, function(err, block) { | |
console.log(err, 'getBlock: ', block); | |
let trx = { | |
ref_block_num: (properties.head_block_number - 3) & 0xFFFF, | |
ref_block_prefix: new Buffer(block.previous, 'hex').readUInt32LE(4), | |
expiration: moment(properties.time).add(1, 'hours').format('YYYY-MM-DDTHH:mm:ss'), // 2018-03-07T18:10:21 | |
operations: operations, | |
signatures: [], | |
}; | |
let keys = { | |
posting: wif | |
}; | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#sign-transaction | |
trx = golos.auth.signTransaction(trx, keys); | |
console.log('signTransaction: ', trx); | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#get-transaction-hex | |
golos.api.getTransactionHex(trx, function(err, result) { | |
console.log(err, 'getTransactionHex: ' + result); | |
}); | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#broadcast-transaction-synchronous | |
golos.api.broadcastTransactionSynchronous(trx, function(err, trxData) { | |
console.log(err, 'broadcastTransactionSynchronous: ', trxData); | |
// https://github.com/GolosChain/golos-js/blob/master/doc/README.md#get-transaction | |
golos.api.getTransaction(trxData.id, function(err, result) { | |
console.log(err, 'getTransaction: ', result); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment