@ledgerhq/hw-app-btc
Advanced tools
Comparing version
@@ -44,2 +44,3 @@ "use strict"; | ||
var MAX_SCRIPT_BLOCK = 50; | ||
var DEFAULT_VERSION = 1; | ||
var DEFAULT_LOCKTIME = 0; | ||
@@ -301,10 +302,10 @@ var DEFAULT_SEQUENCE = 0xffffffff; | ||
var prefix = void 0; | ||
if (inputs[i].trustedInput) { | ||
if (bip143) { | ||
prefix = Buffer.from([0x02]); | ||
if (bip143) { | ||
prefix = Buffer.from([0x02]); | ||
} else { | ||
if (inputs[i].trustedInput) { | ||
prefix = Buffer.from([0x01, inputs[i].value.length]); | ||
} else { | ||
prefix = Buffer.from([0x01, inputs[i].value.length]); | ||
prefix = Buffer.from([0x00]); | ||
} | ||
} else { | ||
prefix = Buffer.from([0x00]); | ||
} | ||
@@ -685,7 +686,10 @@ data = Buffer.concat([prefix, inputs[i].value, _this2.createVarint(input.script.length)]); | ||
value: function signP2SHTransaction(inputs, associatedKeysets, outputScriptHex) { | ||
var _this6 = this; | ||
var lockTime = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : DEFAULT_LOCKTIME; | ||
var sigHashType = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : SIGHASH_ALL; | ||
var _this6 = this; | ||
var segwit = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false; | ||
var transactionVersion = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : DEFAULT_VERSION; | ||
// Inputs are provided as arrays of [transaction, output_index, redeem script, optional sequence] | ||
@@ -696,3 +700,3 @@ // associatedKeysets are provided as arrays of [path] | ||
var defaultVersion = Buffer.alloc(4); | ||
defaultVersion.writeUInt32LE(1, 0); | ||
defaultVersion.writeUInt32LE(transactionVersion, 0); | ||
var trustedInputs = []; | ||
@@ -708,2 +712,3 @@ var regularOutputs = []; | ||
var getTrustedInputCall = segwit ? this.getTrustedInputBIP143.bind(this) : this.getTrustedInput.bind(this); | ||
var outputScript = Buffer.from(outputScriptHex, "hex"); | ||
@@ -713,7 +718,10 @@ | ||
return (0, _utils.doIf)(!resuming, function () { | ||
return _this6.getTrustedInput(input[1], input[0]).then(function (trustedInput) { | ||
var inputItem = {}; | ||
inputItem.trustedInput = false; | ||
inputItem.value = Buffer.from(trustedInput, "hex").slice(4, 4 + 0x24); | ||
trustedInputs.push(inputItem); | ||
return getTrustedInputCall(input[1], input[0]).then(function (trustedInput) { | ||
var sequence = Buffer.alloc(4); | ||
sequence.writeUInt32LE(input.length >= 4 && typeof input[3] === "number" ? input[3] : DEFAULT_SEQUENCE, 0); | ||
trustedInputs.push({ | ||
trustedInput: false, | ||
value: segwit ? Buffer.from(trustedInput, "hex") : Buffer.from(trustedInput, "hex").slice(4, 4 + 0x24), | ||
sequence: sequence | ||
}); | ||
}); | ||
@@ -731,24 +739,36 @@ }).then(function () { | ||
for (var i = 0; i < inputs.length; i++) { | ||
var tmp = Buffer.alloc(4); | ||
var _sequence2 = void 0; | ||
if (inputs[i].length >= 4 && typeof inputs[i][3] === "number") { | ||
_sequence2 = inputs[i][3]; | ||
} else { | ||
_sequence2 = DEFAULT_SEQUENCE; | ||
} | ||
tmp.writeUInt32LE(_sequence2, 0); | ||
var _sequence2 = Buffer.alloc(4); | ||
_sequence2.writeUInt32LE(inputs[i].length >= 4 && typeof inputs[i][3] === "number" ? inputs[i][3] : DEFAULT_SEQUENCE, 0); | ||
targetTransaction.inputs.push({ | ||
script: nullScript, | ||
prevout: nullPrevout, | ||
script: nullScript, | ||
sequence: tmp | ||
sequence: _sequence2 | ||
}); | ||
} | ||
}).then(function () { | ||
return (0, _utils.doIf)(segwit, function () { | ||
return ( | ||
// Do the first run with all inputs | ||
_this6.startUntrustedHashTransactionInput(true, targetTransaction, trustedInputs, true).then(function () { | ||
return _this6.hashOutputFull(outputScript); | ||
}) | ||
); | ||
}); | ||
}).then(function () { | ||
return (0, _utils.foreach)(inputs, function (input, i) { | ||
targetTransaction.inputs[i].script = inputs[i].length >= 3 && typeof inputs[i][2] === "string" ? Buffer.from(inputs[i][2], "hex") : regularOutputs[i].script; | ||
return _this6.startUntrustedHashTransactionInput(firstRun, targetTransaction, trustedInputs, false).then(function () { | ||
return _this6.hashOutputFull(outputScript); | ||
var script = inputs[i].length >= 3 && typeof inputs[i][2] === "string" ? Buffer.from(inputs[i][2], "hex") : regularOutputs[i].script; | ||
var pseudoTX = (0, _assign2.default)({}, targetTransaction); | ||
var pseudoTrustedInputs = segwit ? [trustedInputs[i]] : trustedInputs; | ||
if (segwit) { | ||
pseudoTX.inputs = [(0, _extends3.default)({}, pseudoTX.inputs[i], { script: script })]; | ||
} else { | ||
pseudoTX.inputs[i].script = script; | ||
} | ||
return _this6.startUntrustedHashTransactionInput(!segwit && firstRun, pseudoTX, pseudoTrustedInputs, segwit).then(function () { | ||
return (0, _utils.doIf)(!segwit, function () { | ||
return _this6.hashOutputFull(outputScript); | ||
}); | ||
}).then(function () { | ||
return _this6.signTransaction(associatedKeysets[i], lockTime, sigHashType).then(function (signature) { | ||
signatures.push(signature.slice(0, signature.length - 1).toString("hex")); | ||
signatures.push(segwit ? signature.toString("hex") : signature.slice(0, signature.length - 1).toString("hex")); | ||
targetTransaction.inputs[i].script = nullScript; | ||
@@ -866,8 +886,8 @@ if (firstRun) { | ||
} | ||
offset += 4; | ||
if (overwinter) { | ||
offset += 4; | ||
nExpiryHeight = transaction.slice(offset, offset + 4); | ||
offset += 4; | ||
extraData = transaction.slice(offset); | ||
} | ||
extraData = transaction.slice(offset); | ||
return { | ||
@@ -874,0 +894,0 @@ version: version, |
{ | ||
"name": "@ledgerhq/hw-app-btc", | ||
"version": "4.20.0", | ||
"version": "4.21.0-beta.12bfb9a2", | ||
"description": "Ledger Hardware Wallet Bitcoin Application API", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@ledgerhq/hw-transport": "^4.19.0", | ||
"@ledgerhq/hw-transport": "^4.21.0-beta.12bfb9a2", | ||
"create-hash": "^1.1.3" | ||
@@ -38,6 +38,6 @@ }, | ||
"flow": "flow", | ||
"clean": "rm -rf lib/", | ||
"build": "cd ../.. && export PATH=$(yarn bin):$PATH && cd - && babel --source-maps -d lib src && flow-copy-source -v src lib", | ||
"watch": "cd ../.. && export PATH=$(yarn bin):$PATH && cd - && babel --watch --source-maps -d lib src & flow-copy-source -w -v src lib" | ||
"clean": "bash ../../script/clean.sh", | ||
"build": "bash ../../script/build.sh", | ||
"watch": "bash ../../script/watch.sh" | ||
} | ||
} |
103
src/Btc.js
@@ -12,2 +12,3 @@ //@flow | ||
const MAX_SCRIPT_BLOCK = 50; | ||
const DEFAULT_VERSION = 1; | ||
const DEFAULT_LOCKTIME = 0; | ||
@@ -294,10 +295,10 @@ const DEFAULT_SEQUENCE = 0xffffffff; | ||
let prefix; | ||
if (inputs[i].trustedInput) { | ||
if (bip143) { | ||
prefix = Buffer.from([0x02]); | ||
if (bip143) { | ||
prefix = Buffer.from([0x02]); | ||
} else { | ||
if (inputs[i].trustedInput) { | ||
prefix = Buffer.from([0x01, inputs[i].value.length]); | ||
} else { | ||
prefix = Buffer.from([0x01, inputs[i].value.length]); | ||
prefix = Buffer.from([0x00]); | ||
} | ||
} else { | ||
prefix = Buffer.from([0x00]); | ||
} | ||
@@ -806,3 +807,5 @@ data = Buffer.concat([ | ||
lockTime?: number = DEFAULT_LOCKTIME, | ||
sigHashType?: number = SIGHASH_ALL | ||
sigHashType?: number = SIGHASH_ALL, | ||
segwit?: boolean = false, | ||
transactionVersion?: number = DEFAULT_VERSION | ||
) { | ||
@@ -814,3 +817,3 @@ // Inputs are provided as arrays of [transaction, output_index, redeem script, optional sequence] | ||
const defaultVersion = Buffer.alloc(4); | ||
defaultVersion.writeUInt32LE(1, 0); | ||
defaultVersion.writeUInt32LE(transactionVersion, 0); | ||
const trustedInputs = []; | ||
@@ -820,3 +823,3 @@ const regularOutputs: Array<TransactionOutput> = []; | ||
let firstRun = true; | ||
let resuming = false; | ||
const resuming = false; | ||
let targetTransaction: Transaction = { | ||
@@ -827,2 +830,5 @@ inputs: [], | ||
const getTrustedInputCall = segwit | ||
? this.getTrustedInputBIP143.bind(this) | ||
: this.getTrustedInput.bind(this); | ||
const outputScript = Buffer.from(outputScriptHex, "hex"); | ||
@@ -832,7 +838,17 @@ | ||
doIf(!resuming, () => | ||
this.getTrustedInput(input[1], input[0]).then(trustedInput => { | ||
let inputItem = {}; | ||
inputItem.trustedInput = false; | ||
inputItem.value = Buffer.from(trustedInput, "hex").slice(4, 4 + 0x24); | ||
trustedInputs.push(inputItem); | ||
getTrustedInputCall(input[1], input[0]).then(trustedInput => { | ||
let sequence = Buffer.alloc(4); | ||
sequence.writeUInt32LE( | ||
input.length >= 4 && typeof input[3] === "number" | ||
? input[3] | ||
: DEFAULT_SEQUENCE, | ||
0 | ||
); | ||
trustedInputs.push({ | ||
trustedInput: false, | ||
value: segwit | ||
? Buffer.from(trustedInput, "hex") | ||
: Buffer.from(trustedInput, "hex").slice(4, 4 + 0x24), | ||
sequence | ||
}); | ||
}) | ||
@@ -850,14 +866,13 @@ ).then(() => { | ||
for (let i = 0; i < inputs.length; i++) { | ||
let tmp = Buffer.alloc(4); | ||
let sequence; | ||
if (inputs[i].length >= 4 && typeof inputs[i][3] === "number") { | ||
sequence = inputs[i][3]; | ||
} else { | ||
sequence = DEFAULT_SEQUENCE; | ||
} | ||
tmp.writeUInt32LE(sequence, 0); | ||
let sequence = Buffer.alloc(4); | ||
sequence.writeUInt32LE( | ||
inputs[i].length >= 4 && typeof inputs[i][3] === "number" | ||
? inputs[i][3] | ||
: DEFAULT_SEQUENCE, | ||
0 | ||
); | ||
targetTransaction.inputs.push({ | ||
script: nullScript, | ||
prevout: nullPrevout, | ||
script: nullScript, | ||
sequence: tmp | ||
sequence | ||
}); | ||
@@ -867,14 +882,32 @@ } | ||
.then(() => | ||
doIf(segwit, () => | ||
// Do the first run with all inputs | ||
this.startUntrustedHashTransactionInput( | ||
true, | ||
targetTransaction, | ||
trustedInputs, | ||
true | ||
).then(() => this.hashOutputFull(outputScript)) | ||
) | ||
) | ||
.then(() => | ||
foreach(inputs, (input, i) => { | ||
targetTransaction.inputs[i].script = | ||
let script = | ||
inputs[i].length >= 3 && typeof inputs[i][2] === "string" | ||
? Buffer.from(inputs[i][2], "hex") | ||
: regularOutputs[i].script; | ||
let pseudoTX = Object.assign({}, targetTransaction); | ||
let pseudoTrustedInputs = segwit ? [trustedInputs[i]] : trustedInputs; | ||
if (segwit) { | ||
pseudoTX.inputs = [{ ...pseudoTX.inputs[i], script }]; | ||
} else { | ||
pseudoTX.inputs[i].script = script; | ||
} | ||
return this.startUntrustedHashTransactionInput( | ||
firstRun, | ||
targetTransaction, | ||
trustedInputs, | ||
false | ||
!segwit && firstRun, | ||
pseudoTX, | ||
pseudoTrustedInputs, | ||
segwit | ||
) | ||
.then(() => this.hashOutputFull(outputScript)) | ||
.then(() => doIf(!segwit, () => this.hashOutputFull(outputScript))) | ||
.then(() => | ||
@@ -887,3 +920,5 @@ this.signTransaction( | ||
signatures.push( | ||
signature.slice(0, signature.length - 1).toString("hex") | ||
segwit | ||
? signature.toString("hex") | ||
: signature.slice(0, signature.length - 1).toString("hex") | ||
); | ||
@@ -1001,8 +1036,8 @@ targetTransaction.inputs[i].script = nullScript; | ||
} | ||
offset += 4; | ||
if (overwinter) { | ||
offset += 4; | ||
nExpiryHeight = transaction.slice(offset, offset + 4); | ||
offset += 4; | ||
extraData = transaction.slice(offset); | ||
offset += 4; | ||
} | ||
extraData = transaction.slice(offset); | ||
return { | ||
@@ -1009,0 +1044,0 @@ version, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
206827
-1.64%11
-26.67%2211
-8.64%1
Infinity%