@ledgerhq/hw-app-btc 10.0.4 → 10.0.5-next.0

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.
Files changed (167) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/Btc.d.ts.map +1 -1
  3. package/lib/Btc.js +6 -8
  4. package/lib/Btc.js.map +1 -1
  5. package/lib/BtcNew.d.ts +1 -1
  6. package/lib/BtcNew.d.ts.map +1 -1
  7. package/lib/BtcNew.js +5 -6
  8. package/lib/BtcNew.js.map +1 -1
  9. package/lib/BtcOld.d.ts +1 -1
  10. package/lib/BtcOld.d.ts.map +1 -1
  11. package/lib/BtcOld.js +2 -5
  12. package/lib/BtcOld.js.map +1 -1
  13. package/lib/buffertools.js +3 -3
  14. package/lib/buffertools.js.map +1 -1
  15. package/lib/createTransaction.d.ts +1 -6
  16. package/lib/createTransaction.d.ts.map +1 -1
  17. package/lib/createTransaction.js +5 -13
  18. package/lib/createTransaction.js.map +1 -1
  19. package/lib/finalizeInput.d.ts.map +1 -1
  20. package/lib/finalizeInput.js.map +1 -1
  21. package/lib/getAppAndVersion.d.ts.map +1 -1
  22. package/lib/getAppAndVersion.js.map +1 -1
  23. package/lib/getTrustedInput.d.ts.map +1 -1
  24. package/lib/getTrustedInput.js +5 -12
  25. package/lib/getTrustedInput.js.map +1 -1
  26. package/lib/getTrustedInputBIP143.d.ts.map +1 -1
  27. package/lib/getTrustedInputBIP143.js.map +1 -1
  28. package/lib/getWalletPublicKey.d.ts.map +1 -1
  29. package/lib/getWalletPublicKey.js.map +1 -1
  30. package/lib/newops/accounttype.d.ts.map +1 -1
  31. package/lib/newops/accounttype.js +2 -6
  32. package/lib/newops/accounttype.js.map +1 -1
  33. package/lib/newops/appClient.d.ts.map +1 -1
  34. package/lib/newops/appClient.js +7 -14
  35. package/lib/newops/appClient.js.map +1 -1
  36. package/lib/newops/clientCommands.d.ts.map +1 -1
  37. package/lib/newops/clientCommands.js +2 -2
  38. package/lib/newops/clientCommands.js.map +1 -1
  39. package/lib/newops/merkelizedPsbt.d.ts.map +1 -1
  40. package/lib/newops/merkelizedPsbt.js +4 -4
  41. package/lib/newops/merkelizedPsbt.js.map +1 -1
  42. package/lib/newops/merkle.d.ts.map +1 -1
  43. package/lib/newops/merkle.js.map +1 -1
  44. package/lib/newops/merkleMap.js +2 -2
  45. package/lib/newops/merkleMap.js.map +1 -1
  46. package/lib/newops/policy.d.ts.map +1 -1
  47. package/lib/newops/policy.js +2 -2
  48. package/lib/newops/policy.js.map +1 -1
  49. package/lib/newops/psbtFinalizer.d.ts.map +1 -1
  50. package/lib/newops/psbtFinalizer.js.map +1 -1
  51. package/lib/newops/psbtv2.d.ts.map +1 -1
  52. package/lib/newops/psbtv2.js +6 -6
  53. package/lib/newops/psbtv2.js.map +1 -1
  54. package/lib/serializeTransaction.d.ts.map +1 -1
  55. package/lib/serializeTransaction.js +3 -4
  56. package/lib/serializeTransaction.js.map +1 -1
  57. package/lib/signMessage.d.ts.map +1 -1
  58. package/lib/signMessage.js +2 -6
  59. package/lib/signMessage.js.map +1 -1
  60. package/lib/signP2SHTransaction.d.ts +1 -6
  61. package/lib/signP2SHTransaction.d.ts.map +1 -1
  62. package/lib/signP2SHTransaction.js +2 -6
  63. package/lib/signP2SHTransaction.js.map +1 -1
  64. package/lib/signTransaction.d.ts.map +1 -1
  65. package/lib/signTransaction.js +2 -7
  66. package/lib/signTransaction.js.map +1 -1
  67. package/lib/splitTransaction.d.ts.map +1 -1
  68. package/lib/splitTransaction.js.map +1 -1
  69. package/lib/startUntrustedHashTransactionInput.d.ts.map +1 -1
  70. package/lib/startUntrustedHashTransactionInput.js +1 -4
  71. package/lib/startUntrustedHashTransactionInput.js.map +1 -1
  72. package/lib-es/Btc.d.ts.map +1 -1
  73. package/lib-es/Btc.js +6 -8
  74. package/lib-es/Btc.js.map +1 -1
  75. package/lib-es/BtcNew.d.ts +1 -1
  76. package/lib-es/BtcNew.d.ts.map +1 -1
  77. package/lib-es/BtcNew.js +6 -7
  78. package/lib-es/BtcNew.js.map +1 -1
  79. package/lib-es/BtcOld.d.ts +1 -1
  80. package/lib-es/BtcOld.d.ts.map +1 -1
  81. package/lib-es/BtcOld.js +2 -5
  82. package/lib-es/BtcOld.js.map +1 -1
  83. package/lib-es/buffertools.js +3 -3
  84. package/lib-es/buffertools.js.map +1 -1
  85. package/lib-es/createTransaction.d.ts +1 -6
  86. package/lib-es/createTransaction.d.ts.map +1 -1
  87. package/lib-es/createTransaction.js +5 -13
  88. package/lib-es/createTransaction.js.map +1 -1
  89. package/lib-es/finalizeInput.d.ts.map +1 -1
  90. package/lib-es/finalizeInput.js.map +1 -1
  91. package/lib-es/getAppAndVersion.d.ts.map +1 -1
  92. package/lib-es/getAppAndVersion.js.map +1 -1
  93. package/lib-es/getTrustedInput.d.ts.map +1 -1
  94. package/lib-es/getTrustedInput.js +5 -12
  95. package/lib-es/getTrustedInput.js.map +1 -1
  96. package/lib-es/getTrustedInputBIP143.d.ts.map +1 -1
  97. package/lib-es/getTrustedInputBIP143.js.map +1 -1
  98. package/lib-es/getWalletPublicKey.d.ts.map +1 -1
  99. package/lib-es/getWalletPublicKey.js.map +1 -1
  100. package/lib-es/newops/accounttype.d.ts.map +1 -1
  101. package/lib-es/newops/accounttype.js +3 -7
  102. package/lib-es/newops/accounttype.js.map +1 -1
  103. package/lib-es/newops/appClient.d.ts.map +1 -1
  104. package/lib-es/newops/appClient.js +7 -14
  105. package/lib-es/newops/appClient.js.map +1 -1
  106. package/lib-es/newops/clientCommands.d.ts.map +1 -1
  107. package/lib-es/newops/clientCommands.js +2 -2
  108. package/lib-es/newops/clientCommands.js.map +1 -1
  109. package/lib-es/newops/merkelizedPsbt.d.ts.map +1 -1
  110. package/lib-es/newops/merkelizedPsbt.js +4 -4
  111. package/lib-es/newops/merkelizedPsbt.js.map +1 -1
  112. package/lib-es/newops/merkle.d.ts.map +1 -1
  113. package/lib-es/newops/merkle.js.map +1 -1
  114. package/lib-es/newops/merkleMap.js +2 -2
  115. package/lib-es/newops/merkleMap.js.map +1 -1
  116. package/lib-es/newops/policy.d.ts.map +1 -1
  117. package/lib-es/newops/policy.js +2 -2
  118. package/lib-es/newops/policy.js.map +1 -1
  119. package/lib-es/newops/psbtFinalizer.d.ts.map +1 -1
  120. package/lib-es/newops/psbtFinalizer.js.map +1 -1
  121. package/lib-es/newops/psbtv2.d.ts.map +1 -1
  122. package/lib-es/newops/psbtv2.js +7 -7
  123. package/lib-es/newops/psbtv2.js.map +1 -1
  124. package/lib-es/serializeTransaction.d.ts.map +1 -1
  125. package/lib-es/serializeTransaction.js +3 -4
  126. package/lib-es/serializeTransaction.js.map +1 -1
  127. package/lib-es/signMessage.d.ts.map +1 -1
  128. package/lib-es/signMessage.js +2 -6
  129. package/lib-es/signMessage.js.map +1 -1
  130. package/lib-es/signP2SHTransaction.d.ts +1 -6
  131. package/lib-es/signP2SHTransaction.d.ts.map +1 -1
  132. package/lib-es/signP2SHTransaction.js +3 -7
  133. package/lib-es/signP2SHTransaction.js.map +1 -1
  134. package/lib-es/signTransaction.d.ts.map +1 -1
  135. package/lib-es/signTransaction.js +2 -7
  136. package/lib-es/signTransaction.js.map +1 -1
  137. package/lib-es/splitTransaction.d.ts.map +1 -1
  138. package/lib-es/splitTransaction.js.map +1 -1
  139. package/lib-es/startUntrustedHashTransactionInput.d.ts.map +1 -1
  140. package/lib-es/startUntrustedHashTransactionInput.js +1 -4
  141. package/lib-es/startUntrustedHashTransactionInput.js.map +1 -1
  142. package/package.json +5 -5
  143. package/src/Btc.ts +17 -31
  144. package/src/BtcNew.ts +27 -62
  145. package/src/BtcOld.ts +8 -19
  146. package/src/buffertools.ts +3 -3
  147. package/src/createTransaction.ts +20 -52
  148. package/src/finalizeInput.ts +2 -5
  149. package/src/getAppAndVersion.ts +2 -6
  150. package/src/getTrustedInput.ts +12 -32
  151. package/src/getTrustedInputBIP143.ts +2 -4
  152. package/src/getWalletPublicKey.ts +3 -11
  153. package/src/newops/accounttype.ts +21 -76
  154. package/src/newops/appClient.ts +17 -39
  155. package/src/newops/clientCommands.ts +7 -16
  156. package/src/newops/merkelizedPsbt.ts +6 -14
  157. package/src/newops/merkle.ts +3 -10
  158. package/src/newops/merkleMap.ts +2 -2
  159. package/src/newops/policy.ts +5 -15
  160. package/src/newops/psbtFinalizer.ts +3 -8
  161. package/src/newops/psbtv2.ts +32 -85
  162. package/src/serializeTransaction.ts +5 -9
  163. package/src/signMessage.ts +5 -16
  164. package/src/signP2SHTransaction.ts +11 -34
  165. package/src/signTransaction.ts +3 -8
  166. package/src/splitTransaction.ts +3 -7
  167. package/src/startUntrustedHashTransactionInput.ts +7 -10
