@ledgerhq/live-common 21.16.2 → 21.17.1
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/__tests__/families/bitcoin/wallet-btc/utils.test.js +10 -7
- package/lib/__tests__/families/bitcoin/wallet-btc/utils.test.js.map +1 -1
- package/lib/families/bitcoin/bech32m.d.ts +22 -0
- package/lib/families/bitcoin/bech32m.d.ts.map +1 -0
- package/lib/families/bitcoin/bech32m.js +166 -0
- package/lib/families/bitcoin/bech32m.js.map +1 -0
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.d.ts.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js +11 -11
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoin.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.d.ts.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js +2 -1
- package/lib/families/bitcoin/wallet-btc/crypto/bitcoincash.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/digibyte.js +21 -2
- package/lib/families/bitcoin/wallet-btc/crypto/digibyte.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/litecoin.js +2 -2
- package/lib/families/bitcoin/wallet-btc/crypto/litecoin.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/zec.d.ts.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/zec.js +3 -2
- package/lib/families/bitcoin/wallet-btc/crypto/zec.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts +2 -1
- package/lib/families/bitcoin/wallet-btc/crypto/zen.d.ts.map +1 -1
- package/lib/families/bitcoin/wallet-btc/crypto/zen.js +7 -4
- package/lib/families/bitcoin/wallet-btc/crypto/zen.js.map +1 -1
- package/lib/families/bitcoin/wallet-btc/wallet.d.ts.map +1 -1
- package/lib/families/bitcoin/wallet-btc/wallet.js +3 -3
- package/lib/families/bitcoin/wallet-btc/wallet.js.map +1 -1
- package/lib/families/elrond/logic.js +22 -3
- package/lib/families/elrond/logic.js.map +1 -1
- package/package.json +14 -14
- package/src/__tests__/__snapshots__/all.libcore.ts.snap +7 -7
- package/src/__tests__/families/bitcoin/wallet-btc/utils.test.ts +4 -2
- package/src/currencies/__snapshots__/sortByMarketcap.test.ts.snap +65 -1324
- package/src/families/bitcoin/bech32m.ts +197 -0
- package/src/families/bitcoin/wallet-btc/crypto/bitcoin.ts +5 -4
- package/src/families/bitcoin/wallet-btc/crypto/bitcoincash.ts +2 -1
- package/src/families/bitcoin/wallet-btc/crypto/digibyte.ts +1 -1
- package/src/families/bitcoin/wallet-btc/crypto/litecoin.ts +1 -1
- package/src/families/bitcoin/wallet-btc/crypto/zec.ts +3 -2
- package/src/families/bitcoin/wallet-btc/crypto/zen.ts +10 -4
- package/src/families/bitcoin/wallet-btc/wallet.ts +1 -2
- package/src/families/elrond/logic.ts +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Ported from https://github.com/bitcoinjs/bech32/tree/605655d6b37a782345549cd1388db688a96ad56f
|
|
2
|
+
// until we can use bech32 2.0.0
|
|
3
|
+
// We can't directly use bech32 2.0.0 because many of our dependencies are still using bech32 ^1.1.3
|
|
4
|
+
// which conflict on mobile (root cause of https://ledgerhq.atlassian.net/browse/LL-7930)
|
|
5
|
+
// FIXME Remove that file and use bech32 2.0.0 when all dependencies also uses it.
|
|
6
|
+
|
|
7
|
+
const ENCODING_CONST = 0x2bc830a3;
|
|
8
|
+
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
|
9
|
+
const ALPHABET_MAP: { [key: string]: number } = {};
|
|
10
|
+
for (let z = 0; z < ALPHABET.length; z++) {
|
|
11
|
+
const x = ALPHABET.charAt(z);
|
|
12
|
+
ALPHABET_MAP[x] = z;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function prefixChk(prefix) {
|
|
16
|
+
let chk = 1;
|
|
17
|
+
for (let i = 0; i < prefix.length; ++i) {
|
|
18
|
+
const c = prefix.charCodeAt(i);
|
|
19
|
+
if (c < 33 || c > 126) return "Invalid prefix (" + prefix + ")";
|
|
20
|
+
chk = polymodStep(chk) ^ (c >> 5);
|
|
21
|
+
}
|
|
22
|
+
chk = polymodStep(chk);
|
|
23
|
+
for (let i = 0; i < prefix.length; ++i) {
|
|
24
|
+
const v = prefix.charCodeAt(i);
|
|
25
|
+
chk = polymodStep(chk) ^ (v & 0x1f);
|
|
26
|
+
}
|
|
27
|
+
return chk;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function polymodStep(pre: number): number {
|
|
31
|
+
const b = pre >> 25;
|
|
32
|
+
return (
|
|
33
|
+
((pre & 0x1ffffff) << 5) ^
|
|
34
|
+
(-((b >> 0) & 1) & 0x3b6a57b2) ^
|
|
35
|
+
(-((b >> 1) & 1) & 0x26508e6d) ^
|
|
36
|
+
(-((b >> 2) & 1) & 0x1ea119fa) ^
|
|
37
|
+
(-((b >> 3) & 1) & 0x3d4233dd) ^
|
|
38
|
+
(-((b >> 4) & 1) & 0x2a1462b3)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function encode(
|
|
43
|
+
prefix: string,
|
|
44
|
+
words: ArrayLike<number>,
|
|
45
|
+
LIMIT?: number
|
|
46
|
+
): string {
|
|
47
|
+
LIMIT = LIMIT || 90;
|
|
48
|
+
if (prefix.length + 7 + words.length > LIMIT)
|
|
49
|
+
throw new TypeError("Exceeds length limit");
|
|
50
|
+
|
|
51
|
+
prefix = prefix.toLowerCase();
|
|
52
|
+
|
|
53
|
+
// determine chk mod
|
|
54
|
+
let chk = prefixChk(prefix);
|
|
55
|
+
if (typeof chk === "string") throw new Error(chk);
|
|
56
|
+
|
|
57
|
+
let result = prefix + "1";
|
|
58
|
+
for (let i = 0; i < words.length; ++i) {
|
|
59
|
+
const x = words[i];
|
|
60
|
+
if (x >> 5 !== 0) throw new Error("Non 5-bit word");
|
|
61
|
+
|
|
62
|
+
chk = polymodStep(chk) ^ x;
|
|
63
|
+
result += ALPHABET.charAt(x);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (let i = 0; i < 6; ++i) {
|
|
67
|
+
chk = polymodStep(chk);
|
|
68
|
+
}
|
|
69
|
+
chk ^= ENCODING_CONST;
|
|
70
|
+
|
|
71
|
+
for (let i = 0; i < 6; ++i) {
|
|
72
|
+
const v = (chk >> ((5 - i) * 5)) & 0x1f;
|
|
73
|
+
result += ALPHABET.charAt(v);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function __decode(str: string, LIMIT?: number) {
|
|
80
|
+
LIMIT = LIMIT || 90;
|
|
81
|
+
if (str.length < 8) return str + " too short";
|
|
82
|
+
if (str.length > LIMIT) return "Exceeds length limit";
|
|
83
|
+
|
|
84
|
+
// don't allow mixed case
|
|
85
|
+
const lowered = str.toLowerCase();
|
|
86
|
+
const uppered = str.toUpperCase();
|
|
87
|
+
if (str !== lowered && str !== uppered) return "Mixed-case string " + str;
|
|
88
|
+
str = lowered;
|
|
89
|
+
|
|
90
|
+
const split = str.lastIndexOf("1");
|
|
91
|
+
if (split === -1) return "No separator character for " + str;
|
|
92
|
+
if (split === 0) return "Missing prefix for " + str;
|
|
93
|
+
|
|
94
|
+
const prefix = str.slice(0, split);
|
|
95
|
+
const wordChars = str.slice(split + 1);
|
|
96
|
+
if (wordChars.length < 6) return "Data too short";
|
|
97
|
+
|
|
98
|
+
let chk = prefixChk(prefix);
|
|
99
|
+
if (typeof chk === "string") return chk;
|
|
100
|
+
|
|
101
|
+
const words: number[] = [];
|
|
102
|
+
for (let i = 0; i < wordChars.length; ++i) {
|
|
103
|
+
const c = wordChars.charAt(i);
|
|
104
|
+
const v = ALPHABET_MAP[c];
|
|
105
|
+
if (v === undefined) return "Unknown character " + c;
|
|
106
|
+
chk = polymodStep(chk) ^ v;
|
|
107
|
+
|
|
108
|
+
// not in the checksum?
|
|
109
|
+
if (i + 6 >= wordChars.length) continue;
|
|
110
|
+
words.push(v);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (chk !== ENCODING_CONST) return "Invalid checksum for " + str;
|
|
114
|
+
return { prefix, words };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function decodeUnsafe(str: string, LIMIT?: number) {
|
|
118
|
+
const res = __decode(str, LIMIT);
|
|
119
|
+
if (typeof res === "object") return res;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function decode(str: string, LIMIT?: number) {
|
|
123
|
+
const res = __decode(str, LIMIT);
|
|
124
|
+
if (typeof res === "object") return res;
|
|
125
|
+
|
|
126
|
+
throw new Error(res);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function convert(
|
|
130
|
+
data: ArrayLike<number>,
|
|
131
|
+
inBits: number,
|
|
132
|
+
outBits: number,
|
|
133
|
+
pad: true
|
|
134
|
+
): number[];
|
|
135
|
+
function convert(
|
|
136
|
+
data: ArrayLike<number>,
|
|
137
|
+
inBits: number,
|
|
138
|
+
outBits: number,
|
|
139
|
+
pad: false
|
|
140
|
+
): number[] | string;
|
|
141
|
+
function convert(
|
|
142
|
+
data: ArrayLike<number>,
|
|
143
|
+
inBits: number,
|
|
144
|
+
outBits: number,
|
|
145
|
+
pad: boolean
|
|
146
|
+
): number[] | string {
|
|
147
|
+
let value = 0;
|
|
148
|
+
let bits = 0;
|
|
149
|
+
const maxV = (1 << outBits) - 1;
|
|
150
|
+
|
|
151
|
+
const result: number[] = [];
|
|
152
|
+
for (let i = 0; i < data.length; ++i) {
|
|
153
|
+
value = (value << inBits) | data[i];
|
|
154
|
+
bits += inBits;
|
|
155
|
+
|
|
156
|
+
while (bits >= outBits) {
|
|
157
|
+
bits -= outBits;
|
|
158
|
+
result.push((value >> bits) & maxV);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (pad) {
|
|
163
|
+
if (bits > 0) {
|
|
164
|
+
result.push((value << (outBits - bits)) & maxV);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
if (bits >= inBits) return "Excess padding";
|
|
168
|
+
if ((value << (outBits - bits)) & maxV) return "Non-zero padding";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function toWords(bytes: ArrayLike<number>): number[] {
|
|
175
|
+
return convert(bytes, 8, 5, true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function fromWordsUnsafe(words: ArrayLike<number>): number[] | undefined {
|
|
179
|
+
const res = convert(words, 5, 8, false);
|
|
180
|
+
if (Array.isArray(res)) return res;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function fromWords(words: ArrayLike<number>): number[] {
|
|
184
|
+
const res = convert(words, 5, 8, false);
|
|
185
|
+
if (Array.isArray(res)) return res;
|
|
186
|
+
|
|
187
|
+
throw new Error(res);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export const bech32m = {
|
|
191
|
+
decodeUnsafe,
|
|
192
|
+
decode,
|
|
193
|
+
encode,
|
|
194
|
+
toWords,
|
|
195
|
+
fromWordsUnsafe,
|
|
196
|
+
fromWords,
|
|
197
|
+
};
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// from https://github.com/LedgerHQ/xpub-scan/blob/master/src/actions/deriveAddresses.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
|
|
3
|
+
import * as bech32 from "bech32";
|
|
4
|
+
import { bech32m } from "../../bech32m";
|
|
5
5
|
import * as bjs from "bitcoinjs-lib";
|
|
6
6
|
import { publicKeyTweakAdd } from "secp256k1";
|
|
7
|
+
import { InvalidAddress } from "@ledgerhq/errors";
|
|
7
8
|
import { DerivationModes } from "../types";
|
|
8
9
|
import Base from "./base";
|
|
9
10
|
|
|
@@ -87,7 +88,7 @@ class Bitcoin extends Base {
|
|
|
87
88
|
// Make sure the address is valid on this network
|
|
88
89
|
// otherwise we can't call toOutputScriptTemporary.
|
|
89
90
|
if (!this.validateAddress(address)) {
|
|
90
|
-
throw new
|
|
91
|
+
throw new InvalidAddress();
|
|
91
92
|
}
|
|
92
93
|
// bitcoinjs-lib/src/address doesn't yet have released support for bech32m,
|
|
93
94
|
// so we'll implement our own version of toOutputScript while waiting.
|
|
@@ -5,6 +5,7 @@ import bchaddr from "bchaddrjs";
|
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
6
|
// @ts-ignore
|
|
7
7
|
import { toOutputScript } from "bitcoinjs-lib/src/address";
|
|
8
|
+
import { InvalidAddress } from "@ledgerhq/errors";
|
|
8
9
|
import { DerivationModes } from "../types";
|
|
9
10
|
import { ICrypto } from "./types";
|
|
10
11
|
|
|
@@ -60,7 +61,7 @@ class BitcoinCash implements ICrypto {
|
|
|
60
61
|
|
|
61
62
|
toOutputScript(address: string) {
|
|
62
63
|
if (!this.validateAddress(address)) {
|
|
63
|
-
throw new
|
|
64
|
+
throw new InvalidAddress();
|
|
64
65
|
}
|
|
65
66
|
// TODO find a better way to calculate the script from bch address instead of converting to bitcoin address
|
|
66
67
|
return toOutputScript(bchaddr.toLegacyAddress(address), this.network);
|
|
@@ -3,7 +3,7 @@ import * as bip32 from "bip32";
|
|
|
3
3
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
4
4
|
// @ts-ignore
|
|
5
5
|
import { toOutputScript } from "bitcoinjs-lib/src/address";
|
|
6
|
-
import
|
|
6
|
+
import * as bech32 from "bech32";
|
|
7
7
|
import { DerivationModes } from "../types";
|
|
8
8
|
import { ICrypto } from "./types";
|
|
9
9
|
import Base from "./base";
|
|
@@ -5,9 +5,10 @@ import { toOutputScript } from "bitcoinjs-lib/src/address";
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
import zec from "zcash-bitcore-lib";
|
|
7
7
|
import bs58check from "bs58check";
|
|
8
|
+
import coininfo from "coininfo";
|
|
9
|
+
import { InvalidAddress } from "@ledgerhq/errors";
|
|
8
10
|
import { DerivationModes } from "../types";
|
|
9
11
|
import { ICrypto } from "./types";
|
|
10
|
-
import coininfo from "coininfo";
|
|
11
12
|
|
|
12
13
|
class ZCash implements ICrypto {
|
|
13
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -61,7 +62,7 @@ class ZCash implements ICrypto {
|
|
|
61
62
|
|
|
62
63
|
toOutputScript(address: string) {
|
|
63
64
|
if (!this.validateAddress(address)) {
|
|
64
|
-
throw new
|
|
65
|
+
throw new InvalidAddress();
|
|
65
66
|
}
|
|
66
67
|
// TODO find a better way to calculate the script from zec address instead of converting to bitcoin address
|
|
67
68
|
return toOutputScript(
|
|
@@ -5,9 +5,10 @@ import { toOutputScript } from "bitcoinjs-lib/src/address";
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
import zec from "zcash-bitcore-lib";
|
|
7
7
|
import bs58check from "bs58check";
|
|
8
|
+
import coininfo from "coininfo";
|
|
9
|
+
import { InvalidAddress } from "@ledgerhq/errors";
|
|
8
10
|
import { DerivationModes } from "../types";
|
|
9
11
|
import { ICrypto } from "./types";
|
|
10
|
-
import coininfo from "coininfo";
|
|
11
12
|
|
|
12
13
|
class Zen implements ICrypto {
|
|
13
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -81,13 +82,18 @@ class Zen implements ICrypto {
|
|
|
81
82
|
|
|
82
83
|
toOutputScript(address: string) {
|
|
83
84
|
if (!this.validateAddress(address)) {
|
|
84
|
-
throw new
|
|
85
|
+
throw new InvalidAddress();
|
|
85
86
|
}
|
|
86
|
-
|
|
87
|
-
return toOutputScript(
|
|
87
|
+
const outputScript = toOutputScript(
|
|
88
88
|
Zen.toBitcoinAddr(address),
|
|
89
89
|
coininfo.bitcoin.main.toBitcoinJS()
|
|
90
90
|
);
|
|
91
|
+
// refer to https://github.com/LedgerHQ/lib-ledger-core/blob/fc9d762b83fc2b269d072b662065747a64ab2816/core/src/wallet/bitcoin/scripts/BitcoinLikeScript.cpp#L139 and https://github.com/LedgerHQ/lib-ledger-core/blob/fc9d762b83fc2b269d072b662065747a64ab2816/core/src/wallet/bitcoin/networks.cpp#L39 for bip115 Script and its network parameters
|
|
92
|
+
const bip115Script = Buffer.from(
|
|
93
|
+
"209ec9845acb02fab24e1c0368b3b517c1a4488fba97f0e3459ac053ea0100000003c01f02b4",
|
|
94
|
+
"hex"
|
|
95
|
+
);
|
|
96
|
+
return Buffer.concat([outputScript, bip115Script]);
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
// eslint-disable-next-line class-methods-use-this
|
|
@@ -227,7 +227,6 @@ class BitcoinLikeWallet {
|
|
|
227
227
|
btc,
|
|
228
228
|
fromAccount,
|
|
229
229
|
txInfo,
|
|
230
|
-
segwit,
|
|
231
230
|
additionals,
|
|
232
231
|
hasExtraData,
|
|
233
232
|
onDeviceSignatureRequested,
|
|
@@ -260,7 +259,7 @@ class BitcoinLikeWallet {
|
|
|
260
259
|
const inputs: Inputs = txInfo.inputs.map((i) => {
|
|
261
260
|
log("hw", `splitTransaction`, {
|
|
262
261
|
transactionHex: i.txHex,
|
|
263
|
-
isSegwitSupported:
|
|
262
|
+
isSegwitSupported: true,
|
|
264
263
|
hasTimestamp: false,
|
|
265
264
|
hasExtraData,
|
|
266
265
|
additionals,
|