From 4ecd581606d651bd955da1571ced7b5b33f80e75 Mon Sep 17 00:00:00 2001 From: Toniya Date: Thu, 21 Feb 2019 18:05:02 +0800 Subject: [PATCH 1/2] updating transaction with accordance with intergallactic --- .gitignore | 1 + README.md | 6 +- actions.js | 8 +- index.js | 34 ++++--- libs/transactions/transaction.js | 159 +++++++++++-------------------- package.json | 2 +- 6 files changed, 80 insertions(+), 130 deletions(-) diff --git a/.gitignore b/.gitignore index 3fcdf83..c5a3d01 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ typings/ .next package-lock.json +.DS_Store # Snak project folders diff --git a/README.md b/README.md index b3a4561..33bd607 100644 --- a/README.md +++ b/README.md @@ -153,17 +153,17 @@ Commands: No need to initialize a project before using this command. - transact|tx [options]
+ transact|tx [options]
(Unsafe!) Do regular transaction to a contract, you need pass the private key of sender and address of contract you need to initialize a project before using this command. - bond|bnd [options] + bond|bnd [options] (safe) Do Bond transaction, you need pass the validator publickey, stake amount, transaction fee, and private key of sender you may need to initialize a project before using this command. - unbond|ubnd [options]
+ unbond|ubnd [options]
(safe) Do Unbond transaction, you need pass account address, stake amount, transaction fee and private key of the validator you may need to initialize a project before using this command. Note: you should be a validator to do unbond transaction. diff --git a/actions.js b/actions.js index af71160..1636786 100644 --- a/actions.js +++ b/actions.js @@ -420,16 +420,16 @@ module.exports = class Action { }); } - broadcastBond(pubKey,amount,fee,privKey){ - return this._txHandler().bond(pubKey,amount,fee,privKey).then(data =>{ + broadcastBond(pubKey,amount,privKey){ + return this._txHandler().bond(pubKey,amount,privKey).then(data =>{ logger.console("Safe Bond Tx result :\n" + JSON.stringify(data,null,4)); }).catch(ex => { logger.error(ex); }); } - broadcastUnbond(address,amount,fee,privKey){ - return this._txHandler().unbond(address,amount,fee,privKey).then(data =>{ + broadcastUnbond(address,amount,privKey){ + return this._txHandler().unbond(address,amount,privKey).then(data =>{ logger.console("Safe Unbond Tx result :\n" + JSON.stringify(data,null,4)); }).catch(ex => { logger.error(ex); diff --git a/index.js b/index.js index bdadafe..f966733 100755 --- a/index.js +++ b/index.js @@ -89,7 +89,7 @@ program .action((address) => actions.validatorInfo(address)); program - .command('list_accounts') + .command('list_accounts') .alias('lacnt') .description('\nLoad all accounts\ \nyou need to initialize a project before using this command.\n\n') @@ -143,28 +143,28 @@ program .alias('tx') .description('\n(Unsafe!) Do regular transaction to a contract, you need pass the private key of sender and address of contract\ \nyou need to initialize a project before using this command.\n\n') - .action((priv_key,data,address,fee,gas_limit,cmd) => actions.transact(priv_key,data,address,fee,gas_limit,cmd.unsafe)); + .action((priv_key,data,address,gas_limit,cmd) => actions.transact(priv_key,data,address,fee,gas_limit,cmd.unsafe)); program - .command('bond ') - .option('-u, --unsafe', 'unsafe transaction') //TODO (unsafe should be implemented using privatekey) + .command('bond ') + .option('-u, --unsafe', 'unsafe transaction') //TODO .alias('bnd') - .description('\n(safe) Do Bond transaction, you need pass the validator publickey, stake amount, transaction fee, and private key of sender \ + .description('\n(safe) Do Bond transaction, you need pass the validator publickey, stake amount and private key of sender \ \nyou may need to initialize a project before using this command.\n\n') - .action((public_key,amount,fee,priv_key) => actions.broadcastBond(public_key,parseInt(amount),parseInt(fee),priv_key)); + .action((public_key,amount,priv_key) => actions.broadcastBond(public_key,parseInt(amount), priv_key)); program - .command('unbond
') + .command('unbond
') .option('-u, --unsafe', 'unsafe sending transaction') .alias('ubnd') - .description('\n(safe) Do Unbond transaction, you need pass account address, stake amount, transaction fee and private key of the validator \ + .description('\n(safe) Do Unbond transaction, you need pass account address, stake amount and private key of the validator \ \nyou may need to initialize a project before using this command.\ \nNote: you should be a validator to do unbond transaction.\n\n') - .action((address,amount,fee,priv_key) => actions.broadcastUnbond(address,parseInt(amount),parseInt(fee),priv_key)); + .action((address,amount,priv_key) => actions.broadcastUnbond(address,parseInt(amount),priv_key)); program .command('send
') - .option('-u, --unsafe', 'unsafe sending transaction') //TODO (unsafe should be implemented using privatekey) + .option('-u, --unsafe', 'unsafe sending transaction') //TODO .alias('snd') .description('\n(safe) Do regular transaction, you need to pass the address of the receiver, amount and the private key of sender \ \nyou may need to initialize a project before using this command.\n\n') @@ -178,13 +178,6 @@ program \nyou may need to initialize a project before using this command.\n\n') .action((address,perm_value,priv_key) => actions.permission(address,perm_value,priv_key)); - program - .command('*') - .action(function(others){ - console.log('[Error] There isn\'t any command for "%s" \n\ - please type gnet -h for more helps.\n', others); - }); - program .command('call [parameters_list]') .alias('calf') @@ -249,6 +242,11 @@ program \nYou may need to initialize a project before using this command.\n\n") .action(() => actions.getConfig()); - + program + .command('*') + .action(function(others){ + console.log('[Error] There isn\'t any command for "%s" \n\ + please type gnet -h for more helps.\n', others); + }); program.parse(process.argv); diff --git a/libs/transactions/transaction.js b/libs/transactions/transaction.js index 77c8a6d..a626c56 100644 --- a/libs/transactions/transaction.js +++ b/libs/transactions/transaction.js @@ -1,106 +1,57 @@ -'use strict' - -var Keys = require('../keys'); -var keys = new Keys; - -const SEND_TX_TYPE = 0x1 -const BOND_TX_TYPE = 0x3; -const UNBOND_TX_TYPE = 0x4; -const PERMISSION_TX_TYPE = 0x5; +"use strict"; module.exports = class Transaction { - - constructor(intergallactic) { - this.igc = intergallactic; - } - - send(address, amount, priv_key) { - const account = keys.getAccountInfo(priv_key) - - const myTx = { - from: [{ - address: account.acAddress, - amount: amount - }], - to: [{ - address: address, - amount: amount - }] - }; - - return this.signAndBroadcast(myTx, priv_key, SEND_TX_TYPE) - } - - permission(address,perm_value,priv_key) { - const account = keys.getAccountInfo(priv_key) - const myTx = { - from: { - address: account.acAddress, - amount: 0 - }, - to: { - address: address, - amount: 0 - }, - permissions: perm_value, - set: true - } - - return this.signAndBroadcast(myTx, priv_key,PERMISSION_TX_TYPE) - } - - transact() { - let myTx = this.buildTxn(fromAddress, toAddress, amount) - } - - bond(pubKey,amount,fee,priv_key) { - const account = keys.getAccountInfo(priv_key) - const vaAccount = keys.getInfoByPublicKey(pubKey) - - const myTx = { - from: { - address: account.acAddress, - amount: amount - }, - to: { - address: vaAccount.vaAddress, - amount: amount - }, - publicKey: pubKey, - gasLimit: fee - }; - - return this.signAndBroadcast(myTx, priv_key, BOND_TX_TYPE) - } - - unbond(address,amount,fee,priv_key) { - const account = keys.getAccountInfo(priv_key) - - const myTx = { - from: { - address: account.vaAddress, - amount: amount - }, - to: { - address: address, - amount: amount - }, - gasLimit: fee - }; - - return this.signAndBroadcast(myTx, priv_key, UNBOND_TX_TYPE) - } - - randomTransact() { - //TODO - } - - signAndBroadcast(txnObject, priv_key, txnType) { - - const newTxn = new this.igc.Transaction(txnObject, { type: txnType }); - - return newTxn.signNBroadcast(priv_key).then(data => { - return (data); - }) - } -} \ No newline at end of file + constructor(intergallactic) { + this.igc = intergallactic; + } + + send(address, amount, priv_key) { + const myTx = { + to: address, + amount: amount + }; + const newTxn = new this.igc.Transaction(myTx); + return newTxn.send(priv_key); + } + + permission(address, perm_value, priv_key) { + const myTx = { + to: address, + amount: amount, + permissions:perm_value, + set: true + }; + const newTxn = new this.igc.Transaction(myTx); + return newTxn.permission(priv_key); + } + + bond(pubKey, amount, priv_key) { + const myTx = { + to: address, + amount: amount, + publicKey: pubKey + }; + const newTxn = new this.igc.Transaction(myTx); + return newTxn.bond(priv_key); + } + + unbond(address, amount, priv_key) { + const myTx = { + to: address, + amount: amount + }; + const newTxn = new this.igc.Transaction(myTx); + return newTxn.send(priv_key); + } + + call(to, gaslimit, amount, data) { + const myTx = { + to: to, + gasLimit: gaslimit, + amount: amount, + data: data + }; + const newTxn = new this.igc.Transaction(myTx); + return newTxn.call(priv_key); + } +}; diff --git a/package.json b/package.json index bdc3280..908aaa4 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "dependencies": { "commander": "^2.9.0", "gallactickeys": "^0.2.2", - "intergallactic": "github:gallactic/intergallactic#dev", + "intergallactic": "^0.1.0", "md5": "2.2.1", "mocha": "^5.2.0", "promise": "8.0.1", From dec8951fab741150b06fa56eb7f74a2a9540b9ad Mon Sep 17 00:00:00 2001 From: Toniya Date: Fri, 22 Feb 2019 11:27:57 +0800 Subject: [PATCH 2/2] fixing bugs --- README.md | 6 +++--- index.js | 4 ++-- libs/transactions/transaction.js | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 33bd607..734d104 100644 --- a/README.md +++ b/README.md @@ -159,12 +159,12 @@ Commands: bond|bnd [options] - (safe) Do Bond transaction, you need pass the validator publickey, stake amount, transaction fee, and private key of sender + (safe) Do Bond transaction, you need pass the validator publickey, stake amount and private key of sender you may need to initialize a project before using this command. unbond|ubnd [options]
- (safe) Do Unbond transaction, you need pass account address, stake amount, transaction fee and private key of the validator + (safe) Do Unbond transaction, you need pass account address, stake amount and private key of the validator you may need to initialize a project before using this command. Note: you should be a validator to do unbond transaction. @@ -180,7 +180,7 @@ Commands: permission|perm [options]
- (safe) Do regular permission transaction, you need to pass the permission value and address of the receiver, private key of sender + (safe) Do regular permission transaction, you need to pass the permission value and address of the receiver, private key of any one of the validator you may need to initialize a project before using this command. diff --git a/index.js b/index.js index f966733..9320312 100755 --- a/index.js +++ b/index.js @@ -172,9 +172,9 @@ program program .command('permission
') - .option('-u, --unsafe', 'unsafe sending transaction') //TODO (unsafe should be implemented using privatekey) + .option('-u, --unsafe', 'unsafe sending transaction') //TODO .alias('perm') - .description('\n(safe) Do regular permission transaction, you need to pass the permission value and address of the receiver, private key of sender \ + .description('\n(safe) Do regular permission transaction, you need to pass the permission value and address of the receiver, private key of validator \ \nyou may need to initialize a project before using this command.\n\n') .action((address,perm_value,priv_key) => actions.permission(address,perm_value,priv_key)); diff --git a/libs/transactions/transaction.js b/libs/transactions/transaction.js index a626c56..f855345 100644 --- a/libs/transactions/transaction.js +++ b/libs/transactions/transaction.js @@ -1,5 +1,6 @@ "use strict"; - +var Keys = require('../keys'); +var keys = new Keys; module.exports = class Transaction { constructor(intergallactic) { this.igc = intergallactic; @@ -17,7 +18,7 @@ module.exports = class Transaction { permission(address, perm_value, priv_key) { const myTx = { to: address, - amount: amount, + amount: 0, permissions:perm_value, set: true }; @@ -26,8 +27,9 @@ module.exports = class Transaction { } bond(pubKey, amount, priv_key) { + const vaAccount = keys.getInfoByPublicKey(pubKey) const myTx = { - to: address, + to: vaAccount.vaAddress, amount: amount, publicKey: pubKey };