Comparing version 1.2.0 to 1.3.0
16
index.js
@@ -50,6 +50,14 @@ const ecc = require('eosjs-ecc') | ||
function mergeWriteFunctions(config = {}, Network) { | ||
config = Object.assign({}, config) | ||
// limit signProvider's scope as much as possible | ||
const signProvider = config.signProvider | ||
config = Object.assign({}, config) | ||
if(!config.chainId) { | ||
config.chainId = '00'.repeat(32) | ||
} | ||
const signProvider = config.signProvider ? config.signProvider : ({sign, buf}) => { | ||
if(!config.privateKey) { | ||
throw new TypeError('This transaction requires signing. Provide a config.privateKey string (wif) or config.signProvider function') | ||
} | ||
return sign(buf, config.privateKey) | ||
} | ||
delete config.signProvider | ||
@@ -66,3 +74,3 @@ | ||
const writeApi = writeApiGen(Network, network, eos.structs, signProvider) | ||
const writeApi = writeApiGen(Network, network, eos.structs, signProvider, config.chainId) | ||
throwOnDuplicate(merge, writeApi, 'Conflicting methods in Eos and Transaction Api') | ||
@@ -69,0 +77,0 @@ merge = Object.assign(merge, writeApi) |
@@ -22,9 +22,3 @@ /* eslint-env mocha */ | ||
const wif = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3' | ||
const chainId = new Buffer('\0'.repeat(32)) | ||
const signProvider = ({sign, buf}) => { | ||
buf = Buffer.concat([chainId, buf]) | ||
return sign(buf, wif) | ||
} | ||
const signProvider = ({sign, buf}) => sign(buf, wif) | ||
const promiseSigner = (args) => Promise.resolve(signProvider(args)) | ||
@@ -41,3 +35,3 @@ | ||
const auth = {threshold: 1, keys: [{key: pubkey, weight: 1}], accounts: []} | ||
const name = 'acct' + String(Math.round(Math.random() * 100000)).replace(/[0,6-9]/g, '') | ||
const name = 'act' + String(Math.round(Math.random() * 10000000)).replace(/[0,6-9]/g, '') | ||
@@ -59,2 +53,14 @@ return eos.newaccount({ | ||
it('transfer (get_required_keys)', () => { | ||
const keySigner = ({buf, sign, transaction}) => { | ||
const pubkey = 'EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV' | ||
return eos.getRequiredKeys(transaction, [pubkey]).then(res => { | ||
assert.deepEqual(res.required_keys, [pubkey]) | ||
return sign(buf, wif) // return hex string signature or array of signatures | ||
}) | ||
} | ||
eos = Eos.Testnet({signProvider: keySigner}) | ||
return eos.transfer('inita', 'initb', 2, '') | ||
}) | ||
it('transfer (no broadcast)', () => { | ||
@@ -61,0 +67,0 @@ eos = Eos.Testnet({signProvider}) |
{ | ||
"name": "eosjs", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "General purpose library for the EOS blockchain.", | ||
@@ -32,7 +32,7 @@ "main": "index.js", | ||
"create-hash": "^1.1.3", | ||
"eosjs-api": "^1.1.0", | ||
"eosjs-api": "^1.2.0", | ||
"eosjs-ecc": "^1.1.0", | ||
"eosjs-json": "^1.1.0", | ||
"eosjs-json": "^1.2.0", | ||
"fcbuffer": "^1.1.0" | ||
} | ||
} |
@@ -43,13 +43,4 @@ [](https://travis-ci.org/EOSIO/eosjs) | ||
Eos = require('eosjs') // Or Eos = require('.') | ||
eos = Eos.Testnet({privateKey: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'}) | ||
eos = Eos.Testnet({ | ||
signProvider: ({authorization, buf, sign}) => { | ||
// Example: account === 'inita' && permission === 'active' | ||
const {account, permission} = authorization | ||
// 'sign' returns a hex string. A Promise resolving to a Hex string works too. | ||
return sign(buf, '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3') | ||
} | ||
}) | ||
// All Eos transactions as of the last update are available. Run with no | ||
@@ -60,12 +51,16 @@ // arguments to print usage. | ||
// Object or ordered args may be used | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1}) | ||
eos.transfer('inita', 'initb', 1) | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1, memo: ''}) | ||
eos.transfer('inita', 'initb', 1, 'memo') | ||
// A broadcast boolean may be provided allowing any transaction to be created | ||
// but not sent. | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1}, {broadcast: false}) | ||
eos.transfer('inita', 'initb', 1, false) | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1, memo: ''}, {broadcast: false}) | ||
eos.transfer('inita', 'initb', 1, '', false) | ||
``` | ||
For more advanced signing, see `signProvider` in the [unit test](./index.test.js). | ||
### Shorthand | ||
Shorthand is available for some types such as Asset and Authority. | ||
@@ -79,6 +74,4 @@ | ||
Eos = require('eosjs') // Or Eos = require('.') | ||
eos = Eos.Testnet({privateKey: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'}) | ||
wif = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3' | ||
eos = Eos.Testnet({signProvider: ({sign, buf}) => sign(buf, wif)}) | ||
eos.newaccount({ | ||
@@ -103,5 +96,5 @@ creator: 'inita', | ||
```javascript | ||
Eos = require('eosjs') // Or Eos = require('.') | ||
eos = Eos.Testnet({privateKey: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'}) | ||
// eos = Eos.Testnet({signProvider: ...}) | ||
eos.transaction({ | ||
@@ -120,3 +113,4 @@ scope: ['inita', 'initb'], | ||
to: 'initb', | ||
amount: 7 | ||
amount: 7, | ||
memo: '' | ||
} | ||
@@ -123,0 +117,0 @@ } |
@@ -10,8 +10,12 @@ const ecc = require('eosjs-ecc') | ||
function writeApiGen(Network, network, structs, signProvider) { | ||
function writeApiGen(Network, network, structs, signProvider, chainId) { | ||
let merge = {} | ||
if(typeof chainId !== 'string') { | ||
throw new TypeError('chainId is required') | ||
} | ||
merge.transaction = (args, callback) => | ||
transaction(args, network, structs, signProvider, callback) | ||
transaction(args, network, structs, signProvider, chainId, callback) | ||
@@ -47,3 +51,3 @@ for(let type in Network.schema) { | ||
if(typeof option === 'object') { | ||
return option // {debug, broadcast, scope, etc} (scope, etc my overwrite tx below) | ||
return option // {debug, broadcast, scope, etc} (scope, etc my overwrite tr below) | ||
} | ||
@@ -59,3 +63,3 @@ if(typeof option === 'boolean') { | ||
const tx = Object.assign({ | ||
const tr = Object.assign({ | ||
messages: [{ | ||
@@ -69,10 +73,10 @@ code: 'eos', | ||
if(!tx.scope) {// FIXME Hack, until an API call is available | ||
if(!tr.scope) {// FIXME Hack, until an API call is available | ||
const fields = Object.keys(definition.fields) | ||
tx.scope = [] | ||
tr.scope = [] | ||
const f1 = fields[0] | ||
if(definition.fields[f1] === 'AccountName') { | ||
tx.scope.push(params[f1]) | ||
tx.messages[0].authorization.push({ | ||
tr.scope.push(params[f1]) | ||
tr.messages[0].authorization.push({ | ||
account: params[f1], | ||
@@ -86,3 +90,3 @@ permission: 'active' | ||
if(definition.fields[f2] === 'AccountName') { | ||
tx.scope.push(params[f2]) | ||
tr.scope.push(params[f2]) | ||
} | ||
@@ -92,3 +96,3 @@ } | ||
transactionArg(tx, callback) | ||
transactionArg(tr, callback) | ||
return returnPromise | ||
@@ -134,3 +138,3 @@ } | ||
*/ | ||
function transaction(args, network, structs, signProvider, callback) { | ||
function transaction(args, network, structs, signProvider, chainId, callback) { | ||
if(typeof args !== 'object') { | ||
@@ -181,32 +185,27 @@ throw new TypeError('Expecting args object') | ||
const buf = Fcbuffer.toBuffer(Transaction, txObject) | ||
// console.log('txObject', JSON.stringify(txObject,null,4)) | ||
// Broadcast what is signed (instead of rawTx) | ||
const tx = Fcbuffer.fromBuffer(Transaction, buf) | ||
const tr = Fcbuffer.fromBuffer(Transaction, buf) | ||
tx.signatures = [] | ||
if(args.sign) { | ||
for(const message of args.messages) { | ||
for(const authorization of message.authorization) { | ||
const sig = signProvider({authorization, tx, message, buf, sign}) | ||
if(typeof sig !== 'string' && | ||
(typeof sig !== 'object' || typeof sig.then !== 'function') | ||
) { | ||
throw new Error('signProvider should return a Promise or signature hex') | ||
} | ||
tx.signatures.push(sig) | ||
} | ||
let sigs = [] | ||
if(args.sign){ | ||
const chainIdBuf = new Buffer(chainId, 'hex') | ||
const signBuf = Buffer.concat([chainIdBuf, buf]) | ||
sigs = signProvider({transaction: tr, buf: signBuf, sign}) | ||
if(!Array.isArray(sigs)) { | ||
sigs = [sigs] | ||
} | ||
} | ||
// tx.signatures can be just strings or Promises | ||
Promise.all(tx.signatures).then(sigs => { | ||
tx.signatures = sigs | ||
if(!args.broadcast) { | ||
callback(null, tx) | ||
// tr.signatures can be just strings or Promises | ||
Promise.all(sigs).then(sigs => { | ||
tr.signatures = sigs | ||
if(!args.broadcast || !args.sign) { | ||
callback(null, tr) | ||
} else { | ||
network.pushTransaction(tx, error => { | ||
network.pushTransaction(tr, error => { | ||
if(!error) { | ||
callback(null, tx) | ||
callback(null, tr) | ||
} else { | ||
@@ -216,3 +215,3 @@ let sbuf = buf | ||
// // FIXME - Error: Required UInt64 transfer.amount Message.data Transaction.messages | ||
// sbuf = Fcbuffer.toBuffer(structs.SignedTransaction, tx) | ||
// sbuf = Fcbuffer.toBuffer(structs.SignedTransaction, tr) | ||
// } catch(error) { | ||
@@ -219,0 +218,0 @@ // console.log(error) |
36240
872
155
Updatedeosjs-api@^1.2.0
Updatedeosjs-json@^1.2.0