Comparing version 5.0.0 to 5.1.0
168
lib/index.js
@@ -110,62 +110,138 @@ 'use strict'; | ||
transaction = _ref.transaction; | ||
var keyProvider = config.keyProvider; | ||
var keyProvider = config.keyProvider; | ||
if (!keyProvider) { | ||
throw new TypeError('This transaction requires a config.keyProvider for signing'); | ||
} | ||
var keys = keyProvider; | ||
if (typeof keyProvider === 'function') { | ||
keyProvider = keyProvider({ transaction: transaction }); | ||
keys = keyProvider({ transaction: transaction }); | ||
} | ||
if (keyProvider) { | ||
return Promise.resolve(keyProvider).then(function (keys) { | ||
if (!Array.isArray(keys)) { | ||
keys = [keys]; | ||
} | ||
if (!keys.length) { | ||
throw new Error('missing private key(s), check your keyProvider'); | ||
if (!Array.isArray(keys)) { | ||
keys = [keys]; | ||
} | ||
if (!keys.length) { | ||
throw new Error('missing key, check your keyProvider'); | ||
} | ||
var keyMap = new Map(); | ||
// keys are either public or private keys | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
var isPrivate = ecc.isValidPrivate(key); | ||
var isPublic = ecc.isValidPublic(key); | ||
assert(isPrivate || isPublic, 'expecting public or private keys from keyProvider'); | ||
if (isPrivate) { | ||
keyMap.set(ecc.privateToPublic(key), key); | ||
} else { | ||
keyMap.set(key, null); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
// Public to private key map -> maps server's required public keys | ||
// back to signing keys. | ||
var keyMap = keys.reduce(function (map, wif) { | ||
map[ecc.privateToPublic(wif)] = wif; | ||
return map; | ||
}, {}); | ||
var pubkeys = Array.from(keyMap.keys()); | ||
return eos.getRequiredKeys(transaction, Object.keys(keyMap)).then(function (_ref2) { | ||
var required_keys = _ref2.required_keys; | ||
return eos.getRequiredKeys(transaction, pubkeys).then(function (_ref2) { | ||
var required_keys = _ref2.required_keys; | ||
if (!required_keys.length) { | ||
throw new Error('missing required keys '); | ||
if (!required_keys.length) { | ||
throw new Error('missing required keys '); | ||
} | ||
var wifs = [], | ||
missingKeys = []; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = required_keys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var requiredKey = _step2.value; | ||
var wif = keyMap.get(requiredKey); | ||
if (wif) { | ||
wifs.push(wif); | ||
} else { | ||
missingKeys.push(requiredKey); | ||
} | ||
var sigs = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
try { | ||
for (var _iterator = required_keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var key = _step.value; | ||
if (missingKeys.length !== 0) { | ||
assert(typeof keyProvider === 'function', 'keyProvider function is needed for private key lookup'); | ||
sigs.push(sign(buf, keyMap[key])); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
keyProvider({ pubkeys: missingKeys }).forEach(function (wif) { | ||
wifs.push(wif); | ||
}); | ||
} | ||
var sigs = []; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = wifs[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _wif = _step3.value; | ||
sigs.push(sign(buf, _wif)); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return sigs; | ||
}); | ||
}); | ||
} | ||
throw new TypeError('This transaction requires a config.keyProvider for signing'); | ||
return sigs; | ||
}); | ||
}; | ||
}; |
@@ -9,5 +9,11 @@ 'use strict'; | ||
var Eos = require('.'); | ||
var ecc = Eos.modules.ecc; | ||
var _require = require('eosjs-keygen'), | ||
Keystore = _require.Keystore; | ||
// even transactions that don't broadcast require Api lookups | ||
// no testnet yet, avoid breaking travis-ci | ||
if (process.env['NODE_ENV'] === 'development') { | ||
@@ -43,11 +49,38 @@ | ||
it('keyProvider', function () { | ||
// Ultimatly keyProvider should return a string or array of private keys. | ||
// Optionally use a function and(or) return a promise if needed. | ||
// This is the more advanced case. | ||
// A keyProvider can return private keys directly.. | ||
it('keyProvider private key', function () { | ||
// keyProvider should return an array of keys | ||
var keyProvider = function keyProvider() { | ||
return [wif]; | ||
}; | ||
var eos = Eos.Localnet({ keyProvider: keyProvider }); | ||
return eos.transfer('inita', 'initb', 1, '', false).then(function (tr) { | ||
assert.equal(tr.transaction.signatures.length, 1); | ||
assert.equal(_typeof(tr.transaction.signatures[0]), 'string'); | ||
}); | ||
}); | ||
// If a keystore is used, the keyProvider should return available | ||
// public keys first then respond with private keys next. | ||
it('keyProvider public keys then private key', function () { | ||
var pubkey = ecc.privateToPublic(wif); | ||
// keyProvider should return a string or array of keys. | ||
var keyProvider = function keyProvider(_ref2) { | ||
var transaction = _ref2.transaction; | ||
var transaction = _ref2.transaction, | ||
pubkeys = _ref2.pubkeys; | ||
assert.equal(transaction.messages[0].type, 'transfer'); | ||
return Promise.resolve(wif); | ||
if (!pubkeys) { | ||
assert.equal(transaction.messages[0].type, 'transfer'); | ||
return [pubkey]; | ||
} | ||
if (pubkeys) { | ||
assert.deepEqual(pubkeys, [pubkey]); | ||
return [wif]; | ||
} | ||
assert(false, 'unexpected keyProvider callback'); | ||
}; | ||
@@ -63,2 +96,9 @@ | ||
it('keyProvider from eosjs-keygen', function () { | ||
var keystore = Keystore('uid'); | ||
keystore.deriveKeys({ parent: wif }); | ||
var eos = Eos.Localnet({ keyProvider: keystore.keyProvider }); | ||
return eos.transfer('inita', 'initb', 12, '', true); | ||
}); | ||
it('signProvider', function () { | ||
@@ -65,0 +105,0 @@ var customSignProvider = function customSignProvider(_ref3) { |
@@ -14,3 +14,2 @@ 'use strict'; | ||
var assert = require('assert'); | ||
var binaryen = require('binaryen'); | ||
@@ -46,3 +45,3 @@ var _require2 = require('./format'), | ||
var override = Object.assign({}, authorityOverride, abiOverride, wasmCodeOverride, messageDataOverride(structLookup, forceMessageDataHex), config.override); | ||
var override = Object.assign({}, authorityOverride, abiOverride, wasmCodeOverride(config), messageDataOverride(structLookup, forceMessageDataHex), config.override); | ||
@@ -311,20 +310,25 @@ var eosTypes = { | ||
var wasmCodeOverride = { | ||
'setcode.code.fromObject': function setcodeCodeFromObject(_ref) { | ||
var object = _ref.object, | ||
result = _ref.result; | ||
var wasmCodeOverride = function wasmCodeOverride(config) { | ||
return { | ||
'setcode.code.fromObject': function setcodeCodeFromObject(_ref) { | ||
var object = _ref.object, | ||
result = _ref.result; | ||
var binaryen = config.binaryen; | ||
try { | ||
var code = object.code.toString(); | ||
if (/^\s*\(module/.test(code)) { | ||
console.log('Assembling WASM...'); | ||
var wasm = Buffer.from(binaryen.parseText(code).emitBinary()); | ||
result.code = wasm; | ||
} else { | ||
result.code = object.code; | ||
assert(binaryen != null, 'required: config.binaryen = require("binaryen")'); | ||
try { | ||
var code = object.code.toString(); | ||
if (/^\s*\(module/.test(code)) { | ||
console.log('Assembling WASM...'); | ||
var wasm = Buffer.from(binaryen.parseText(code).emitBinary()); | ||
result.code = wasm; | ||
} else { | ||
result.code = object.code; | ||
} | ||
} catch (error) { | ||
console.error(error, object.code); | ||
throw error; | ||
} | ||
} catch (error) { | ||
console.error(error, object.code); | ||
} | ||
} | ||
}; | ||
}; | ||
@@ -331,0 +335,0 @@ |
{ | ||
"name": "eosjs", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "General purpose library for the EOS blockchain.", | ||
@@ -36,2 +36,3 @@ "main": "lib/index.js", | ||
"coveralls": "^2.13.1", | ||
"eosjs-keygen": "^1.2.0", | ||
"istanbul": "^0.4.5", | ||
@@ -38,0 +39,0 @@ "mocha": "^3.4.2" |
@@ -149,3 +149,23 @@ [](https://travis-ci.org/EOSIO/eosjs) | ||
Deploy a smart contract. | ||
The `setcode` command accepts WASM text and converts this to binary before | ||
signing and broadcasting. For this, the Binaryen library is used. Because | ||
this is a large library it is not included in `eosjs` by default. | ||
Add binaryen to your project: | ||
```bash | ||
npm i binaryen | ||
``` | ||
Import and include the library when you configure Eos: | ||
```javascript | ||
const binaryen = require('binaryen') | ||
eos = Eos.Testnet({..., binaryen}) | ||
``` | ||
Complete example: | ||
```javascript | ||
Eos = require('eosjs') // Eos = require('./src') | ||
@@ -163,4 +183,8 @@ let {ecc} = Eos.modules | ||
eos = Eos.Localnet({keyProvider}) | ||
// Requires a large library, separate from the eosjs bundle | ||
// $ npm install binaryen | ||
binaryen = require('binaryen') | ||
eos = Eos.Localnet({keyProvider, binaryen}) | ||
eos.newaccount({ | ||
@@ -167,0 +191,0 @@ creator: 'inita', |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
363
898948
8
24624
5
1