Comparing version 1.4.6 to 2.0.0
@@ -50,3 +50,3 @@ 'use strict'; | ||
var charmap = '.abcdefghijklmnopqrstuvwxyz12345'; | ||
var charmap = '.12345abcdefghijklmnopqrstuvwxyz'; | ||
var charidx = function charidx(ch) { | ||
@@ -53,0 +53,0 @@ var idx = charmap.indexOf(ch); |
@@ -14,7 +14,7 @@ 'use strict'; | ||
describe('format', function () { | ||
// valid: 555555555555o, invalid: 555555555555p 'p' overflows the last 4 bits of the name | ||
// In isname111111k, 'k' overflows the last 4 bits of the name | ||
describe('name', function () { | ||
var nameFixture = { | ||
isname: ['555555555555o', 'isname11111a', 'isname111115', 'a', '1', '5', 'sam5', 'sam', 'adam.apple'], | ||
noname: ['555555555555p', 'noname1111111', undefined, null, 1, '6', 'a6', ' '] | ||
isname: ['isname111111j', 'a', '1', '5', 'sam5', 'sam', 'adam.applejjj'], | ||
noname: ['isname111111k', undefined, null, 1, '6', 'a6', ' '] | ||
}; | ||
@@ -77,8 +77,8 @@ | ||
it('encode / decode', function () { | ||
assert.equal(encodeName('eos'), '58923', 'encode'); | ||
assert.equal(encodeNameHex('eos'), 'e62b', 'encode hex'); | ||
assert.equal('12373', encodeName('eos'), 'encode'); | ||
assert.equal('3055', encodeNameHex('eos'), 'encode hex'); | ||
assert.equal(decodeName(encodeName('eos')), 'eos', 'decode'); | ||
assert.equal(encodeNameHex('transfer'), 'b298e982a4', 'encode'); | ||
assert.equal(decodeNameHex('b298e982a4'), 'transfer', 'decode'); | ||
assert.equal('572d3ccdcd', encodeNameHex('transfer'), 'encode'); | ||
assert.equal(decodeNameHex('572d3ccdcd'), 'transfer', 'decode'); | ||
@@ -85,0 +85,0 @@ var _iteratorNormalCompletion3 = true; |
@@ -32,4 +32,7 @@ 'use strict'; | ||
Eos.Testnet = function (config) { | ||
var defaultConfig = { transactionLog: consoleObjCallbackLog }; | ||
config = Object.assign({}, defaultConfig, config); | ||
return createEos(config, api.Testnet); | ||
}; | ||
// Eos.Mainnet = config => createEos(config, Mainnet(config)) | ||
@@ -64,2 +67,10 @@ | ||
function consoleObjCallbackLog(error, result) { | ||
if (error) { | ||
console.error(error); | ||
} else { | ||
console.log(JSON.stringify(result, null, 4)); | ||
} | ||
} | ||
/** | ||
@@ -66,0 +77,0 @@ @arg {object} network - all read-only api calls |
@@ -10,2 +10,5 @@ 'use strict'; | ||
var forceMessageDataHex = true, | ||
debug = true; | ||
if (process.env['NODE_ENV'] === 'development') { | ||
@@ -56,4 +59,4 @@ // avoid breaking travis-ci | ||
return eos.transfer('inita', 'initb', 1, '', false).then(function (tr) { | ||
assert.equal(tr.signatures.length, 1); | ||
assert.equal(_typeof(tr.signatures[0]), 'string'); | ||
assert.equal(tr.transaction.signatures.length, 1); | ||
assert.equal(_typeof(tr.transaction.signatures[0]), 'string'); | ||
}); | ||
@@ -83,3 +86,3 @@ }); | ||
it('newaccount', function () { | ||
eos = Eos.Testnet({ signProvider: signProvider, debug: false }); | ||
eos = Eos.Testnet({ signProvider: signProvider }); | ||
var pubkey = 'EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV'; | ||
@@ -113,3 +116,3 @@ // const auth = {threshold: 1, keys: [{key: pubkey, weight: 1}], accounts: []} | ||
return eos.transfer('inita', 'initb', 1, '', opts).then(function (tr) { | ||
return assert.deepEqual(tr.signatures, []); | ||
return assert.deepEqual(tr.transaction.signatures, []); | ||
}); | ||
@@ -123,2 +126,43 @@ }); | ||
it('multi-message transaction (broadcast)', function () { | ||
eos = Eos.Testnet({ signProvider: signProvider }); | ||
return eos.transaction(function (tr) { | ||
tr.transfer('inita', 'initb', 1, ''); | ||
tr.transfer({ from: 'inita', to: 'initc', amount: 1, memo: '' }); | ||
}); | ||
}); | ||
it('multi-message transaction no inner callback', function () { | ||
eos = Eos.Testnet({ signProvider: signProvider }); | ||
eos.transaction(function (tr) { | ||
tr.okproducer('inita', 'inita', 1, function (cb) {}); | ||
}).then(function () { | ||
throw 'expecting rollback'; | ||
}).catch(function (error) { | ||
assert(/Callback during a transaction/.test(error), error); | ||
}); | ||
}); | ||
it('multi-message transaction error rollback', function () { | ||
eos = Eos.Testnet({ signProvider: signProvider }); | ||
return eos.transaction(function (tr) { | ||
throw 'rollback'; | ||
}).then(function () { | ||
throw 'expecting rollback'; | ||
}).catch(function (error) { | ||
assert(/rollback/.test(error), error); | ||
}); | ||
}); | ||
it('multi-message transaction Promise.reject rollback', function () { | ||
eos = Eos.Testnet({ signProvider: signProvider }); | ||
eos.transaction(function (tr) { | ||
return Promise.reject('rollback'); | ||
}).then(function () { | ||
throw 'expecting rollback'; | ||
}).catch(function (error) { | ||
assert(/rollback/.test(error), error); | ||
}); | ||
}); | ||
it('custom transfer', function () { | ||
@@ -141,7 +185,6 @@ eos = Eos.Testnet({ signProvider: signProvider }); | ||
}] | ||
}], | ||
broadcast: false | ||
}); | ||
}] | ||
}, { broadcast: false }); | ||
}); | ||
}); | ||
} // if development |
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -12,2 +14,5 @@ | ||
var Structs = require('./structs'); | ||
var assert = require('assert'); | ||
module.exports = writeApiGen; | ||
@@ -19,3 +24,2 @@ | ||
function writeApiGen(Network, network, structs, config) { | ||
var merge = {}; | ||
@@ -27,4 +31,26 @@ | ||
merge.transaction = function (args, callback) { | ||
return transaction(args, network, structs, config, callback); | ||
/** | ||
@arg {object} transaction | ||
@arg {object} [options] | ||
@arg {function} [callback] | ||
*/ | ||
merge.transaction = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var options = void 0, | ||
callback = void 0; | ||
if (args.length > 1 && typeof args[args.length - 1] === 'function') { | ||
callback = args.pop(); | ||
} | ||
if (args.length > 1 && _typeof(args[args.length - 1]) === 'object') { | ||
options = args.pop(); | ||
} | ||
assert.equal(args.length, 1, 'Transaction args: transaction, [options], [callback]'); | ||
var arg = args[0]; | ||
return transaction(arg, options, network, structs, config, merge, callback); | ||
}; | ||
@@ -53,4 +79,4 @@ | ||
return function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
@@ -63,2 +89,11 @@ | ||
// Special case like multi-message transactions where this lib needs | ||
// to be sure the broadcast is off. | ||
var optionOverrides = {}; | ||
var lastArg = args[args.length - 1]; | ||
if ((typeof lastArg === 'undefined' ? 'undefined' : _typeof(lastArg)) === 'object' && _typeof(lastArg.__optionOverrides) === 'object') { | ||
// pop() fixes the args.length | ||
Object.assign(optionOverrides, args.pop().__optionOverrides); | ||
} | ||
// Normalize the extra optional options argument | ||
@@ -81,2 +116,11 @@ var optionsFormatter = function optionsFormatter(option) { | ||
// internal options (ex: multi-message transaction) | ||
Object.assign(options, optionOverrides); | ||
if (optionOverrides.noCallback && !returnPromise) { | ||
throw new Error('Callback during a transaction are not supported'); | ||
} | ||
var tr = Object.assign({ | ||
@@ -89,3 +133,3 @@ messages: [{ | ||
}] | ||
}, options); | ||
}); | ||
@@ -114,51 +158,28 @@ if (!tr.scope) { | ||
tr.scope = tr.scope.sort(); | ||
transactionArg(tr, callback); | ||
return returnPromise; | ||
}; | ||
} | ||
var Structs = require('./structs'); | ||
// multi-message transaction support | ||
if (!optionOverrides.messageOnly) { | ||
transactionArg(tr, options, callback); | ||
} else { | ||
callback(null, tr); | ||
} | ||
function usage(type, definition, Network) { | ||
var _Structs = Structs({ defaults: true, network: Network }), | ||
structs = _Structs.structs; | ||
var struct = structs[type]; | ||
if (struct == null) { | ||
throw TypeError('Unknown type: ' + type); | ||
} | ||
var usage = ''; | ||
var out = function out() { | ||
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
usage += str + '\n'; | ||
return returnPromise; | ||
}; | ||
out('' + type); | ||
out(); | ||
out('USAGE'); | ||
out('' + JSON.stringify(definition, null, 4)); | ||
out(); | ||
out('EXAMPLE STRUCTURE'); | ||
out('' + JSON.stringify(struct.toObject(), null, 4)); | ||
return usage; | ||
} | ||
/** | ||
@args {object} args - { | ||
@args {object} args { | ||
scope: [], | ||
messages: [{}, ..], | ||
sign: [wif_string], | ||
messages: [{}, ..] | ||
} | ||
@args {object} options { | ||
[expireInSeconds = 60], | ||
[broadcast = true] | ||
[broadcast = true], | ||
[sign = true] | ||
} | ||
*/ | ||
function transaction(args, network, structs, config, callback) { | ||
if ((typeof args === 'undefined' ? 'undefined' : _typeof(args)) !== 'object') { | ||
throw new TypeError('Expecting args object'); | ||
} | ||
function transaction(arg, options, network, structs, config, merge, callback) { | ||
var optionDefault = { expireInSeconds: 60, broadcast: true, sign: true }; | ||
options = Object.assign({} /*clone*/, optionDefault, options); | ||
@@ -178,10 +199,32 @@ var returnPromise = void 0; | ||
if (!Array.isArray(args.scope)) { | ||
throw new TypeError('Expecting args.scope array'); | ||
if (typeof arg === 'function') { | ||
var cbPromis = atomicTransaction(arg, options, merge, callback); | ||
return returnPromise ? returnPromise : cbPromis; | ||
} | ||
if (!Array.isArray(args.messages)) { | ||
throw new TypeError('Expecting args.messages array'); | ||
if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) !== 'object') { | ||
throw new TypeError('First transaction argument should be an object or function'); | ||
} | ||
args.messages.forEach(function (message) { | ||
if (!Array.isArray(arg.scope)) { | ||
throw new TypeError('Expecting scope array'); | ||
} | ||
if (!Array.isArray(arg.messages)) { | ||
throw new TypeError('Expecting messages array'); | ||
} | ||
if (config.transactionLog) { | ||
// wrap the callback with the logger | ||
var superCallback = callback; | ||
callback = function callback(error, tr) { | ||
if (error) { | ||
config.transactionLog(error); | ||
} else { | ||
config.transactionLog(null, tr); | ||
} | ||
superCallback(error, tr); | ||
}; | ||
} | ||
arg.messages.forEach(function (message) { | ||
if (!Array.isArray(message.authorization) || message.authorization.length === 0) { | ||
@@ -192,13 +235,10 @@ throw new TypeError('Expecting message.authorization array', message); | ||
var argsDefaults = { expireInSeconds: 60, broadcast: true, sign: true }; | ||
args = Object.assign(argsDefaults, args); | ||
if (args.sign && typeof config.signProvider !== 'function') { | ||
if (options.sign && typeof config.signProvider !== 'function') { | ||
throw new TypeError('Expecting config.signProvider function (disable using {sign: false})'); | ||
} | ||
network.createTransaction(args.expireInSeconds, checkError(callback, function (rawTx) { | ||
rawTx.scope = args.scope; | ||
rawTx.messages = args.messages; | ||
rawTx.readscope = args.readscope || []; | ||
network.createTransaction(options.expireInSeconds, checkError(callback, function (rawTx) { | ||
rawTx.scope = arg.scope; | ||
rawTx.messages = arg.messages; | ||
rawTx.readscope = arg.readscope || []; | ||
@@ -215,4 +255,6 @@ var Transaction = structs.Transaction; | ||
var transactionId = createHash('sha256').update(buf).digest().toString('hex'); | ||
var sigs = []; | ||
if (args.sign) { | ||
if (options.sign) { | ||
var chainIdBuf = new Buffer(config.chainId, 'hex'); | ||
@@ -231,10 +273,16 @@ var signBuf = Buffer.concat([chainIdBuf, buf]); | ||
console.log(JSON.stringify(tr, null, 4)); // DEBUG | ||
if (!args.broadcast || !args.sign) { | ||
callback(null, tr); | ||
if (!options.broadcast) { | ||
callback(null, { | ||
transaction_id: transactionId, | ||
broadcast: false, | ||
transaction: tr | ||
}); | ||
} else { | ||
network.pushTransaction(tr, function (error) { | ||
if (!error) { | ||
callback(null, tr); | ||
callback(null, { | ||
transaction_id: transactionId, | ||
broadcast: true, | ||
transaction: tr | ||
}); | ||
} else { | ||
@@ -254,2 +302,122 @@ console.error('[push_transaction error] \'' + error.message + '\', digest \'' + buf.toString('hex') + '\''); | ||
function atomicTransaction(tr, options, merge, callback) { | ||
assert.equal(typeof tr === 'undefined' ? 'undefined' : _typeof(tr), 'function', 'tr'); | ||
assert.equal(typeof options === 'undefined' ? 'undefined' : _typeof(options), 'object', 'options'); | ||
var scope = {}; | ||
var messageList = []; | ||
var messageCollector = {}; | ||
var _loop = function _loop(op) { | ||
messageCollector[op] = function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
// call the original function but force-disable a lot of stuff | ||
var opFunction = merge[op]; | ||
var ret = opFunction.apply(undefined, args.concat([{ | ||
__optionOverrides: { | ||
broadcast: false, | ||
messageOnly: true, | ||
noCallback: true | ||
} | ||
}])); | ||
if (ret == null) { | ||
// double-check (code can change) | ||
throw new Error('Callbacks can not be used when creating a multi-message transaction'); | ||
} | ||
messageList.push(ret); | ||
}; | ||
}; | ||
for (var op in merge) { | ||
_loop(op); | ||
} | ||
var collectorPromise = void 0; | ||
try { | ||
// caller will load this up with messages | ||
collectorPromise = tr(messageCollector); | ||
} catch (error) { | ||
collectorPromise = Promise.reject(error); | ||
} | ||
Promise.resolve(collectorPromise).then(function () { | ||
return Promise.all(messageList).then(function (resolvedMessageList) { | ||
var scopes = new Set(); | ||
var messages = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = resolvedMessageList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var m = _step.value; | ||
var _scope = m.scope, | ||
_m$messages = _slicedToArray(m.messages, 1), | ||
message = _m$messages[0]; | ||
_scope.forEach(function (s) { | ||
scopes.add(s); | ||
}); | ||
messages.push(message); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
var trObject = {}; | ||
trObject.scope = Array.from(scopes).sort(); | ||
trObject.messages = messages; | ||
merge.transaction(trObject, options, callback); | ||
}); | ||
}).catch(function (error) { | ||
// console.error(error) | ||
callback(error); | ||
}); | ||
} | ||
function usage(type, definition, Network) { | ||
var _Structs = Structs({ defaults: true, network: Network }), | ||
structs = _Structs.structs; | ||
var struct = structs[type]; | ||
if (struct == null) { | ||
throw TypeError('Unknown type: ' + type); | ||
} | ||
var usage = ''; | ||
var out = function out() { | ||
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
usage += str + '\n'; | ||
}; | ||
out('' + type); | ||
out(); | ||
out('USAGE'); | ||
out('' + JSON.stringify(definition, null, 4)); | ||
out(); | ||
out('EXAMPLE STRUCTURE'); | ||
out('' + JSON.stringify(struct.toObject(), null, 4)); | ||
return usage; | ||
} | ||
var checkError = function checkError(parentErr, parrentRes) { | ||
@@ -256,0 +424,0 @@ return function (error, result) { |
{ | ||
"name": "eosjs", | ||
"version": "1.4.6", | ||
"version": "2.0.0", | ||
"description": "General purpose library for the EOS blockchain.", | ||
@@ -10,4 +10,4 @@ "main": "lib/index.js", | ||
"coveralls": "npm run coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", | ||
"build": "node_modules/babel-cli/bin/babel.js src --out-dir lib", | ||
"prepublish": "npm run build" | ||
"prepublish": "node_modules/babel-cli/bin/babel.js src --out-dir lib", | ||
"build": "mkdir -p dist && browserify -o dist/eos.js -s Eos src/index.js" | ||
}, | ||
@@ -32,2 +32,3 @@ "repository": { | ||
"babel-preset-es2015": "^6.24.1", | ||
"browserify": "^14.4.0", | ||
"coveralls": "^2.13.1", | ||
@@ -34,0 +35,0 @@ "istanbul": "^0.4.5", |
@@ -41,2 +41,6 @@ [](https://travis-ci.org/EOSIO/eosjs) | ||
### Transaction Options | ||
`options = {broadcast: true, sign: true, expireInSeconds: N, debug: false}` | ||
### Usage (read/write) | ||
@@ -46,2 +50,3 @@ | ||
Eos = require('eosjs') // Or Eos = require('./src') | ||
eos = Eos.Testnet({keyProvider: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3'}) | ||
@@ -53,11 +58,11 @@ | ||
// Object or ordered args may be used | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1, memo: ''}) | ||
// Usage with options | ||
options = {broadcast: false} | ||
eos.transfer({from: 'inita', to: 'initb', amount: 1, memo: ''}, options) | ||
// Object or ordered args may be used. Options are optional. | ||
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, memo: ''}, {broadcast: false}) | ||
// A broadcast boolean may be provided as a shortcut for {broadcast: false} | ||
eos.transfer('inita', 'initb', 1, '', false) | ||
``` | ||
@@ -131,2 +136,26 @@ | ||
### Atomic Operations | ||
Blockchain level atomic operations. All will pass or fail. | ||
```javascript | ||
Eos = require('eosjs') // Or Eos = require('./src') | ||
keyProvider = '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3' | ||
eos = Eos.Testnet({keyProvider}) | ||
eos.transaction(tr => | ||
{ | ||
tr.transfer('inita', 'initb', 1, '') | ||
tr.transfer({from: 'inita', to: 'initc', amount: 1, memo: ''}) | ||
// A returned promise or thrown error is handled as expected | ||
} | ||
// options, | ||
// callback | ||
) | ||
``` | ||
### Usage (manual) | ||
@@ -166,2 +195,18 @@ | ||
# Development | ||
From time-to-time the eosjs and eosd binary format will change between releases | ||
so you may need to start `eosd` with the `--skip-transaction-signatures` parameter | ||
to get your transactions to pass. | ||
Note, `package.json` has a "main" pointing to `./lib`. The `./lib` folder is for | ||
es2015 code built in a separate step. If your changing and testing code, | ||
import from `./src` instead. | ||
```javascript | ||
Eos = require('./src') | ||
``` | ||
Use Node v8+ to `package-lock.json`. | ||
# Related Libraries | ||
@@ -175,3 +220,3 @@ | ||
## About | ||
# About | ||
@@ -199,3 +244,20 @@ * eosjs-api [[Github](https://github.com/eosio/eosjs-api), [NPM](https://www.npmjs.org/package/eosjs-api)] | ||
# Browser | ||
```bash | ||
git clone https://github.com/EOSIO/eosjs.git | ||
cd eosjs | ||
npm install | ||
npm run build | ||
# builds: ./dist/eos.js | ||
``` | ||
```html | ||
<script src="eos.js"></script> | ||
<script> | ||
var eos = Eos.Testnet() | ||
//... | ||
</script> | ||
``` | ||
# Environment | ||
@@ -202,0 +264,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3547073
17
41956
261
7