@@ -7,9 +7,7 @@ export type AppAndVersion = {
7
7
  flags: number | Buffer;
8
8
  };
9
9
 
10
- export const getAppAndVersion = async (
11
- transport: Transport
12
- ): Promise<AppAndVersion> => {
10
+ export const getAppAndVersion = async (transport: Transport): Promise<AppAndVersion> => {
13
11
  const r = await transport.send(0xb0, 0x01, 0x00, 0x00);
14
12
  let i = 0;
15
13
  const format = r[i++];
@@ -27,9 +25,7 @@ export const getAppAndVersion = async (
27
25
  };
28
26
  };
29
27
 
30
- export const checkIsBtcLegacy = async (
31
- transport: Transport
32
- ): Promise<boolean> => {
28
+ export const checkIsBtcLegacy = async (transport: Transport): Promise<boolean> => {
33
29
  try {
34
30
  // Call old btc API, it will throw an exception with new btc app. It is a workaround to differentiate new/old btc nano app
35
31
  await transport.send(0xe0, 0xc4, 0, 0);
@@ -6,7 +6,7 @@ import { createVarint } from "./varint";
6
6
  export async function getTrustedInputRaw(
7
7
  transport: Transport,
8
8
  transactionData: Buffer,
9
- indexLookup?: number | null | undefined
9
+ indexLookup?: number | null | undefined,
10
10
  ): Promise<string> {
11
11
  let data;
12
12
  let firstRound = false;
@@ -20,13 +20,7 @@ export async function getTrustedInputRaw(
20
20
  data = transactionData;
21
21
  }
22
22
 
23
- const trustedInput = await transport.send(
24
- 0xe0,
25
- 0x42,
26
- firstRound ? 0x00 : 0x80,
27
- 0x00,
28
- data
29
- );
23
+ const trustedInput = await transport.send(0xe0, 0x42, firstRound ? 0x00 : 0x80, 0x00, data);
30
24
  const res = trustedInput.slice(0, trustedInput.length - 2).toString("hex");
31
25
  return res;
32
26
  }
@@ -34,10 +28,9 @@ export async function getTrustedInput(
34
28
  transport: Transport,
35
29
  indexLookup: number,
36
30
  transaction: Transaction,
37
- additionals: Array<string> = []
31
+ additionals: Array<string> = [],
38
32
  ): Promise<string> {
39
- const { version, inputs, outputs, locktime, nExpiryHeight, extraData } =
40
- transaction;
33
+ const { version, inputs, outputs, locktime, nExpiryHeight, extraData } = transaction;
41
34
 
42
35
  if (!outputs || !locktime) {
43
36
  throw new Error("getTrustedInput: locktime & outputs is expected");
@@ -53,16 +46,12 @@ export async function getTrustedInput(
53
46
 
54
47
  while (offset !== script.length) {
55
48
  const blockSize =
56
- script.length - offset > MAX_SCRIPT_BLOCK
57
- ? MAX_SCRIPT_BLOCK
58
- : script.length - offset;
49
+ script.length - offset > MAX_SCRIPT_BLOCK ? MAX_SCRIPT_BLOCK : script.length - offset;
59
50
 
60
51
  if (offset + blockSize !== script.length) {
61
52
  scriptBlocks.push(script.slice(offset, offset + blockSize));
62
53
  } else {
63
- scriptBlocks.push(
64
- Buffer.concat([script.slice(offset, offset + blockSize), seq])
65
- );
54
+ scriptBlocks.push(Buffer.concat([script.slice(offset, offset + blockSize), seq]));
66
55
  }
67
56
 
68
57
  offset += blockSize;
@@ -83,8 +72,7 @@ export async function getTrustedInput(
83
72
  return res;
84
73
  };
85
74
 
86
- const processWholeScriptBlock = (block) =>
87
- getTrustedInputRaw(transport, block);
75
+ const processWholeScriptBlock = block => getTrustedInputRaw(transport, block);
88
76
 
89
77
  await getTrustedInputRaw(
90
78
  transport,
@@ -94,16 +82,12 @@ export async function getTrustedInput(
94
82
  transaction.nVersionGroupId || Buffer.alloc(0),
95
83
  createVarint(inputs.length),
96
84
  ]),
97
- indexLookup
85
+ indexLookup,
98
86
  );
99
87
 
100
88
  for (const input of inputs) {
101
- const isXSTV2 =
102
- isXST &&
103
- Buffer.compare(version, Buffer.from([0x02, 0x00, 0x00, 0x00])) === 0;
104
- const treeField = isDecred
105
- ? input.tree || Buffer.from([0x00])
106
- : Buffer.alloc(0);
89
+ const isXSTV2 = isXST && Buffer.compare(version, Buffer.from([0x02, 0x00, 0x00, 0x00])) === 0;
90
+ const treeField = isDecred ? input.tree || Buffer.from([0x00]) : Buffer.alloc(0);
107
91
  const data = Buffer.concat([
108
92
  input.prevout,
109
93
  treeField,
@@ -147,14 +131,10 @@ export async function getTrustedInput(
147
131
 
148
132
  if (endData.length) {
149
133
  const data = Buffer.concat(endData);
150
- extraPart = isDecred
151
- ? data
152
- : Buffer.concat([createVarint(data.length), data]);
134
+ extraPart = isDecred ? data : Buffer.concat([createVarint(data.length), data]);
153
135
  }
154
136
 
155
- const res = await processScriptBlocks(
156
- Buffer.concat([locktime, extraPart || Buffer.alloc(0)])
157
- );
137
+ const res = await processScriptBlocks(Buffer.concat([locktime, extraPart || Buffer.alloc(0)]));
158
138
  invariant(res, "missing result in processScriptBlocks");
159
139
  return res;
160
140
  }
@@ -6,7 +6,7 @@ export function getTrustedInputBIP143(
6
6
  transport: Transport,
7
7
  indexLookup: number,
8
8
  transaction: Transaction,
9
- additionals: Array<string> = []
9
+ additionals: Array<string> = [],
10
10
  ): string {
11
11
  if (!transaction) {
12
12
  throw new Error("getTrustedInputBIP143: missing tx");
@@ -19,9 +19,7 @@ export function getTrustedInputBIP143(
19
19
  }
20
20
 
21
21
  let hash = shajs("sha256")
22
- .update(
23
- shajs("sha256").update(serializeTransaction(transaction, true)).digest()
24
- )
22
+ .update(shajs("sha256").update(serializeTransaction(transaction, true)).digest())
25
23
  .digest();
26
24
  const data = Buffer.alloc(4);
27
25
  data.writeUInt32LE(indexLookup, 0);
@@ -4,12 +4,7 @@ import { bip32asBuffer } from "./bip32";
4
4
  /**
5
5
  * address format is one of legacy | p2sh | bech32 | cashaddr
6
6
  */
7
- export type AddressFormat =
8
- | "legacy"
9
- | "p2sh"
10
- | "bech32"
11
- | "bech32m"
12
- | "cashaddr";
7
+ export type AddressFormat = "legacy" | "p2sh" | "bech32" | "bech32m" | "cashaddr";
13
8
  const addressFormatMap = {
14
9
  legacy: 0,
15
10
  p2sh: 1,
@@ -22,7 +17,7 @@ export async function getWalletPublicKey(
22
17
  path: string;
23
18
  verify?: boolean;
24
19
  format?: AddressFormat;
25
- }
20
+ },
26
21
  ): Promise<{
27
22
  publicKey: string;
28
23
  bitcoinAddress: string;
@@ -49,10 +44,7 @@ export async function getWalletPublicKey(
49
44
  .slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength)
50
45
  .toString("ascii");
51
46
  const chainCode = response
52
- .slice(
53
- 1 + publicKeyLength + 1 + addressLength,
54
- 1 + publicKeyLength + 1 + addressLength + 32
55
- )
47
+ .slice(1 + publicKeyLength + 1 + addressLength, 1 + publicKeyLength + 1 + addressLength + 32)
56
48
  .toString("hex");
57
49
  return {
58
50
  publicKey,
@@ -1,14 +1,7 @@
1
1
  import { crypto } from "bitcoinjs-lib";
2
2
  import { pointAddScalar } from "tiny-secp256k1";
3
3
  import { BufferWriter } from "../buffertools";
4
- import {
5
- HASH_SIZE,
6
- OP_CHECKSIG,
7
- OP_DUP,
8
- OP_EQUAL,
9
- OP_EQUALVERIFY,
10
- OP_HASH160,
11
- } from "../constants";
4
+ import { HASH_SIZE, OP_CHECKSIG, OP_DUP, OP_EQUAL, OP_EQUALVERIFY, OP_HASH160 } from "../constants";
12
5
  import { hashPublicKey } from "../hashPublicKey";
13
6
  import { DefaultDescriptorTemplate } from "./policy";
14
7
  import { PsbtV2 } from "./psbtv2";
@@ -51,7 +44,7 @@ export interface AccountType {
51
44
  inputTx: Buffer | undefined,
52
45
  spentOutput: SpentOutput,
53
46
  pubkeys: Buffer[],
54
- pathElems: number[][]
47
+ pathElems: number[][],
55
48
  ): void;
56
49
 
57
50
  /**
@@ -63,12 +56,7 @@ export interface AccountType {
63
56
  * @param pubkeys The 33 byte ecdsa compressed public keys involved in this output
64
57
  * @param paths The paths corresponding to the pubkeys, in same order.
65
58
  */
66
- setOwnOutput(
67
- i: number,
68
- cond: SpendingCondition,
69
- pubkeys: Buffer[],
70
- paths: number[][]
71
- ): void;
59
+ setOwnOutput(i: number, cond: SpendingCondition, pubkeys: Buffer[], paths: number[][]): void;
72
60
 
73
61
  /**
74
62
  * Returns the descriptor template for this account type. Currently only
@@ -105,7 +93,7 @@ abstract class SingleKeyAccount extends BaseAccount {
105
93
  inputTx: Buffer | undefined,
106
94
  spentOutput: SpentOutput,
107
95
  pubkeys: Buffer[],
108
- pathElems: number[][]
96
+ pathElems: number[][],
109
97
  ) {
110
98
  if (pubkeys.length != 1) {
111
99
  throw new Error("Expected single key, got " + pubkeys.length);
@@ -120,15 +108,10 @@ abstract class SingleKeyAccount extends BaseAccount {
120
108
  inputTx: Buffer | undefined,
121
109
  spentOutput: SpentOutput,
122
110
  pubkey: Buffer,
123
- path: number[]
111
+ path: number[],
124
112
  );
125
113
 
126
- setOwnOutput(
127
- i: number,
128
- cond: SpendingCondition,
129
- pubkeys: Buffer[],
130
- paths: number[][]
131
- ) {
114
+ setOwnOutput(i: number, cond: SpendingCondition, pubkeys: Buffer[], paths: number[][]) {
132
115
  if (pubkeys.length != 1) {
133
116
  throw new Error("Expected single key, got " + pubkeys.length);
134
117
  }
@@ -141,7 +124,7 @@ abstract class SingleKeyAccount extends BaseAccount {
141
124
  i: number,
142
125
  cond: SpendingCondition,
143
126
  pubkey: Buffer,
144
- path: number[]
127
+ path: number[],
145
128
  );
146
129
  }
147
130
 
@@ -160,7 +143,7 @@ export class p2pkh extends SingleKeyAccount {
160
143
  inputTx: Buffer | undefined,
161
144
  _spentOutput: SpentOutput,
162
145
  pubkey: Buffer,
163
- path: number[]
146
+ path: number[],
164
147
  ) {
165
148
  if (!inputTx) {
166
149
  throw new Error("Full input base transaction required");
@@ -169,12 +152,7 @@ export class p2pkh extends SingleKeyAccount {
169
152
  this.psbt.setInputBip32Derivation(i, pubkey, this.masterFp, path);
170
153
  }
171
154
 
172
- setSingleKeyOutput(
173
- i: number,
174
- cond: SpendingCondition,
175
- pubkey: Buffer,
176
- path: number[]
177
- ) {
155
+ setSingleKeyOutput(i: number, cond: SpendingCondition, pubkey: Buffer, path: number[]) {
178
156
  this.psbt.setOutputBip32Derivation(i, pubkey, this.masterFp, path);
179
157
  }
180
158
 
@@ -198,23 +176,14 @@ export class p2tr extends SingleKeyAccount {
198
176
  _inputTx: Buffer | undefined,
199
177
  spentOutput: SpentOutput,
200
178
  pubkey: Buffer,
201
- path: number[]
179
+ path: number[],
202
180
  ) {
203
181
  const xonly = pubkey.slice(1);
204
182
  this.psbt.setInputTapBip32Derivation(i, xonly, [], this.masterFp, path);
205
- this.psbt.setInputWitnessUtxo(
206
- i,
207
- spentOutput.amount,
208
- spentOutput.cond.scriptPubKey
209
- );
183
+ this.psbt.setInputWitnessUtxo(i, spentOutput.amount, spentOutput.cond.scriptPubKey);
210
184
  }
211
185
 
212
- setSingleKeyOutput(
213
- i: number,
214
- cond: SpendingCondition,
215
- pubkey: Buffer,
216
- path: number[]
217
- ) {
186
+ setSingleKeyOutput(i: number, cond: SpendingCondition, pubkey: Buffer, path: number[]) {
218
187
  const xonly = pubkey.slice(1);
219
188
  this.psbt.setOutputTapBip32Derivation(i, xonly, [], this.masterFp, path);
220
189
  }
@@ -251,10 +220,7 @@ export class p2tr extends SingleKeyAccount {
251
220
  // the first byte, which represent the oddness/evenness. In schnorr all
252
221
  // pubkeys are even.
253
222
  // https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#public-key-conversion
254
- const evenEcdsaPubkey = Buffer.concat([
255
- Buffer.from([0x02]),
256
- internalPubkey,
257
- ]);
223
+ const evenEcdsaPubkey = Buffer.concat([Buffer.from([0x02]), internalPubkey]);
258
224
  const tweak = this.hashTapTweak(internalPubkey);
259
225
 
260
226
  // Q = P + int(hash_TapTweak(bytes(P)))G
@@ -282,7 +248,7 @@ export class p2wpkhWrapped extends SingleKeyAccount {
282
248
  inputTx: Buffer | undefined,
283
249
  spentOutput: SpentOutput,
284
250
  pubkey: Buffer,
285
- path: number[]
251
+ path: number[],
286
252
  ) {
287
253
  if (!inputTx) {
288
254
  throw new Error("Full input base transaction required");
@@ -292,30 +258,18 @@ export class p2wpkhWrapped extends SingleKeyAccount {
292
258
 
293
259
  const userSuppliedRedeemScript = spentOutput.cond.redeemScript;
294
260
  const expectedRedeemScript = this.createRedeemScript(pubkey);
295
- if (
296
- userSuppliedRedeemScript &&
297
- !expectedRedeemScript.equals(userSuppliedRedeemScript)
298
- ) {
261
+ if (userSuppliedRedeemScript && !expectedRedeemScript.equals(userSuppliedRedeemScript)) {
299
262
  // At what point might a user set the redeemScript on its own?
300
263
  throw new Error(`User-supplied redeemScript ${userSuppliedRedeemScript.toString(
301
- "hex"
264
+ "hex",
302
265
  )} doesn't
303
266
  match expected ${expectedRedeemScript.toString("hex")} for input ${i}`);
304
267
  }
305
268
  this.psbt.setInputRedeemScript(i, expectedRedeemScript);
306
- this.psbt.setInputWitnessUtxo(
307
- i,
308
- spentOutput.amount,
309
- spentOutput.cond.scriptPubKey
310
- );
269
+ this.psbt.setInputWitnessUtxo(i, spentOutput.amount, spentOutput.cond.scriptPubKey);
311
270
  }
312
271
 
313
- setSingleKeyOutput(
314
- i: number,
315
- cond: SpendingCondition,
316
- pubkey: Buffer,
317
- path: number[]
318
- ) {
272
+ setSingleKeyOutput(i: number, cond: SpendingCondition, pubkey: Buffer, path: number[]) {
319
273
  this.psbt.setOutputRedeemScript(i, cond.redeemScript!);
320
274
  this.psbt.setOutputBip32Derivation(i, pubkey, this.masterFp, path);
321
275
  }
@@ -344,26 +298,17 @@ export class p2wpkh extends SingleKeyAccount {
344
298
  inputTx: Buffer | undefined,
345
299
  spentOutput: SpentOutput,
346
300
  pubkey: Buffer,
347
- path: number[]
301
+ path: number[],
348
302
  ) {
349
303
  if (!inputTx) {
350
304
  throw new Error("Full input base transaction required");
351
305
  }
352
306
  this.psbt.setInputNonWitnessUtxo(i, inputTx);
353
307
  this.psbt.setInputBip32Derivation(i, pubkey, this.masterFp, path);
354
- this.psbt.setInputWitnessUtxo(
355
- i,
356
- spentOutput.amount,
357
- spentOutput.cond.scriptPubKey
358
- );
308
+ this.psbt.setInputWitnessUtxo(i, spentOutput.amount, spentOutput.cond.scriptPubKey);
359
309
  }
360
310
 
361
- setSingleKeyOutput(
362
- i: number,
363
- cond: SpendingCondition,
364
- pubkey: Buffer,
365
- path: number[]
366
- ) {
311
+ setSingleKeyOutput(i: number, cond: SpendingCondition, pubkey: Buffer, path: number[]) {
367
312
  this.psbt.setOutputBip32Derivation(i, pubkey, this.masterFp, path);
368
313
  }
369
314
 
@@ -38,16 +38,9 @@ export class AppClient {
38
38
  private async makeRequest(
39
39
  ins: BitcoinIns,
40
40
  data: Buffer,
41
- cci?: ClientCommandInterpreter
41
+ cci?: ClientCommandInterpreter,
42
42
  ): Promise<Buffer> {
43
- let response: Buffer = await this.transport.send(
44
- CLA_BTC,
45
- ins,
46
- 0,
47
- 0,
48
- data,
49
- [0x9000, 0xe000]
50
- );
43
+ let response: Buffer = await this.transport.send(CLA_BTC, ins, 0, 0, data, [0x9000, 0xe000]);
51
44
  while (response.readUInt16BE(response.length - 2) === 0xe000) {
52
45
  if (!cci) {
53
46
  throw new Error("Unexpected SW_INTERRUPTED_EXECUTION");
@@ -62,25 +55,19 @@ export class AppClient {
62
55
  0,
63
56
  0,
64
57
  commandResponse,
65
- [0x9000, 0xe000]
58
+ [0x9000, 0xe000],
66
59
  );
67
60
  }
68
61
  return response.slice(0, -2); // drop the status word (can only be 0x9000 at this point)
69
62
  }
70
63
 
71
- async getExtendedPubkey(
72
- display: boolean,
73
- pathElements: number[]
74
- ): Promise<string> {
64
+ async getExtendedPubkey(display: boolean, pathElements: number[]): Promise<string> {
75
65
  if (pathElements.length > 6) {
76
66
  throw new Error("Path too long. At most 6 levels allowed.");
77
67
  }
78
68
  const response = await this.makeRequest(
79
69
  BitcoinIns.GET_PUBKEY,
80
- Buffer.concat([
81
- Buffer.from(display ? [1] : [0]),
82
- pathElementsToBuffer(pathElements),
83
- ])
70
+ Buffer.concat([Buffer.from(display ? [1] : [0]), pathElementsToBuffer(pathElements)]),
84
71
  );
85
72
  return response.toString("ascii");
86
73
  }
@@ -90,10 +77,9 @@ export class AppClient {
90
77
  walletHMAC: Buffer | null,
91
78
  change: number,
92
79
  addressIndex: number,
93
- display: boolean
80
+ display: boolean,
94
81
  ): Promise<string> {
95
- if (change !== 0 && change !== 1)
96
- throw new Error("Change can only be 0 or 1");
82
+ if (change !== 0 && change !== 1) throw new Error("Change can only be 0 or 1");
97
83
  if (addressIndex < 0 || !Number.isInteger(addressIndex))
98
84
  throw new Error("Invalid address index");
99
85
 
@@ -102,9 +88,7 @@ export class AppClient {
102
88
  }
103
89
 
104
90
  const clientInterpreter = new ClientCommandInterpreter(() => {});
105
- clientInterpreter.addKnownList(
106
- walletPolicy.keys.map((k) => Buffer.from(k, "ascii"))
107
- );
91
+ clientInterpreter.addKnownList(walletPolicy.keys.map(k => Buffer.from(k, "ascii")));
108
92
  clientInterpreter.addKnownPreimage(walletPolicy.serialize());
109
93
 
110
94
  const addressIndexBuffer = Buffer.alloc(4);
@@ -119,7 +103,7 @@ export class AppClient {
119
103
  Buffer.from([change]),
120
104
  addressIndexBuffer,
121
105
  ]),
122
- clientInterpreter
106
+ clientInterpreter,
123
107
  );
124
108
 
125
109
  return response.toString("ascii");
@@ -129,7 +113,7 @@ export class AppClient {
129
113
  psbt: PsbtV2,
130
114
  walletPolicy: WalletPolicy,
131
115
  walletHMAC: Buffer | null,
132
- progressCallback: () => void
116
+ progressCallback: () => void,
133
117
  ): Promise<Map<number, Buffer>> {
134
118
  const merkelizedPsbt = new MerkelizedPsbt(psbt);
135
119
 
@@ -140,9 +124,7 @@ export class AppClient {
140
124
  const clientInterpreter = new ClientCommandInterpreter(progressCallback);
141
125
 
142
126
  // prepare ClientCommandInterpreter
143
- clientInterpreter.addKnownList(
144
- walletPolicy.keys.map((k) => Buffer.from(k, "ascii"))
145
- );
127
+ clientInterpreter.addKnownList(walletPolicy.keys.map(k => Buffer.from(k, "ascii")));
146
128
  clientInterpreter.addKnownPreimage(walletPolicy.serialize());
147
129
 
148
130
  clientInterpreter.addKnownMapping(merkelizedPsbt.globalMerkleMap);
@@ -155,11 +137,11 @@ export class AppClient {
155
137
 
156
138
  clientInterpreter.addKnownList(merkelizedPsbt.inputMapCommitments);
157
139
  const inputMapsRoot = new Merkle(
158
- merkelizedPsbt.inputMapCommitments.map((m) => hashLeaf(m))
140
+ merkelizedPsbt.inputMapCommitments.map(m => hashLeaf(m)),
159
141
  ).getRoot();
160
142
  clientInterpreter.addKnownList(merkelizedPsbt.outputMapCommitments);
161
143
  const outputMapsRoot = new Merkle(
162
- merkelizedPsbt.outputMapCommitments.map((m) => hashLeaf(m))
144
+ merkelizedPsbt.outputMapCommitments.map(m => hashLeaf(m)),
163
145
  ).getRoot();
164
146
 
165
147
  await this.makeRequest(
@@ -173,7 +155,7 @@ export class AppClient {
173
155
  walletPolicy.getWalletId(),
174
156
  walletHMAC || Buffer.alloc(32, 0),
175
157
  ]),
176
- clientInterpreter
158
+ clientInterpreter,
177
159
  );
178
160
 
179
161
  const yielded = clientInterpreter.getYielded();
@@ -204,16 +186,12 @@ export class AppClient {
204
186
  }
205
187
 
206
188
  clientInterpreter.addKnownList(chunks);
207
- const chunksRoot = new Merkle(chunks.map((m) => hashLeaf(m))).getRoot();
189
+ const chunksRoot = new Merkle(chunks.map(m => hashLeaf(m))).getRoot();
208
190
 
209
191
  const response = await this.makeRequest(
210
192
  BitcoinIns.SIGN_MESSAGE,
211
- Buffer.concat([
212
- pathElementsToBuffer(pathElements),
213
- createVarint(message.length),
214
- chunksRoot,
215
- ]),
216
- clientInterpreter
193
+ Buffer.concat([pathElementsToBuffer(pathElements), createVarint(message.length), chunksRoot]),
194
+ clientInterpreter,
217
195
  );
218
196
 
219
197
  return response.toString("base64");
@@ -121,9 +121,7 @@ export class GetMerkleLeafProofCommand extends ClientCommand {
121
121
  tree_size = reqBuf.readVarInt();
122
122
  leaf_index = reqBuf.readVarInt();
123
123
  } catch (e: any) {
124
- throw new Error(
125
- "Invalid request, couldn't parse tree_size or leaf_index"
126
- );
124
+ throw new Error("Invalid request, couldn't parse tree_size or leaf_index");
127
125
  }
128
126
 
129
127
  const mt = this.known_trees.get(hash_hex);
@@ -136,17 +134,12 @@ export class GetMerkleLeafProofCommand extends ClientCommand {
136
134
  }
137
135
 
138
136
  if (this.queue.length != 0) {
139
- throw Error(
140
- "This command should not execute when the queue is not empty."
141
- );
137
+ throw Error("This command should not execute when the queue is not empty.");
142
138
  }
143
139
 
144
140
  const proof = mt.getProof(leaf_index);
145
141
 
146
- const n_response_elements = Math.min(
147
- Math.floor((255 - 32 - 1 - 1) / 32),
148
- proof.length
149
- );
142
+ const n_response_elements = Math.min(Math.floor((255 - 32 - 1 - 1) / 32), proof.length);
150
143
  const n_leftover_elements = proof.length - n_response_elements;
151
144
 
152
145
  // Add to the queue any proof elements that do not fit the response
@@ -196,9 +189,7 @@ export class GetMerkleLeafIndexCommand extends ClientCommand {
196
189
 
197
190
  const mt = this.known_trees.get(root_hash_hex);
198
191
  if (!mt) {
199
- throw Error(
200
- `Requested Merkle leaf index for unknown root: ${root_hash_hex}`
201
- );
192
+ throw Error(`Requested Merkle leaf index for unknown root: ${root_hash_hex}`);
202
193
  }
203
194
 
204
195
  let leaf_index = 0;
@@ -235,9 +226,9 @@ export class GetMoreElementsCommand extends ClientCommand {
235
226
 
236
227
  // all elements should have the same length
237
228
  const element_len = this.queue[0].length;
238
- if (this.queue.some((el) => el.length != element_len)) {
229
+ if (this.queue.some(el => el.length != element_len)) {
239
230
  throw new Error(
240
- "The queue contains elements with different byte length, which is not expected"
231
+ "The queue contains elements with different byte length, which is not expected",
241
232
  );
242
233
  }
243
234
 
@@ -309,7 +300,7 @@ export class ClientCommandInterpreter {
309
300
  const preimage = Buffer.concat([Buffer.from([0]), el]);
310
301
  this.addKnownPreimage(preimage);
311
302
  }
312
- const mt = new Merkle(elements.map((el) => hashLeaf(el)));
303
+ const mt = new Merkle(elements.map(el => hashLeaf(el)));
313
304
  this.roots.set(mt.getRoot().toString("hex"), mt);
314
305
  }
315
306
 
@@ -24,22 +24,14 @@ export class MerkelizedPsbt extends PsbtV2 {
24
24
  this.globalMerkleMap = MerkelizedPsbt.createMerkleMap(this.globalMap);
25
25
 
26
26
  for (let i = 0; i < this.getGlobalInputCount(); i++) {
27
- this.inputMerkleMaps.push(
28
- MerkelizedPsbt.createMerkleMap(this.inputMaps[i])
29
- );
27
+ this.inputMerkleMaps.push(MerkelizedPsbt.createMerkleMap(this.inputMaps[i]));
30
28
  }
31
- this.inputMapCommitments = [...this.inputMerkleMaps.values()].map((v) =>
32
- v.commitment()
33
- );
29
+ this.inputMapCommitments = [...this.inputMerkleMaps.values()].map(v => v.commitment());
34
30
 
35
31
  for (let i = 0; i < this.getGlobalOutputCount(); i++) {
36
- this.outputMerkleMaps.push(
37
- MerkelizedPsbt.createMerkleMap(this.outputMaps[i])
38
- );
32
+ this.outputMerkleMaps.push(MerkelizedPsbt.createMerkleMap(this.outputMaps[i]));
39
33
  }
40
- this.outputMapCommitments = [...this.outputMerkleMaps.values()].map((v) =>
41
- v.commitment()
42
- );
34
+ this.outputMapCommitments = [...this.outputMerkleMaps.values()].map(v => v.commitment());
43
35
  }
44
36
  // These public functions are for MerkelizedPsbt.
45
37
  getGlobalSize(): number {
@@ -51,14 +43,14 @@ export class MerkelizedPsbt extends PsbtV2 {
51
43
 
52
44
  private static createMerkleMap(map: Map<string, Buffer>): MerkleMap {
53
45
  const sortedKeysStrings = [...map.keys()].sort();
54
- const values = sortedKeysStrings.map((k) => {
46
+ const values = sortedKeysStrings.map(k => {
55
47
  const v = map.get(k);
56
48
  if (!v) {
57
49
  throw new Error("No value for key " + k);
58
50
  }
59
51
  return v;
60
52
  });
61
- const sortedKeys = sortedKeysStrings.map((k) => Buffer.from(k, "hex"));
53
+ const sortedKeys = sortedKeysStrings.map(k => Buffer.from(k, "hex"));
62
54
 
63
55
  const merkleMap = new MerkleMap(sortedKeys, values);
64
56
  return merkleMap;
@@ -10,10 +10,7 @@ export class Merkle {
10
10
  private rootNode: Node;
11
11
  private leafNodes: Node[];
12
12
  private h: (buf: Buffer) => Buffer;
13
- constructor(
14
- leaves: Buffer[],
15
- hasher: (buf: Buffer) => Buffer = crypto.sha256
16
- ) {
13
+ constructor(leaves: Buffer[], hasher: (buf: Buffer) => Buffer = crypto.sha256) {
17
14
  this.leaves = leaves;
18
15
  this.h = hasher;
19
16
  const nodes = this.calculateRoot(leaves);
@@ -68,16 +65,12 @@ export class Merkle {
68
65
 
69
66
  export function hashLeaf(
70
67
  buf: Buffer,
71
- hashFunction: (buf: Buffer) => Buffer = crypto.sha256
68
+ hashFunction: (buf: Buffer) => Buffer = crypto.sha256,
72
69
  ): Buffer {
73
70
  return hashConcat(Buffer.from([0]), buf, hashFunction);
74
71
  }
75
72
 
76
- function hashConcat(
77
- bufA: Buffer,
78
- bufB: Buffer,
79
- hashFunction: (buf: Buffer) => Buffer
80
- ): Buffer {
73
+ function hashConcat(bufA: Buffer, bufB: Buffer, hashFunction: (buf: Buffer) => Buffer): Buffer {
81
74
  return hashFunction(Buffer.concat([bufA, bufB]));
82
75
  }
83
76
 
@@ -33,9 +33,9 @@ export class MerkleMap {
33
33
  }
34
34
 
35
35
  this.keys = keys;
36
- this.keysTree = new Merkle(keys.map((k) => hashLeaf(k)));
36
+ this.keysTree = new Merkle(keys.map(k => hashLeaf(k)));
37
37
  this.values = values;
38
- this.valuesTree = new Merkle(values.map((v) => hashLeaf(v)));
38
+ this.valuesTree = new Merkle(values.map(v => hashLeaf(v)));
39
39
  }
40
40
 
41
41
  commitment(): Buffer {