@ledgerhq/hw-app-eth
Advanced tools
Comparing version 1.1.1-beta.068e2a14 to 1.1.2-beta.068e2a14
335
lib/Eth.js
@@ -7,27 +7,4 @@ "use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /******************************************************************************** | ||
* Ledger Node JS API | ||
* (c) 2016-2017 Ledger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
// FIXME drop: | ||
var _utils = require("./utils"); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/** | ||
@@ -40,6 +17,5 @@ * Ethereum API | ||
*/ | ||
var Eth = function () { | ||
function Eth(transport) { | ||
_classCallCheck(this, Eth); | ||
class Eth { | ||
constructor(transport) { | ||
this.transport = transport; | ||
@@ -58,176 +34,159 @@ transport.setScrambleKey("w0w"); | ||
*/ | ||
getAddress(path, boolDisplay, boolChaincode) { | ||
let paths = (0, _utils.splitPath)(path); | ||
let buffer = new Buffer(5 + 1 + paths.length * 4); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x02; | ||
buffer[2] = boolDisplay ? 0x01 : 0x00; | ||
buffer[3] = boolChaincode ? 0x01 : 0x00; | ||
buffer[4] = 1 + paths.length * 4; | ||
buffer[5] = paths.length; | ||
paths.forEach((element, index) => { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
return this.transport.exchange(buffer.toString("hex"), [0x9000]).then(responseHex => { | ||
let result = {}; | ||
let response = new Buffer(responseHex, "hex"); | ||
let publicKeyLength = response[0]; | ||
let addressLength = response[1 + publicKeyLength]; | ||
result.publicKey = response.slice(1, 1 + publicKeyLength).toString("hex"); | ||
result.address = "0x" + response.slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength).toString("ascii"); | ||
if (boolChaincode) { | ||
result.chainCode = response.slice(1 + publicKeyLength + 1 + addressLength, 1 + publicKeyLength + 1 + addressLength + 32).toString("hex"); | ||
} | ||
return result; | ||
}); | ||
} | ||
_createClass(Eth, [{ | ||
key: "getAddress", | ||
value: function getAddress(path, boolDisplay, boolChaincode) { | ||
var paths = (0, _utils.splitPath)(path); | ||
var buffer = new Buffer(5 + 1 + paths.length * 4); | ||
/** | ||
* You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign | ||
* @example | ||
eth.signTransaction("44'/60'/0'/0'/0", "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080").then(result => ...) | ||
*/ | ||
signTransaction(path, rawTxHex) { | ||
let paths = (0, _utils.splitPath)(path); | ||
let offset = 0; | ||
let rawTx = new Buffer(rawTxHex, "hex"); | ||
let apdus = []; | ||
let response = []; | ||
while (offset !== rawTx.length) { | ||
let maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 : 150; | ||
let chunkSize = offset + maxChunkSize > rawTx.length ? rawTx.length - offset : maxChunkSize; | ||
let buffer = new Buffer(offset === 0 ? 5 + 1 + paths.length * 4 + chunkSize : 5 + chunkSize); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x02; | ||
buffer[2] = boolDisplay ? 0x01 : 0x00; | ||
buffer[3] = boolChaincode ? 0x01 : 0x00; | ||
buffer[4] = 1 + paths.length * 4; | ||
buffer[5] = paths.length; | ||
paths.forEach(function (element, index) { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
return this.transport.exchange(buffer.toString("hex"), [0x9000]).then(function (responseHex) { | ||
var result = {}; | ||
var response = new Buffer(responseHex, "hex"); | ||
var publicKeyLength = response[0]; | ||
var addressLength = response[1 + publicKeyLength]; | ||
result.publicKey = response.slice(1, 1 + publicKeyLength).toString("hex"); | ||
result.address = "0x" + response.slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength).toString("ascii"); | ||
if (boolChaincode) { | ||
result.chainCode = response.slice(1 + publicKeyLength + 1 + addressLength, 1 + publicKeyLength + 1 + addressLength + 32).toString("hex"); | ||
} | ||
return result; | ||
}); | ||
} | ||
/** | ||
* You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign | ||
* @example | ||
eth.signTransaction("44'/60'/0'/0'/0", "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080").then(result => ...) | ||
*/ | ||
}, { | ||
key: "signTransaction", | ||
value: function signTransaction(path, rawTxHex) { | ||
var _this = this; | ||
var paths = (0, _utils.splitPath)(path); | ||
var offset = 0; | ||
var rawTx = new Buffer(rawTxHex, "hex"); | ||
var apdus = []; | ||
var response = []; | ||
var _loop = function _loop() { | ||
var maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 : 150; | ||
var chunkSize = offset + maxChunkSize > rawTx.length ? rawTx.length - offset : maxChunkSize; | ||
var buffer = new Buffer(offset === 0 ? 5 + 1 + paths.length * 4 + chunkSize : 5 + chunkSize); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x04; | ||
buffer[2] = offset === 0 ? 0x00 : 0x80; | ||
buffer[3] = 0x00; | ||
buffer[4] = offset === 0 ? 1 + paths.length * 4 + chunkSize : chunkSize; | ||
if (offset === 0) { | ||
buffer[5] = paths.length; | ||
paths.forEach(function (element, index) { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
rawTx.copy(buffer, 6 + 4 * paths.length, offset, offset + chunkSize); | ||
} else { | ||
rawTx.copy(buffer, 5, offset, offset + chunkSize); | ||
} | ||
apdus.push(buffer.toString("hex")); | ||
offset += chunkSize; | ||
}; | ||
while (offset !== rawTx.length) { | ||
_loop(); | ||
buffer[1] = 0x04; | ||
buffer[2] = offset === 0 ? 0x00 : 0x80; | ||
buffer[3] = 0x00; | ||
buffer[4] = offset === 0 ? 1 + paths.length * 4 + chunkSize : chunkSize; | ||
if (offset === 0) { | ||
buffer[5] = paths.length; | ||
paths.forEach((element, index) => { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
rawTx.copy(buffer, 6 + 4 * paths.length, offset, offset + chunkSize); | ||
} else { | ||
rawTx.copy(buffer, 5, offset, offset + chunkSize); | ||
} | ||
return (0, _utils.foreach)(apdus, function (apdu) { | ||
return _this.transport.exchange(apdu, [0x9000]).then(function (apduResponse) { | ||
response = apduResponse; | ||
}); | ||
}).then(function () { | ||
response = new Buffer(response, "hex"); | ||
var v = response.slice(0, 1).toString("hex"); | ||
var r = response.slice(1, 1 + 32).toString("hex"); | ||
var s = response.slice(1 + 32, 1 + 32 + 32).toString("hex"); | ||
return { v: v, r: r, s: s }; | ||
}); | ||
apdus.push(buffer.toString("hex")); | ||
offset += chunkSize; | ||
} | ||
return (0, _utils.foreach)(apdus, apdu => this.transport.exchange(apdu, [0x9000]).then(apduResponse => { | ||
response = apduResponse; | ||
})).then(() => { | ||
response = new Buffer(response, "hex"); | ||
const v = response.slice(0, 1).toString("hex"); | ||
const r = response.slice(1, 1 + 32).toString("hex"); | ||
const s = response.slice(1 + 32, 1 + 32 + 32).toString("hex"); | ||
return { v, r, s }; | ||
}); | ||
} | ||
/** | ||
*/ | ||
/** | ||
*/ | ||
getAppConfiguration() { | ||
let buffer = new Buffer(5); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x06; | ||
buffer[2] = 0x00; | ||
buffer[3] = 0x00; | ||
buffer[4] = 0x00; | ||
return this.transport.exchange(buffer.toString("hex"), [0x9000]).then(responseHex => { | ||
let result = {}; | ||
let response = Buffer.from(responseHex, "hex"); | ||
result.arbitraryDataEnabled = response[0] & 0x01; | ||
result.version = "" + response[1] + "." + response[2] + "." + response[3]; | ||
return result; | ||
}); | ||
} | ||
}, { | ||
key: "getAppConfiguration", | ||
value: function getAppConfiguration() { | ||
var buffer = new Buffer(5); | ||
/** | ||
* You can sign a message according to eth_sign RPC call and retrieve v, r, s given the message and the BIP 32 path of the account to sign. | ||
* @example | ||
eth.signPersonalMessage("44'/60'/0'/0'/0", Buffer.from("test").toString("hex")).then(result => { | ||
var v = result['v'] - 27; | ||
v = v.toString(16); | ||
if (v.length < 2) { | ||
v = "0" + v; | ||
} | ||
console.log("Signature 0x" + result['r'] + result['s'] + v); | ||
}) | ||
*/ | ||
signPersonalMessage(path, messageHex) { | ||
let paths = (0, _utils.splitPath)(path); | ||
let offset = 0; | ||
let message = new Buffer(messageHex, "hex"); | ||
let apdus = []; | ||
let response = []; | ||
while (offset !== message.length) { | ||
let maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150; | ||
let chunkSize = offset + maxChunkSize > message.length ? message.length - offset : maxChunkSize; | ||
let buffer = new Buffer(offset === 0 ? 5 + 1 + paths.length * 4 + 4 + chunkSize : 5 + chunkSize); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x06; | ||
buffer[2] = 0x00; | ||
buffer[1] = 0x08; | ||
buffer[2] = offset === 0 ? 0x00 : 0x80; | ||
buffer[3] = 0x00; | ||
buffer[4] = 0x00; | ||
return this.transport.exchange(buffer.toString("hex"), [0x9000]).then(function (responseHex) { | ||
var result = {}; | ||
var response = Buffer.from(responseHex, "hex"); | ||
result.arbitraryDataEnabled = response[0] & 0x01; | ||
result.version = "" + response[1] + "." + response[2] + "." + response[3]; | ||
return result; | ||
}); | ||
} | ||
/** | ||
* You can sign a message according to eth_sign RPC call and retrieve v, r, s given the message and the BIP 32 path of the account to sign. | ||
* @example | ||
eth.signPersonalMessage("44'/60'/0'/0'/0", Buffer.from("test").toString("hex")).then(result => { | ||
var v = result['v'] - 27; | ||
v = v.toString(16); | ||
if (v.length < 2) { | ||
v = "0" + v; | ||
} | ||
console.log("Signature 0x" + result['r'] + result['s'] + v); | ||
}) | ||
*/ | ||
}, { | ||
key: "signPersonalMessage", | ||
value: function signPersonalMessage(path, messageHex) { | ||
var _this2 = this; | ||
var paths = (0, _utils.splitPath)(path); | ||
var offset = 0; | ||
var message = new Buffer(messageHex, "hex"); | ||
var apdus = []; | ||
var response = []; | ||
var _loop2 = function _loop2() { | ||
var maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150; | ||
var chunkSize = offset + maxChunkSize > message.length ? message.length - offset : maxChunkSize; | ||
var buffer = new Buffer(offset === 0 ? 5 + 1 + paths.length * 4 + 4 + chunkSize : 5 + chunkSize); | ||
buffer[0] = 0xe0; | ||
buffer[1] = 0x08; | ||
buffer[2] = offset === 0 ? 0x00 : 0x80; | ||
buffer[3] = 0x00; | ||
buffer[4] = offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize; | ||
if (offset === 0) { | ||
buffer[5] = paths.length; | ||
paths.forEach(function (element, index) { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
buffer.writeUInt32BE(message.length, 6 + 4 * paths.length); | ||
message.copy(buffer, 6 + 4 * paths.length + 4, offset, offset + chunkSize); | ||
} else { | ||
message.copy(buffer, 5, offset, offset + chunkSize); | ||
} | ||
apdus.push(buffer.toString("hex")); | ||
offset += chunkSize; | ||
}; | ||
while (offset !== message.length) { | ||
_loop2(); | ||
buffer[4] = offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize; | ||
if (offset === 0) { | ||
buffer[5] = paths.length; | ||
paths.forEach((element, index) => { | ||
buffer.writeUInt32BE(element, 6 + 4 * index); | ||
}); | ||
buffer.writeUInt32BE(message.length, 6 + 4 * paths.length); | ||
message.copy(buffer, 6 + 4 * paths.length + 4, offset, offset + chunkSize); | ||
} else { | ||
message.copy(buffer, 5, offset, offset + chunkSize); | ||
} | ||
return (0, _utils.foreach)(apdus, function (apdu) { | ||
return _this2.transport.exchange(apdu, [0x9000]).then(function (apduResponse) { | ||
response = apduResponse; | ||
}); | ||
}).then(function () { | ||
response = new Buffer(response, "hex"); | ||
var v = response[0]; | ||
var r = response.slice(1, 1 + 32).toString("hex"); | ||
var s = response.slice(1 + 32, 1 + 32 + 32).toString("hex"); | ||
return { v: v, r: r, s: s }; | ||
}); | ||
apdus.push(buffer.toString("hex")); | ||
offset += chunkSize; | ||
} | ||
}]); | ||
return (0, _utils.foreach)(apdus, apdu => this.transport.exchange(apdu, [0x9000]).then(apduResponse => { | ||
response = apduResponse; | ||
})).then(() => { | ||
response = new Buffer(response, "hex"); | ||
const v = response[0]; | ||
const r = response.slice(1, 1 + 32).toString("hex"); | ||
const s = response.slice(1 + 32, 1 + 32 + 32).toString("hex"); | ||
return { v, r, s }; | ||
}); | ||
} | ||
} | ||
exports.default = Eth; /******************************************************************************** | ||
* Ledger Node JS API | ||
* (c) 2016-2017 Ledger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
return Eth; | ||
}(); | ||
exports.default = Eth; | ||
// FIXME drop: | ||
//# sourceMappingURL=Eth.js.map |
@@ -13,5 +13,4 @@ "use strict"; | ||
function defer() { | ||
var resolve = void 0, | ||
reject = void 0; | ||
var promise = new Promise(function (success, failure) { | ||
let resolve, reject; | ||
let promise = new Promise(function (success, failure) { | ||
resolve = success; | ||
@@ -21,3 +20,3 @@ reject = failure; | ||
if (!resolve || !reject) throw "defer() error"; // this never happens and is just to make flow happy | ||
return { promise: promise, resolve: resolve, reject: reject }; | ||
return { promise, resolve, reject }; | ||
} | ||
@@ -45,6 +44,6 @@ | ||
function splitPath(path) { | ||
var result = []; | ||
var components = path.split("/"); | ||
components.forEach(function (element) { | ||
var number = parseInt(element, 10); | ||
let result = []; | ||
let components = path.split("/"); | ||
components.forEach(element => { | ||
let number = parseInt(element, 10); | ||
if (isNaN(number)) { | ||
@@ -64,7 +63,3 @@ return; // FIXME shouldn't it throws instead? | ||
function eachSeries(arr, fun) { | ||
return arr.reduce(function (p, e) { | ||
return p.then(function () { | ||
return fun(e); | ||
}); | ||
}, Promise.resolve()); | ||
return arr.reduce((p, e) => p.then(() => fun(e)), Promise.resolve()); | ||
} | ||
@@ -81,9 +76,7 @@ | ||
} | ||
return Promise.resolve().then(function () { | ||
return iterate(0, arr, []); | ||
}); | ||
return Promise.resolve().then(() => iterate(0, arr, [])); | ||
} | ||
function doIf(condition, callback) { | ||
return Promise.resolve().then(function () { | ||
return Promise.resolve().then(() => { | ||
if (condition) { | ||
@@ -100,3 +93,3 @@ return callback(); | ||
} else { | ||
return callback().then(function (res) { | ||
return callback().then(res => { | ||
result.push(res); | ||
@@ -103,0 +96,0 @@ return iterate(result); |
{ | ||
"name": "@ledgerhq/hw-app-eth", | ||
"version": "1.1.1-beta.068e2a14", | ||
"version": "1.1.2-beta.068e2a14", | ||
"description": "Ledger Hardware Wallet Ethereum Application API", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@ledgerhq/hw-transport": "^1.1.1-beta.068e2a14" | ||
"@ledgerhq/hw-transport": "^1.1.2-beta.068e2a14" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60749
820
+ Added@ledgerhq/hw-transport@1.1.2-beta.068e2a14(transitive)
- Removed@ledgerhq/hw-transport@1.1.1-beta.068e2a14(transitive)