@ledgerhq/hw-app-btc 6.24.1 → 6.25.1-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib-es/Btc.js.flow +280 -0
- package/lib-es/bip32.js.flow +13 -0
- package/lib-es/compressPublicKey.js.flow +8 -0
- package/lib-es/constants.js.flow +13 -0
- package/lib-es/createTransaction.js.flow +419 -0
- package/lib-es/debug.js.flow +41 -0
- package/lib-es/finalizeInput.js.flow +38 -0
- package/lib-es/getAppAndVersion.js.flow +19 -0
- package/lib-es/getTrustedInput.js.flow +163 -0
- package/lib-es/getTrustedInputBIP143.js.flow +37 -0
- package/lib-es/getWalletPublicKey.js.flow +51 -0
- package/lib-es/hashPublicKey.js.flow +8 -0
- package/lib-es/serializeTransaction.js.flow +77 -0
- package/lib-es/shouldUseTrustedInputForSegwit.js.flow +15 -0
- package/lib-es/signMessage.js.flow +67 -0
- package/lib-es/signP2SHTransaction.js.flow +170 -0
- package/lib-es/signTransaction.js.flow +40 -0
- package/lib-es/splitTransaction.js.flow +145 -0
- package/lib-es/startUntrustedHashTransactionInput.js.flow +136 -0
- package/lib-es/types.js.flow +31 -0
- package/lib-es/varint.js.flow +43 -0
- package/package.json +3 -3
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
//@flow
|
|
2
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
3
|
+
import { signMessage } from "./signMessage";
|
|
4
|
+
import { getWalletPublicKey } from "./getWalletPublicKey";
|
|
5
|
+
import type { AddressFormat } from "./getWalletPublicKey";
|
|
6
|
+
import { splitTransaction } from "./splitTransaction";
|
|
7
|
+
import { getTrustedInput } from "./getTrustedInput";
|
|
8
|
+
import { getTrustedInputBIP143 } from "./getTrustedInputBIP143";
|
|
9
|
+
import type { Transaction } from "./types";
|
|
10
|
+
import { createTransaction } from "./createTransaction";
|
|
11
|
+
import type { CreateTransactionArg } from "./createTransaction";
|
|
12
|
+
import { signP2SHTransaction } from "./signP2SHTransaction";
|
|
13
|
+
import type { SignP2SHTransactionArg } from "./signP2SHTransaction";
|
|
14
|
+
import { serializeTransactionOutputs } from "./serializeTransaction";
|
|
15
|
+
|
|
16
|
+
export type { AddressFormat };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Bitcoin API.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* import Btc from "@ledgerhq/hw-app-btc";
|
|
23
|
+
* const btc = new Btc(transport)
|
|
24
|
+
*/
|
|
25
|
+
export default class Btc {
|
|
26
|
+
transport: Transport<*>;
|
|
27
|
+
|
|
28
|
+
constructor(transport: Transport<*>, scrambleKey: string = "BTC") {
|
|
29
|
+
this.transport = transport;
|
|
30
|
+
transport.decorateAppAPIMethods(
|
|
31
|
+
this,
|
|
32
|
+
[
|
|
33
|
+
"getWalletPublicKey",
|
|
34
|
+
"signP2SHTransaction",
|
|
35
|
+
"signMessageNew",
|
|
36
|
+
"createPaymentTransactionNew",
|
|
37
|
+
"getTrustedInput",
|
|
38
|
+
"getTrustedInputBIP143",
|
|
39
|
+
],
|
|
40
|
+
scrambleKey
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param path a BIP 32 path
|
|
46
|
+
* @param options an object with optional these fields:
|
|
47
|
+
*
|
|
48
|
+
* - verify (boolean) will ask user to confirm the address on the device
|
|
49
|
+
*
|
|
50
|
+
* - format ("legacy" | "p2sh" | "bech32" | "cashaddr") to use different bitcoin address formatter.
|
|
51
|
+
*
|
|
52
|
+
* NB The normal usage is to use:
|
|
53
|
+
*
|
|
54
|
+
* - legacy format with 44' paths
|
|
55
|
+
*
|
|
56
|
+
* - p2sh format with 49' paths
|
|
57
|
+
*
|
|
58
|
+
* - bech32 format with 173' paths
|
|
59
|
+
*
|
|
60
|
+
* - cashaddr in case of Bitcoin Cash
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* btc.getWalletPublicKey("44'/0'/0'/0/0").then(o => o.bitcoinAddress)
|
|
64
|
+
* btc.getWalletPublicKey("49'/0'/0'/0/0", { format: "p2sh" }).then(o => o.bitcoinAddress)
|
|
65
|
+
*/
|
|
66
|
+
getWalletPublicKey(
|
|
67
|
+
path: string,
|
|
68
|
+
opts?: boolean | { verify?: boolean, format?: AddressFormat }
|
|
69
|
+
): Promise<{
|
|
70
|
+
publicKey: string,
|
|
71
|
+
bitcoinAddress: string,
|
|
72
|
+
chainCode: string,
|
|
73
|
+
}> {
|
|
74
|
+
let options;
|
|
75
|
+
if (arguments.length > 2 || typeof opts === "boolean") {
|
|
76
|
+
console.warn(
|
|
77
|
+
"btc.getWalletPublicKey deprecated signature used. Please switch to getWalletPublicKey(path, { format, verify })"
|
|
78
|
+
);
|
|
79
|
+
options = {
|
|
80
|
+
verify: !!opts,
|
|
81
|
+
format: arguments[2] ? "p2sh" : "legacy",
|
|
82
|
+
};
|
|
83
|
+
} else {
|
|
84
|
+
options = opts || {};
|
|
85
|
+
}
|
|
86
|
+
return getWalletPublicKey(this.transport, { ...options, path });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* You can sign a message according to the Bitcoin Signature format and retrieve v, r, s given the message and the BIP 32 path of the account to sign.
|
|
91
|
+
* @example
|
|
92
|
+
btc.signMessageNew_async("44'/60'/0'/0'/0", Buffer.from("test").toString("hex")).then(function(result) {
|
|
93
|
+
var v = result['v'] + 27 + 4;
|
|
94
|
+
var signature = Buffer.from(v.toString(16) + result['r'] + result['s'], 'hex').toString('base64');
|
|
95
|
+
console.log("Signature : " + signature);
|
|
96
|
+
}).catch(function(ex) {console.log(ex);});
|
|
97
|
+
*/
|
|
98
|
+
signMessageNew(
|
|
99
|
+
path: string,
|
|
100
|
+
messageHex: string
|
|
101
|
+
): Promise<{ v: number, r: string, s: string }> {
|
|
102
|
+
return signMessage(this.transport, { path, messageHex });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* To sign a transaction involving standard (P2PKH) inputs, call createTransaction with the following parameters
|
|
107
|
+
* @param inputs is an array of [ transaction, output_index, optional redeem script, optional sequence ] where
|
|
108
|
+
*
|
|
109
|
+
* * transaction is the previously computed transaction object for this UTXO
|
|
110
|
+
* * output_index is the output in the transaction used as input for this UTXO (counting from 0)
|
|
111
|
+
* * redeem script is the optional redeem script to use when consuming a Segregated Witness input
|
|
112
|
+
* * sequence is the sequence number to use for this input (when using RBF), or non present
|
|
113
|
+
* @param associatedKeysets is an array of BIP 32 paths pointing to the path to the private key used for each UTXO
|
|
114
|
+
* @param changePath is an optional BIP 32 path pointing to the path to the public key used to compute the change address
|
|
115
|
+
* @param outputScriptHex is the hexadecimal serialized outputs of the transaction to sign
|
|
116
|
+
* @param lockTime is the optional lockTime of the transaction to sign, or default (0)
|
|
117
|
+
* @param sigHashType is the hash type of the transaction to sign, or default (all)
|
|
118
|
+
* @param segwit is an optional boolean indicating wether to use segwit or not
|
|
119
|
+
* @param initialTimestamp is an optional timestamp of the function call to use for coins that necessitate timestamps only, (not the one that the tx will include)
|
|
120
|
+
* @param additionals list of additionnal options
|
|
121
|
+
*
|
|
122
|
+
* - "bech32" for spending native segwit outputs
|
|
123
|
+
* - "abc" for bch
|
|
124
|
+
* - "gold" for btg
|
|
125
|
+
* - "bipxxx" for using BIPxxx
|
|
126
|
+
* - "sapling" to indicate a zec transaction is supporting sapling (to be set over block 419200)
|
|
127
|
+
* @param expiryHeight is an optional Buffer for zec overwinter / sapling Txs
|
|
128
|
+
* @param useTrustedInputForSegwit trust inputs for segwit transactions
|
|
129
|
+
* @return the signed transaction ready to be broadcast
|
|
130
|
+
* @example
|
|
131
|
+
btc.createTransaction({
|
|
132
|
+
inputs: [ [tx1, 1] ],
|
|
133
|
+
associatedKeysets: ["0'/0/0"],
|
|
134
|
+
outputScriptHex: "01905f0100000000001976a91472a5d75c8d2d0565b656a5232703b167d50d5a2b88ac"
|
|
135
|
+
}).then(res => ...);
|
|
136
|
+
*/
|
|
137
|
+
createPaymentTransactionNew(arg: CreateTransactionArg) {
|
|
138
|
+
if (arguments.length > 1) {
|
|
139
|
+
console.warn(
|
|
140
|
+
"@ledgerhq/hw-app-btc: createPaymentTransactionNew multi argument signature is deprecated. please switch to named parameters."
|
|
141
|
+
);
|
|
142
|
+
arg = fromDeprecateArguments(arguments, [
|
|
143
|
+
"inputs",
|
|
144
|
+
"associatedKeysets",
|
|
145
|
+
"changePath",
|
|
146
|
+
"outputScriptHex",
|
|
147
|
+
"lockTime",
|
|
148
|
+
"sigHashType",
|
|
149
|
+
"segwit",
|
|
150
|
+
"initialTimestamp",
|
|
151
|
+
"additionals",
|
|
152
|
+
"expiryHeight",
|
|
153
|
+
"useTrustedInputForSegwit",
|
|
154
|
+
]);
|
|
155
|
+
}
|
|
156
|
+
return createTransaction(this.transport, arg);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* To obtain the signature of multisignature (P2SH) inputs, call signP2SHTransaction_async with the folowing parameters
|
|
161
|
+
* @param inputs is an array of [ transaction, output_index, redeem script, optional sequence ] where
|
|
162
|
+
* * transaction is the previously computed transaction object for this UTXO
|
|
163
|
+
* * output_index is the output in the transaction used as input for this UTXO (counting from 0)
|
|
164
|
+
* * redeem script is the mandatory redeem script associated to the current P2SH input
|
|
165
|
+
* * sequence is the sequence number to use for this input (when using RBF), or non present
|
|
166
|
+
* @param associatedKeysets is an array of BIP 32 paths pointing to the path to the private key used for each UTXO
|
|
167
|
+
* @param outputScriptHex is the hexadecimal serialized outputs of the transaction to sign
|
|
168
|
+
* @param lockTime is the optional lockTime of the transaction to sign, or default (0)
|
|
169
|
+
* @param sigHashType is the hash type of the transaction to sign, or default (all)
|
|
170
|
+
* @return the signed transaction ready to be broadcast
|
|
171
|
+
* @example
|
|
172
|
+
btc.signP2SHTransaction({
|
|
173
|
+
inputs: [ [tx, 1, "52210289b4a3ad52a919abd2bdd6920d8a6879b1e788c38aa76f0440a6f32a9f1996d02103a3393b1439d1693b063482c04bd40142db97bdf139eedd1b51ffb7070a37eac321030b9a409a1e476b0d5d17b804fcdb81cf30f9b99c6f3ae1178206e08bc500639853ae"] ],
|
|
174
|
+
associatedKeysets: ["0'/0/0"],
|
|
175
|
+
outputScriptHex: "01905f0100000000001976a91472a5d75c8d2d0565b656a5232703b167d50d5a2b88ac"
|
|
176
|
+
}).then(result => ...);
|
|
177
|
+
*/
|
|
178
|
+
signP2SHTransaction(arg: SignP2SHTransactionArg) {
|
|
179
|
+
if (arguments.length > 1) {
|
|
180
|
+
console.warn(
|
|
181
|
+
"@ledgerhq/hw-app-btc: signP2SHTransaction multi argument signature is deprecated. please switch to named parameters."
|
|
182
|
+
);
|
|
183
|
+
const [
|
|
184
|
+
inputs,
|
|
185
|
+
associatedKeysets,
|
|
186
|
+
outputScriptHex,
|
|
187
|
+
lockTime,
|
|
188
|
+
sigHashType,
|
|
189
|
+
segwit,
|
|
190
|
+
transactionVersion,
|
|
191
|
+
] = arguments;
|
|
192
|
+
arg = {
|
|
193
|
+
inputs,
|
|
194
|
+
associatedKeysets,
|
|
195
|
+
outputScriptHex,
|
|
196
|
+
lockTime,
|
|
197
|
+
sigHashType,
|
|
198
|
+
segwit,
|
|
199
|
+
transactionVersion,
|
|
200
|
+
};
|
|
201
|
+
arg = fromDeprecateArguments(arguments, [
|
|
202
|
+
"inputs",
|
|
203
|
+
"associatedKeysets",
|
|
204
|
+
"outputScriptHex",
|
|
205
|
+
"lockTime",
|
|
206
|
+
"sigHashType",
|
|
207
|
+
"segwit",
|
|
208
|
+
"transactionVersion",
|
|
209
|
+
]);
|
|
210
|
+
}
|
|
211
|
+
return signP2SHTransaction(this.transport, arg);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* For each UTXO included in your transaction, create a transaction object from the raw serialized version of the transaction used in this UTXO.
|
|
216
|
+
* @example
|
|
217
|
+
const tx1 = btc.splitTransaction("01000000014ea60aeac5252c14291d428915bd7ccd1bfc4af009f4d4dc57ae597ed0420b71010000008a47304402201f36a12c240dbf9e566bc04321050b1984cd6eaf6caee8f02bb0bfec08e3354b022012ee2aeadcbbfd1e92959f57c15c1c6debb757b798451b104665aa3010569b49014104090b15bde569386734abf2a2b99f9ca6a50656627e77de663ca7325702769986cf26cc9dd7fdea0af432c8e2becc867c932e1b9dd742f2a108997c2252e2bdebffffffff0281b72e00000000001976a91472a5d75c8d2d0565b656a5232703b167d50d5a2b88aca0860100000000001976a9144533f5fb9b4817f713c48f0bfe96b9f50c476c9b88ac00000000");
|
|
218
|
+
*/
|
|
219
|
+
splitTransaction(
|
|
220
|
+
transactionHex: string,
|
|
221
|
+
isSegwitSupported: ?boolean = false,
|
|
222
|
+
hasTimestamp?: boolean = false,
|
|
223
|
+
hasExtraData?: boolean = false,
|
|
224
|
+
additionals: Array<string> = []
|
|
225
|
+
): Transaction {
|
|
226
|
+
return splitTransaction(
|
|
227
|
+
transactionHex,
|
|
228
|
+
isSegwitSupported,
|
|
229
|
+
hasTimestamp,
|
|
230
|
+
hasExtraData,
|
|
231
|
+
additionals
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
@example
|
|
237
|
+
const tx1 = btc.splitTransaction("01000000014ea60aeac5252c14291d428915bd7ccd1bfc4af009f4d4dc57ae597ed0420b71010000008a47304402201f36a12c240dbf9e566bc04321050b1984cd6eaf6caee8f02bb0bfec08e3354b022012ee2aeadcbbfd1e92959f57c15c1c6debb757b798451b104665aa3010569b49014104090b15bde569386734abf2a2b99f9ca6a50656627e77de663ca7325702769986cf26cc9dd7fdea0af432c8e2becc867c932e1b9dd742f2a108997c2252e2bdebffffffff0281b72e00000000001976a91472a5d75c8d2d0565b656a5232703b167d50d5a2b88aca0860100000000001976a9144533f5fb9b4817f713c48f0bfe96b9f50c476c9b88ac00000000");
|
|
238
|
+
const outputScript = btc.serializeTransactionOutputs(tx1).toString('hex');
|
|
239
|
+
*/
|
|
240
|
+
serializeTransactionOutputs(t: Transaction): Buffer {
|
|
241
|
+
return serializeTransactionOutputs(t);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
getTrustedInput(
|
|
245
|
+
indexLookup: number,
|
|
246
|
+
transaction: Transaction,
|
|
247
|
+
additionals: Array<string> = []
|
|
248
|
+
): Promise<string> {
|
|
249
|
+
return getTrustedInput(
|
|
250
|
+
this.transport,
|
|
251
|
+
indexLookup,
|
|
252
|
+
transaction,
|
|
253
|
+
additionals
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
getTrustedInputBIP143(
|
|
258
|
+
indexLookup: number,
|
|
259
|
+
transaction: Transaction,
|
|
260
|
+
additionals: Array<string> = []
|
|
261
|
+
): string {
|
|
262
|
+
return getTrustedInputBIP143(
|
|
263
|
+
this.transport,
|
|
264
|
+
indexLookup,
|
|
265
|
+
transaction,
|
|
266
|
+
additionals
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function fromDeprecateArguments(args, keys) {
|
|
272
|
+
const obj = {};
|
|
273
|
+
keys.forEach((key, i) => {
|
|
274
|
+
const value = args[i];
|
|
275
|
+
if (value !== undefined) {
|
|
276
|
+
obj[key] = value;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
return obj;
|
|
280
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import bippath from "bip32-path";
|
|
4
|
+
|
|
5
|
+
export function bip32asBuffer(path: string): Buffer {
|
|
6
|
+
const paths = !path ? [] : bippath.fromString(path).toPathArray();
|
|
7
|
+
let buffer = Buffer.alloc(1 + paths.length * 4);
|
|
8
|
+
buffer[0] = paths.length;
|
|
9
|
+
paths.forEach((element, index) => {
|
|
10
|
+
buffer.writeUInt32BE(element, 1 + 4 * index);
|
|
11
|
+
});
|
|
12
|
+
return buffer;
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
export function compressPublicKey(publicKey: Buffer): Buffer {
|
|
4
|
+
const prefix = (publicKey[64] & 1) !== 0 ? 0x03 : 0x02;
|
|
5
|
+
const prefixBuffer = Buffer.alloc(1);
|
|
6
|
+
prefixBuffer[0] = prefix;
|
|
7
|
+
return Buffer.concat([prefixBuffer, publicKey.slice(1, 1 + 32)]);
|
|
8
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// flow
|
|
2
|
+
|
|
3
|
+
export const MAX_SCRIPT_BLOCK = 50;
|
|
4
|
+
export const DEFAULT_VERSION = 1;
|
|
5
|
+
export const DEFAULT_LOCKTIME = 0;
|
|
6
|
+
export const DEFAULT_SEQUENCE = 0xffffffff;
|
|
7
|
+
export const SIGHASH_ALL = 1;
|
|
8
|
+
export const OP_DUP = 0x76;
|
|
9
|
+
export const OP_HASH160 = 0xa9;
|
|
10
|
+
export const HASH_SIZE = 0x14;
|
|
11
|
+
export const OP_EQUALVERIFY = 0x88;
|
|
12
|
+
export const OP_CHECKSIG = 0xac;
|
|
13
|
+
export const OP_RETURN = 0x6a;
|