@qrkit/core 0.3.2 → 0.4.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/README.md +86 -26
- package/dist/index.cjs +304 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -4
- package/dist/index.d.ts +46 -4
- package/dist/index.js +297 -61
- package/dist/index.js.map +1 -1
- package/package.json +8 -4
package/dist/index.js
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
+
// src/btc/address.ts
|
|
2
|
+
import { bech32, createBase58check } from "@scure/base";
|
|
3
|
+
import { ripemd160 } from "@noble/hashes/legacy.js";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
var base58check = createBase58check(sha256);
|
|
6
|
+
function hash160(bytes) {
|
|
7
|
+
return ripemd160(sha256(bytes));
|
|
8
|
+
}
|
|
9
|
+
function pubKeyToP2wpkh(compressedPubKey) {
|
|
10
|
+
const h = hash160(compressedPubKey);
|
|
11
|
+
const words = bech32.toWords(h);
|
|
12
|
+
return bech32.encode("bc", [0, ...words]);
|
|
13
|
+
}
|
|
14
|
+
function pubKeyToP2shP2wpkh(compressedPubKey) {
|
|
15
|
+
const h = hash160(compressedPubKey);
|
|
16
|
+
const redeemScript = new Uint8Array(22);
|
|
17
|
+
redeemScript[0] = 0;
|
|
18
|
+
redeemScript[1] = 20;
|
|
19
|
+
redeemScript.set(h, 2);
|
|
20
|
+
const scriptHash = hash160(redeemScript);
|
|
21
|
+
const payload = new Uint8Array(21);
|
|
22
|
+
payload[0] = 5;
|
|
23
|
+
payload.set(scriptHash, 1);
|
|
24
|
+
return base58check.encode(payload);
|
|
25
|
+
}
|
|
26
|
+
function pubKeyToP2pkh(compressedPubKey) {
|
|
27
|
+
const h = hash160(compressedPubKey);
|
|
28
|
+
const payload = new Uint8Array(21);
|
|
29
|
+
payload[0] = 0;
|
|
30
|
+
payload.set(h, 1);
|
|
31
|
+
return base58check.encode(payload);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/bytes.ts
|
|
35
|
+
function bytesToBase64(bytes) {
|
|
36
|
+
let binary = "";
|
|
37
|
+
for (const byte of bytes) {
|
|
38
|
+
binary += String.fromCharCode(byte);
|
|
39
|
+
}
|
|
40
|
+
return btoa(binary);
|
|
41
|
+
}
|
|
42
|
+
function bytesToHex(bytes) {
|
|
43
|
+
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
44
|
+
}
|
|
45
|
+
function hexToBytes(hex) {
|
|
46
|
+
const normalized = hex.trim().replace(/^0x/i, "");
|
|
47
|
+
if (normalized.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(normalized)) {
|
|
48
|
+
throw new Error("Invalid hex string");
|
|
49
|
+
}
|
|
50
|
+
return new Uint8Array(
|
|
51
|
+
normalized.match(/.{2}/g)?.map((byte) => parseInt(byte, 16)) ?? []
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/btc/deriveAccount.ts
|
|
56
|
+
function firstChild(accountKey) {
|
|
57
|
+
return accountKey.deriveChild(0).deriveChild(0);
|
|
58
|
+
}
|
|
59
|
+
function scriptTypeFromPurpose(purpose) {
|
|
60
|
+
if (purpose === 84) return "p2wpkh";
|
|
61
|
+
if (purpose === 49) return "p2sh-p2wpkh";
|
|
62
|
+
if (purpose === 44) return "p2pkh";
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
function deriveAddress(pubKey, scriptType) {
|
|
66
|
+
if (scriptType === "p2wpkh") return pubKeyToP2wpkh(pubKey);
|
|
67
|
+
if (scriptType === "p2sh-p2wpkh") return pubKeyToP2shP2wpkh(pubKey);
|
|
68
|
+
return pubKeyToP2pkh(pubKey);
|
|
69
|
+
}
|
|
70
|
+
function deriveBtcAccount(parsed) {
|
|
71
|
+
const results = [];
|
|
72
|
+
for (const entry of parsed) {
|
|
73
|
+
const { hdKey, purpose, coinType, sourceFingerprint, name } = entry;
|
|
74
|
+
if (coinType !== 0) continue;
|
|
75
|
+
const scriptType = scriptTypeFromPurpose(purpose);
|
|
76
|
+
if (!scriptType) continue;
|
|
77
|
+
const child = firstChild(hdKey);
|
|
78
|
+
if (!child.publicKey) continue;
|
|
79
|
+
results.push({
|
|
80
|
+
address: deriveAddress(child.publicKey, scriptType),
|
|
81
|
+
scriptType,
|
|
82
|
+
publicKey: bytesToHex(child.publicKey),
|
|
83
|
+
sourceFingerprint,
|
|
84
|
+
device: name
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return results;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/eth/address.ts
|
|
91
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
92
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
93
|
+
function pubKeyToEthAddress(compressedPubKey) {
|
|
94
|
+
const pubKeyHex = [...compressedPubKey].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
95
|
+
const uncompressed = secp256k1.Point.fromHex(pubKeyHex).toBytes(false);
|
|
96
|
+
const hash = keccak_256(uncompressed.slice(1));
|
|
97
|
+
const hex = [...hash.slice(12)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
98
|
+
return toChecksumAddress(hex);
|
|
99
|
+
}
|
|
100
|
+
function toChecksumAddress(hex) {
|
|
101
|
+
const checksumHash = keccak_256(new TextEncoder().encode(hex));
|
|
102
|
+
return "0x" + [...hex].map((c, i) => {
|
|
103
|
+
if (c >= "0" && c <= "9") return c;
|
|
104
|
+
const nibble = i % 2 === 0 ? checksumHash[Math.floor(i / 2)] >> 4 & 15 : checksumHash[Math.floor(i / 2)] & 15;
|
|
105
|
+
return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();
|
|
106
|
+
}).join("");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/eth/deriveAccount.ts
|
|
110
|
+
function firstChild2(accountKey) {
|
|
111
|
+
return accountKey.deriveChild(0).deriveChild(0);
|
|
112
|
+
}
|
|
113
|
+
function deriveEvmAccount(parsed) {
|
|
114
|
+
const results = [];
|
|
115
|
+
for (const entry of parsed) {
|
|
116
|
+
const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;
|
|
117
|
+
const isEvm = purpose === 44 && coinType === 60 || purpose === void 0 && type === "xpub";
|
|
118
|
+
if (!isEvm) continue;
|
|
119
|
+
const child = firstChild2(hdKey);
|
|
120
|
+
if (!child.publicKey) continue;
|
|
121
|
+
results.push({
|
|
122
|
+
address: pubKeyToEthAddress(child.publicKey),
|
|
123
|
+
publicKey: bytesToHex(child.publicKey),
|
|
124
|
+
sourceFingerprint,
|
|
125
|
+
device: name
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return results;
|
|
129
|
+
}
|
|
130
|
+
|
|
1
131
|
// src/parseXpub.ts
|
|
2
132
|
import { HDKey } from "@scure/bip32";
|
|
3
133
|
import { decode as cborDecode } from "cborg";
|
|
@@ -5,23 +135,46 @@ function get(m, k) {
|
|
|
5
135
|
if (m instanceof Map) return m.get(k);
|
|
6
136
|
return void 0;
|
|
7
137
|
}
|
|
138
|
+
function bytesFromCbor(value) {
|
|
139
|
+
if (value instanceof Uint8Array) return value;
|
|
140
|
+
if (value instanceof Map && value.get("type") === "Buffer") {
|
|
141
|
+
const data = value.get("data");
|
|
142
|
+
if (Array.isArray(data) && data.every((byte) => Number.isInteger(byte) && byte >= 0 && byte <= 255)) {
|
|
143
|
+
return new Uint8Array(data);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
8
148
|
var passthrough = (v) => v;
|
|
9
149
|
function decodeCbor(cbor) {
|
|
150
|
+
const tags = new Proxy([], {
|
|
151
|
+
get(target, property, receiver) {
|
|
152
|
+
if (typeof property === "string" && /^\d+$/.test(property)) {
|
|
153
|
+
return passthrough;
|
|
154
|
+
}
|
|
155
|
+
return Reflect.get(target, property, receiver);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
10
158
|
return cborDecode(cbor, {
|
|
11
159
|
useMaps: true,
|
|
12
|
-
tags
|
|
13
|
-
303: passthrough,
|
|
14
|
-
// crypto-hdkey
|
|
15
|
-
304: passthrough,
|
|
16
|
-
// crypto-keypath
|
|
17
|
-
305: passthrough
|
|
18
|
-
// crypto-coin-info
|
|
19
|
-
})
|
|
160
|
+
tags
|
|
20
161
|
});
|
|
21
162
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
163
|
+
function isCborMap(value) {
|
|
164
|
+
return value instanceof Map;
|
|
165
|
+
}
|
|
166
|
+
function assertCryptoHdKeyShape(value) {
|
|
167
|
+
if (!isCborMap(value)) {
|
|
168
|
+
throw new Error("crypto-hdkey entry must be a CBOR map");
|
|
169
|
+
}
|
|
170
|
+
if (!value.has(3) || !value.has(4)) {
|
|
171
|
+
throw new Error("crypto-hdkey missing key-data or chain-code");
|
|
172
|
+
}
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
function parseCryptoHdKey(map, raw, fallbackName) {
|
|
176
|
+
const keyData = bytesFromCbor(get(map, 3));
|
|
177
|
+
const chainCode = bytesFromCbor(get(map, 4));
|
|
25
178
|
if (!keyData || !chainCode) {
|
|
26
179
|
throw new Error("crypto-hdkey missing key-data or chain-code");
|
|
27
180
|
}
|
|
@@ -37,25 +190,28 @@ function parseCryptoHdKey(map, raw) {
|
|
|
37
190
|
}
|
|
38
191
|
sourceFingerprint = get(origin, 2);
|
|
39
192
|
}
|
|
40
|
-
const name = get(map, 9);
|
|
193
|
+
const name = get(map, 9) ?? fallbackName;
|
|
41
194
|
const hdKey = new HDKey({ publicKey: keyData, chainCode });
|
|
42
195
|
return { hdKey, type: "xpub", purpose, coinType, sourceFingerprint, name, raw };
|
|
43
196
|
}
|
|
44
197
|
function parseScannedUR(scanned) {
|
|
45
198
|
const { type, cbor } = scanned;
|
|
46
199
|
const raw = `ur:${type}`;
|
|
47
|
-
if (type !== "crypto-hdkey" && type !== "crypto-account") {
|
|
200
|
+
if (type !== "crypto-hdkey" && type !== "crypto-account" && type !== "crypto-multi-accounts") {
|
|
48
201
|
throw new Error(`Unsupported UR type: ${type}`);
|
|
49
202
|
}
|
|
50
203
|
const map = decodeCbor(cbor);
|
|
51
204
|
if (type === "crypto-hdkey") {
|
|
52
|
-
return parseCryptoHdKey(map, raw);
|
|
205
|
+
return parseCryptoHdKey(assertCryptoHdKeyShape(map), raw);
|
|
53
206
|
}
|
|
54
207
|
const accounts = map.get(2);
|
|
55
208
|
if (!Array.isArray(accounts) || accounts.length === 0) {
|
|
56
|
-
throw new Error(
|
|
209
|
+
throw new Error(`${type} contains no keys`);
|
|
57
210
|
}
|
|
58
|
-
|
|
211
|
+
const fallbackName = type === "crypto-multi-accounts" ? map.get(3) : void 0;
|
|
212
|
+
return accounts.map(
|
|
213
|
+
(entry) => parseCryptoHdKey(assertCryptoHdKeyShape(entry), raw, fallbackName)
|
|
214
|
+
);
|
|
59
215
|
}
|
|
60
216
|
function parseXpub(input) {
|
|
61
217
|
if (typeof input !== "string") {
|
|
@@ -75,48 +231,6 @@ function parseXpub(input) {
|
|
|
75
231
|
];
|
|
76
232
|
}
|
|
77
233
|
|
|
78
|
-
// src/eth/address.ts
|
|
79
|
-
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
80
|
-
import * as secp from "@noble/secp256k1";
|
|
81
|
-
function pubKeyToEthAddress(compressedPubKey) {
|
|
82
|
-
const uncompressed = secp.Point.fromBytes(compressedPubKey).toBytes(false);
|
|
83
|
-
const hash = keccak_256(uncompressed.slice(1));
|
|
84
|
-
const hex = [...hash.slice(12)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
85
|
-
return toChecksumAddress(hex);
|
|
86
|
-
}
|
|
87
|
-
function toChecksumAddress(hex) {
|
|
88
|
-
const checksumHash = keccak_256(new TextEncoder().encode(hex));
|
|
89
|
-
return "0x" + [...hex].map((c, i) => {
|
|
90
|
-
if (c >= "0" && c <= "9") return c;
|
|
91
|
-
const nibble = i % 2 === 0 ? checksumHash[Math.floor(i / 2)] >> 4 & 15 : checksumHash[Math.floor(i / 2)] & 15;
|
|
92
|
-
return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();
|
|
93
|
-
}).join("");
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// src/eth/deriveAccount.ts
|
|
97
|
-
function firstChild(accountKey) {
|
|
98
|
-
return accountKey.deriveChild(0).deriveChild(0);
|
|
99
|
-
}
|
|
100
|
-
function toHex(bytes) {
|
|
101
|
-
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
102
|
-
}
|
|
103
|
-
function deriveEvmAccount(parsed) {
|
|
104
|
-
for (const entry of parsed) {
|
|
105
|
-
const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;
|
|
106
|
-
const isEvm = purpose === 44 && coinType === 60 || purpose === void 0 && type === "xpub";
|
|
107
|
-
if (!isEvm) continue;
|
|
108
|
-
const child = firstChild(hdKey);
|
|
109
|
-
if (!child.publicKey) continue;
|
|
110
|
-
return {
|
|
111
|
-
address: pubKeyToEthAddress(child.publicKey),
|
|
112
|
-
publicKey: toHex(child.publicKey),
|
|
113
|
-
sourceFingerprint,
|
|
114
|
-
device: name
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
return void 0;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
234
|
// src/parseConnection.ts
|
|
121
235
|
var ALL_CHAINS = ["evm", "btc"];
|
|
122
236
|
function parseConnection(scannedUR, config = {}) {
|
|
@@ -124,11 +238,15 @@ function parseConnection(scannedUR, config = {}) {
|
|
|
124
238
|
const parsed = parseXpub(scannedUR);
|
|
125
239
|
const accounts = [];
|
|
126
240
|
if (chains.includes("evm")) {
|
|
127
|
-
const account
|
|
128
|
-
if (account) {
|
|
241
|
+
for (const account of deriveEvmAccount(parsed)) {
|
|
129
242
|
accounts.push({ chain: "evm", ...account });
|
|
130
243
|
}
|
|
131
244
|
}
|
|
245
|
+
if (chains.includes("btc")) {
|
|
246
|
+
for (const account of deriveBtcAccount(parsed)) {
|
|
247
|
+
accounts.push({ chain: "btc", ...account });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
132
250
|
return accounts;
|
|
133
251
|
}
|
|
134
252
|
|
|
@@ -265,13 +383,131 @@ function parseEthSignature(scanned) {
|
|
|
265
383
|
if (!sigBytes || sigBytes.length < 64) {
|
|
266
384
|
throw new Error("Invalid or missing signature bytes");
|
|
267
385
|
}
|
|
268
|
-
return
|
|
386
|
+
return `0x${bytesToHex(sigBytes)}`;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// src/btc/signRequest.ts
|
|
390
|
+
var BtcDataType = {
|
|
391
|
+
Message: 1
|
|
392
|
+
};
|
|
393
|
+
var TAG_KEYPATH2 = 304;
|
|
394
|
+
var PURPOSE_BY_SCRIPT_TYPE = {
|
|
395
|
+
p2wpkh: 84,
|
|
396
|
+
"p2sh-p2wpkh": 49,
|
|
397
|
+
p2pkh: 44
|
|
398
|
+
};
|
|
399
|
+
function randomBytes2(n) {
|
|
400
|
+
const buf = new Uint8Array(n);
|
|
401
|
+
crypto.getRandomValues(buf);
|
|
402
|
+
return buf;
|
|
403
|
+
}
|
|
404
|
+
function purposeFromScriptType(scriptType) {
|
|
405
|
+
return PURPOSE_BY_SCRIPT_TYPE[scriptType];
|
|
406
|
+
}
|
|
407
|
+
function buildKeypath2(scriptType, sourceFingerprint) {
|
|
408
|
+
const purpose = purposeFromScriptType(scriptType);
|
|
409
|
+
const components = [purpose, true, 0, true, 0, true, 0, false, 0, false];
|
|
410
|
+
const keypathMap = /* @__PURE__ */ new Map([[1, components]]);
|
|
411
|
+
if (sourceFingerprint !== void 0) {
|
|
412
|
+
keypathMap.set(2, sourceFingerprint);
|
|
413
|
+
}
|
|
414
|
+
return new CborTag(TAG_KEYPATH2, keypathMap);
|
|
415
|
+
}
|
|
416
|
+
function buildBtcSignRequestCbor(params) {
|
|
417
|
+
const { signData, address, scriptType, sourceFingerprint, origin = "qrkit" } = params;
|
|
418
|
+
const requestId = randomBytes2(16);
|
|
419
|
+
const signBytes = typeof signData === "string" ? new TextEncoder().encode(signData) : signData;
|
|
420
|
+
const keypath = buildKeypath2(scriptType, sourceFingerprint);
|
|
421
|
+
return encode(
|
|
422
|
+
/* @__PURE__ */ new Map([
|
|
423
|
+
[1, new CborTag(37, requestId)],
|
|
424
|
+
// request-id: uuid = #6.37(bstr)
|
|
425
|
+
[2, signBytes],
|
|
426
|
+
// sign-data
|
|
427
|
+
[3, BtcDataType.Message],
|
|
428
|
+
// data-type
|
|
429
|
+
[4, [keypath]],
|
|
430
|
+
// btc-derivation-paths
|
|
431
|
+
[5, [address]],
|
|
432
|
+
// btc-addresses
|
|
433
|
+
[6, origin]
|
|
434
|
+
// origin
|
|
435
|
+
])
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
function buildBtcSignRequestURParts(params) {
|
|
439
|
+
return encodeURParts(buildBtcSignRequestCbor(params), "btc-sign-request");
|
|
440
|
+
}
|
|
441
|
+
function buildBtcSignRequestUR(params) {
|
|
442
|
+
return encodeURParts(buildBtcSignRequestCbor(params), "btc-sign-request")[0];
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// src/btc/signature.ts
|
|
446
|
+
import { decode as cborDecode3 } from "cborg";
|
|
447
|
+
function parseBtcSignature(scanned) {
|
|
448
|
+
if (scanned.type !== "btc-signature") {
|
|
449
|
+
throw new Error(`Expected btc-signature, got: ${scanned.type}`);
|
|
450
|
+
}
|
|
451
|
+
const map = cborDecode3(scanned.cbor, {
|
|
452
|
+
useMaps: true,
|
|
453
|
+
tags: Object.assign([], { 37: (v) => v })
|
|
454
|
+
});
|
|
455
|
+
const requestId = map.get(1);
|
|
456
|
+
const sigBytes = map.get(2);
|
|
457
|
+
const publicKeyBytes = map.get(3);
|
|
458
|
+
if (!sigBytes || sigBytes.length !== 65) {
|
|
459
|
+
throw new Error("Invalid or missing BTC signature bytes");
|
|
460
|
+
}
|
|
461
|
+
if (!publicKeyBytes || publicKeyBytes.length !== 33) {
|
|
462
|
+
throw new Error("Invalid or missing BTC public key bytes");
|
|
463
|
+
}
|
|
464
|
+
return {
|
|
465
|
+
signature: bytesToBase64(sigBytes),
|
|
466
|
+
publicKey: bytesToHex(publicKeyBytes),
|
|
467
|
+
requestId: requestId ? bytesToHex(requestId) : void 0
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// src/btc/psbt.ts
|
|
472
|
+
import { decode as cborDecode4 } from "cborg";
|
|
473
|
+
function normalizePsbt(psbt) {
|
|
474
|
+
try {
|
|
475
|
+
return typeof psbt === "string" ? hexToBytes(psbt) : psbt;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
throw new Error("Invalid PSBT hex", { cause: error });
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function parseCryptoPsbt(scanned) {
|
|
481
|
+
if (scanned.type !== "crypto-psbt") {
|
|
482
|
+
throw new Error(`Expected crypto-psbt, got: ${scanned.type}`);
|
|
483
|
+
}
|
|
484
|
+
const psbt = cborDecode4(scanned.cbor);
|
|
485
|
+
if (!(psbt instanceof Uint8Array)) {
|
|
486
|
+
throw new Error("Invalid crypto-psbt payload");
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
psbt,
|
|
490
|
+
psbtHex: bytesToHex(psbt)
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
function buildCryptoPsbtURParts(psbt) {
|
|
494
|
+
return encodeURParts(encode(normalizePsbt(psbt)), "crypto-psbt");
|
|
495
|
+
}
|
|
496
|
+
function buildCryptoPsbtUR(psbt) {
|
|
497
|
+
return buildCryptoPsbtURParts(psbt)[0];
|
|
269
498
|
}
|
|
270
499
|
export {
|
|
500
|
+
BtcDataType,
|
|
271
501
|
EthDataType,
|
|
502
|
+
buildBtcSignRequestUR,
|
|
503
|
+
buildBtcSignRequestURParts,
|
|
504
|
+
buildCryptoPsbtUR,
|
|
505
|
+
buildCryptoPsbtURParts,
|
|
272
506
|
buildEthSignRequestUR,
|
|
273
507
|
buildEthSignRequestURParts,
|
|
508
|
+
parseBtcSignature,
|
|
274
509
|
parseConnection,
|
|
510
|
+
parseCryptoPsbt,
|
|
275
511
|
parseEthSignature
|
|
276
512
|
};
|
|
277
513
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/parseXpub.ts","../src/eth/address.ts","../src/eth/deriveAccount.ts","../src/parseConnection.ts","../src/cbor.ts","../src/urEncoding.ts","../src/eth/signRequest.ts","../src/eth/signature.ts"],"sourcesContent":["import { HDKey } from \"@scure/bip32\";\nimport { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"./types.js\";\n\nexport type XpubType = \"xpub\";\n\nexport interface ParsedXpub {\n hdKey: HDKey;\n type: XpubType;\n /** BIP-44 purpose index: 44 for EVM */\n purpose: number | undefined;\n /** BIP-44 coin type: 60 = ETH */\n coinType: number | undefined;\n /** source-fingerprint from the origin keypath — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name from crypto-hdkey key 9, set by some wallets (e.g. Keystone) */\n name?: string;\n raw: string;\n}\n\nfunction get(m: unknown, k: number): unknown {\n if (m instanceof Map) return (m as Map<number, unknown>).get(k);\n return undefined;\n}\n\nconst passthrough = (v: unknown) => v;\n\nfunction decodeCbor(cbor: Uint8Array): Map<number, unknown> {\n return cborDecode(cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], {\n 303: passthrough, // crypto-hdkey\n 304: passthrough, // crypto-keypath\n 305: passthrough, // crypto-coin-info\n }),\n }) as Map<number, unknown>;\n}\n\nfunction parseCryptoHdKey(map: unknown, raw: string): ParsedXpub {\n const keyData = get(map, 3) as Uint8Array | undefined;\n const chainCode = get(map, 4) as Uint8Array | undefined;\n\n if (!keyData || !chainCode) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n let purpose: number | undefined;\n let coinType: number | undefined;\n let sourceFingerprint: number | undefined;\n const origin = get(map, 6);\n if (origin) {\n const components = get(origin, 1);\n if (Array.isArray(components)) {\n if (components.length >= 1) purpose = components[0] as number;\n if (components.length >= 3) coinType = components[2] as number;\n }\n sourceFingerprint = get(origin, 2) as number | undefined;\n }\n\n const name = get(map, 9) as string | undefined;\n\n const hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, name, raw };\n}\n\nfunction parseScannedUR(scanned: ScannedUR): ParsedXpub | ParsedXpub[] {\n const { type, cbor } = scanned;\n const raw = `ur:${type}`;\n\n if (type !== \"crypto-hdkey\" && type !== \"crypto-account\") {\n throw new Error(`Unsupported UR type: ${type}`);\n }\n\n const map = decodeCbor(cbor);\n\n if (type === \"crypto-hdkey\") {\n return parseCryptoHdKey(map, raw);\n }\n\n const accounts = map.get(2) as unknown[] | undefined;\n if (!Array.isArray(accounts) || accounts.length === 0) {\n throw new Error(\"crypto-account contains no keys\");\n }\n return accounts.map((entry) => parseCryptoHdKey(entry, raw));\n}\n\nexport function parseXpub(input: ScannedUR | string): ParsedXpub[] {\n if (typeof input !== \"string\") {\n const result = parseScannedUR(input);\n return Array.isArray(result) ? result : [result];\n }\n // Raw base58 xpub string — @scure/bip32 handles decoding directly\n const hdKey = HDKey.fromExtendedKey(input.trim());\n return [\n {\n hdKey,\n type: \"xpub\",\n purpose: undefined,\n coinType: undefined,\n sourceFingerprint: undefined,\n raw: input.trim(),\n },\n ];\n}\n","import { keccak_256 } from \"@noble/hashes/sha3.js\";\nimport * as secp from \"@noble/secp256k1\";\n\nexport function pubKeyToEthAddress(compressedPubKey: Uint8Array): string {\n const uncompressed = secp.Point.fromBytes(compressedPubKey).toBytes(false);\n const hash = keccak_256(uncompressed.slice(1));\n const hex = [...hash.slice(12)]\n .map((b: number) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n return toChecksumAddress(hex);\n}\n\nfunction toChecksumAddress(hex: string): string {\n const checksumHash = keccak_256(new TextEncoder().encode(hex));\n return (\n \"0x\" +\n [...hex]\n .map((c, i) => {\n if (c >= \"0\" && c <= \"9\") return c;\n const nibble =\n i % 2 === 0\n ? (checksumHash[Math.floor(i / 2)] >> 4) & 0xf\n : checksumHash[Math.floor(i / 2)] & 0xf;\n return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();\n })\n .join(\"\")\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport type { ParsedXpub } from \"../parseXpub.js\";\nimport { pubKeyToEthAddress } from \"./address.js\";\n\nexport interface DerivedAccount {\n address: string;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nfunction toHex(bytes: Uint8Array): string {\n return [...bytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function deriveEvmAccount(parsed: ParsedXpub[]): DerivedAccount | undefined {\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;\n const isEvm =\n (purpose === 44 && coinType === 60) || (purpose === undefined && type === \"xpub\");\n\n if (!isEvm) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n return {\n address: pubKeyToEthAddress(child.publicKey),\n publicKey: toHex(child.publicKey),\n sourceFingerprint,\n device: name,\n };\n }\n return undefined;\n}\n","import { parseXpub } from \"./parseXpub.js\";\nimport { deriveEvmAccount } from \"./eth/deriveAccount.js\";\nimport type { Account, Chain, EvmAccount, QRKitConfig, ScannedUR } from \"./types.js\";\n\nconst ALL_CHAINS: Chain[] = [\"evm\", \"btc\"];\n\n/**\n * Parse a connection QR (crypto-hdkey or crypto-account) and return\n * only the accounts for the chains configured in QRKitConfig.\n *\n * A dApp configured with `chains: [\"evm\"]` will never see BTC accounts,\n * and vice versa. Both chains can be enabled with `chains: [\"evm\", \"btc\"]`.\n * If `chains` is omitted, all supported chains are tried.\n */\nexport function parseConnection(\n scannedUR: ScannedUR,\n config: QRKitConfig = {},\n): Account[] {\n const chains = config.chains ?? ALL_CHAINS;\n const parsed = parseXpub(scannedUR);\n const accounts: Account[] = [];\n\n if (chains.includes(\"evm\")) {\n const account = deriveEvmAccount(parsed);\n if (account) {\n accounts.push({ chain: \"evm\", ...account } satisfies EvmAccount);\n }\n }\n\n // \"btc\" — will be implemented in src/btc/ when Bitcoin support is added.\n\n return accounts;\n}\n","// Minimal CBOR encoder.\n// Supports only the types needed for eth-sign-request: uint, bytes, text, array, map (integer keys), bool, tag.\n\nfunction majorType(major: number, n: number): number[] {\n const base = major << 5;\n if (n <= 23) return [base | n];\n if (n <= 0xff) return [base | 0x18, n];\n if (n <= 0xffff) return [base | 0x19, (n >> 8) & 0xff, n & 0xff];\n return [base | 0x1a, (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];\n}\n\nexport function encodeItem(value: unknown): number[] {\n if (typeof value === \"boolean\") {\n return [value ? 0xf5 : 0xf4];\n }\n if (typeof value === \"number\") {\n return majorType(0, value);\n }\n if (value instanceof Uint8Array) {\n return [...majorType(2, value.length), ...value];\n }\n if (typeof value === \"string\") {\n const bytes = new TextEncoder().encode(value);\n return [...majorType(3, bytes.length), ...bytes];\n }\n if (Array.isArray(value)) {\n return [...majorType(4, value.length), ...value.flatMap(encodeItem)];\n }\n if (value instanceof CborTag) {\n return [...majorType(6, value.tag), ...encodeItem(value.value)];\n }\n if (value instanceof Map) {\n const entries = [...value.entries()];\n return [\n ...majorType(5, entries.length),\n ...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)]),\n ];\n }\n throw new Error(`Unsupported CBOR value: ${typeof value}`);\n}\n\nexport class CborTag {\n tag: number;\n value: unknown;\n constructor(tag: number, value: unknown) {\n this.tag = tag;\n this.value = value;\n }\n}\n\nexport function encode(value: unknown): Uint8Array {\n return new Uint8Array(encodeItem(value));\n}\n","import { UR, UrFountainEncoder } from \"@qrkit/bc-ur-web\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n const ur = UR.fromCbor({ type, payload: cbor });\n const encoder = new UrFountainEncoder(ur, maxFragmentLength);\n return encoder.getAllPartsUr().map((part) => part.toString().toUpperCase());\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\n// ERC-4527 eth-sign-request data types\nexport const EthDataType = {\n /** Legacy transaction (RLP-encoded). Wallet applies EIP-155: v = 35 + 2*chainId + recId */\n LegacyTransaction: 1,\n /** EIP-712 typed data bytes. Wallet hashes internally. */\n TypedData: 2,\n /** Personal message (EIP-191). Wallet prepends \"\\x19Ethereum Signed Message:\\n{len}\". */\n PersonalMessage: 3,\n /** EIP-1559 transaction (RLP-encoded). Wallet uses v = recId. */\n TypedTransaction: 4,\n} as const;\n\nexport type EthDataTypeValue = (typeof EthDataType)[keyof typeof EthDataType];\n\n// CBOR tag for crypto-keypath\nconst TAG_KEYPATH = 304;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction buildKeypath(\n purpose: number,\n coinType: number,\n sourceFingerprint: number | undefined,\n): CborTag {\n // m/purpose'/coinType'/0'/0/0\n const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface EthSignRequestParams {\n /** Raw sign data. For PersonalMessage, a string is UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n /** ERC-4527 data type. Defaults to PersonalMessage (3). Use EthDataType constants. */\n dataType?: number;\n address: string;\n sourceFingerprint: number | undefined;\n /** Chain ID — required by the wallet for v-value encoding on LegacyTransaction (type 1). */\n chainId?: number;\n origin?: string;\n}\n\nfunction buildEthSignRequestCbor(params: EthSignRequestParams): Uint8Array {\n const {\n signData,\n dataType = EthDataType.PersonalMessage,\n address,\n sourceFingerprint,\n chainId,\n origin = \"qrkit\",\n } = params;\n\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(44, 60, sourceFingerprint);\n\n const addrHex = address.replace(/^0x/i, \"\");\n const addrBytes = new Uint8Array(addrHex.match(/.{2}/g)!.map((b) => parseInt(b, 16)));\n\n // Keys must be in strictly ascending order for zcbor (Shell firmware decoder).\n const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, dataType], // data-type\n ]);\n\n if (chainId !== undefined) {\n map.set(4, chainId); // chain-id — must come before key 5\n }\n\n map.set(5, keypath); // derivation-path\n map.set(6, addrBytes); // address\n map.set(7, origin); // origin\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(params: EthSignRequestParams): string[] {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\");\n}\n\nexport function buildEthSignRequestUR(params: EthSignRequestParams): string {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"../types.js\";\n\nexport function parseEthSignature(scanned: ScannedUR): string {\n if (scanned.type !== \"eth-signature\") {\n throw new Error(`Expected eth-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const sigBytes = map.get(2) as Uint8Array | undefined;\n if (!sigBytes || sigBytes.length < 64) {\n throw new Error(\"Invalid or missing signature bytes\");\n }\n\n return \"0x\" + [...sigBytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAS,UAAU,kBAAmC;AAoBtD,SAAS,IAAI,GAAY,GAAoB;AAC3C,MAAI,aAAa,IAAK,QAAQ,EAA2B,IAAI,CAAC;AAC9D,SAAO;AACT;AAEA,IAAM,cAAc,CAAC,MAAe;AAEpC,SAAS,WAAW,MAAwC;AAC1D,SAAO,WAAW,MAAM;AAAA,IACtB,SAAS;AAAA,IACT,MAAM,OAAO,OAAO,CAAC,GAAmB;AAAA,MACtC,KAAK;AAAA;AAAA,MACL,KAAK;AAAA;AAAA,MACL,KAAK;AAAA;AAAA,IACP,CAAC;AAAA,EACH,CAAC;AACH;AAEA,SAAS,iBAAiB,KAAc,KAAyB;AAC/D,QAAM,UAAU,IAAI,KAAK,CAAC;AAC1B,QAAM,YAAY,IAAI,KAAK,CAAC;AAE5B,MAAI,CAAC,WAAW,CAAC,WAAW;AAC1B,UAAM,IAAI,MAAM,6CAA6C;AAAA,EAC/D;AAEA,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,QAAM,SAAS,IAAI,KAAK,CAAC;AACzB,MAAI,QAAQ;AACV,UAAM,aAAa,IAAI,QAAQ,CAAC;AAChC,QAAI,MAAM,QAAQ,UAAU,GAAG;AAC7B,UAAI,WAAW,UAAU,EAAG,WAAU,WAAW,CAAC;AAClD,UAAI,WAAW,UAAU,EAAG,YAAW,WAAW,CAAC;AAAA,IACrD;AACA,wBAAoB,IAAI,QAAQ,CAAC;AAAA,EACnC;AAEA,QAAM,OAAO,IAAI,KAAK,CAAC;AAEvB,QAAM,QAAQ,IAAI,MAAM,EAAE,WAAW,SAAS,UAAU,CAAC;AACzD,SAAO,EAAE,OAAO,MAAM,QAAQ,SAAS,UAAU,mBAAmB,MAAM,IAAI;AAChF;AAEA,SAAS,eAAe,SAA+C;AAC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QAAQ,IAAI,EAAE;AAAA,EAChE;AAEA,QAAM,MAAMA,YAAW,QAAQ,MAAM;AAAA,IACnC,SAAS;AAAA,IACT,MAAM,OAAO,OAAO,CAAC,GAAmB,EAAE,IAAI,CAAC,MAAe,EAAE,CAAC;AAAA,EACnE,CAAC;AAED,QAAM,WAAW,IAAI,IAAI,CAAC;AAC1B,MAAI,CAAC,YAAY,SAAS,SAAS,IAAI;AACrC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO,OAAO,CAAC,GAAG,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACjF;","names":["cborDecode"]}
|
|
1
|
+
{"version":3,"sources":["../src/btc/address.ts","../src/bytes.ts","../src/btc/deriveAccount.ts","../src/eth/address.ts","../src/eth/deriveAccount.ts","../src/parseXpub.ts","../src/parseConnection.ts","../src/cbor.ts","../src/urEncoding.ts","../src/eth/signRequest.ts","../src/eth/signature.ts","../src/btc/signRequest.ts","../src/btc/signature.ts","../src/btc/psbt.ts"],"sourcesContent":["import { bech32, createBase58check } from \"@scure/base\";\nimport { ripemd160 } from \"@noble/hashes/legacy.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\n\nexport type BtcScriptType = \"p2wpkh\" | \"p2sh-p2wpkh\" | \"p2pkh\";\n\nconst base58check = createBase58check(sha256);\n\nfunction hash160(bytes: Uint8Array): Uint8Array {\n return ripemd160(sha256(bytes));\n}\n\n/** Native SegWit — P2WPKH, `bc1q...` */\nexport function pubKeyToP2wpkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n const words = bech32.toWords(h);\n return bech32.encode(\"bc\", [0, ...words]);\n}\n\n/** Nested SegWit — P2SH-P2WPKH, `3...` */\nexport function pubKeyToP2shP2wpkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n // redeemScript = OP_0 <20-byte-pubkey-hash>\n const redeemScript = new Uint8Array(22);\n redeemScript[0] = 0x00;\n redeemScript[1] = 0x14;\n redeemScript.set(h, 2);\n const scriptHash = hash160(redeemScript);\n const payload = new Uint8Array(21);\n payload[0] = 0x05; // P2SH version byte\n payload.set(scriptHash, 1);\n return base58check.encode(payload);\n}\n\n/** Legacy — P2PKH, `1...` */\nexport function pubKeyToP2pkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n const payload = new Uint8Array(21);\n payload[0] = 0x00; // mainnet P2PKH version byte\n payload.set(h, 1);\n return base58check.encode(payload);\n}\n","export function bytesToBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nexport function bytesToHex(bytes: Uint8Array): string {\n return [...bytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n const normalized = hex.trim().replace(/^0x/i, \"\");\n if (normalized.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(normalized)) {\n throw new Error(\"Invalid hex string\");\n }\n\n return new Uint8Array(\n normalized.match(/.{2}/g)?.map((byte) => parseInt(byte, 16)) ?? [],\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport {\n pubKeyToP2pkh,\n pubKeyToP2shP2wpkh,\n pubKeyToP2wpkh,\n type BtcScriptType,\n} from \"./address.js\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport type { ParsedXpub } from \"../parseXpub.js\";\n\nexport interface DerivedBtcAccount {\n address: string;\n scriptType: BtcScriptType;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nfunction scriptTypeFromPurpose(purpose: number | undefined): BtcScriptType | undefined {\n if (purpose === 84) return \"p2wpkh\";\n if (purpose === 49) return \"p2sh-p2wpkh\";\n if (purpose === 44) return \"p2pkh\";\n return undefined;\n}\n\nfunction deriveAddress(pubKey: Uint8Array, scriptType: BtcScriptType): string {\n if (scriptType === \"p2wpkh\") return pubKeyToP2wpkh(pubKey);\n if (scriptType === \"p2sh-p2wpkh\") return pubKeyToP2shP2wpkh(pubKey);\n return pubKeyToP2pkh(pubKey);\n}\n\nexport function deriveBtcAccount(parsed: ParsedXpub[]): DerivedBtcAccount[] {\n const results: DerivedBtcAccount[] = [];\n\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, sourceFingerprint, name } = entry;\n\n // BTC: coin type 0\n if (coinType !== 0) continue;\n\n const scriptType = scriptTypeFromPurpose(purpose);\n if (!scriptType) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n results.push({\n address: deriveAddress(child.publicKey, scriptType),\n scriptType,\n publicKey: bytesToHex(child.publicKey),\n sourceFingerprint,\n device: name,\n });\n }\n\n return results;\n}\n","import { secp256k1 } from \"@noble/curves/secp256k1.js\";\nimport { keccak_256 } from \"@noble/hashes/sha3.js\";\n\nexport function pubKeyToEthAddress(compressedPubKey: Uint8Array): string {\n const pubKeyHex = [...compressedPubKey]\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n const uncompressed = secp256k1.Point.fromHex(pubKeyHex).toBytes(false);\n const hash = keccak_256(uncompressed.slice(1));\n const hex = [...hash.slice(12)]\n .map((b: number) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n return toChecksumAddress(hex);\n}\n\nfunction toChecksumAddress(hex: string): string {\n const checksumHash = keccak_256(new TextEncoder().encode(hex));\n return (\n \"0x\" +\n [...hex]\n .map((c, i) => {\n if (c >= \"0\" && c <= \"9\") return c;\n const nibble =\n i % 2 === 0\n ? (checksumHash[Math.floor(i / 2)] >> 4) & 0xf\n : checksumHash[Math.floor(i / 2)] & 0xf;\n return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();\n })\n .join(\"\")\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport { pubKeyToEthAddress } from \"./address.js\";\nimport type { ParsedXpub } from \"../parseXpub.js\";\n\nexport interface DerivedAccount {\n address: string;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nexport function deriveEvmAccount(parsed: ParsedXpub[]): DerivedAccount[] {\n const results: DerivedAccount[] = [];\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;\n const isEvm =\n (purpose === 44 && coinType === 60) || (purpose === undefined && type === \"xpub\");\n\n if (!isEvm) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n results.push({\n address: pubKeyToEthAddress(child.publicKey),\n publicKey: bytesToHex(child.publicKey),\n sourceFingerprint,\n device: name,\n });\n }\n return results;\n}\n","import { HDKey } from \"@scure/bip32\";\nimport { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"./types.js\";\n\nexport type XpubType = \"xpub\";\n\nexport interface ParsedXpub {\n hdKey: HDKey;\n type: XpubType;\n /** BIP-44 purpose index: 44 for EVM, 44/49/84 for supported BTC scripts */\n purpose: number | undefined;\n /** BIP-44 coin type: 60 = ETH, 0 = BTC */\n coinType: number | undefined;\n /** source-fingerprint from the origin keypath — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name from crypto-hdkey key 9, set by some wallets (e.g. Keystone) */\n name?: string;\n raw: string;\n}\n\ntype CborMap = Map<number, unknown>;\n\nfunction get(m: unknown, k: number): unknown {\n if (m instanceof Map) return (m as Map<number, unknown>).get(k);\n return undefined;\n}\n\nfunction bytesFromCbor(value: unknown): Uint8Array | undefined {\n if (value instanceof Uint8Array) return value;\n\n if (value instanceof Map && value.get(\"type\") === \"Buffer\") {\n const data = value.get(\"data\");\n if (\n Array.isArray(data) &&\n data.every((byte) => Number.isInteger(byte) && byte >= 0 && byte <= 0xff)\n ) {\n return new Uint8Array(data);\n }\n }\n\n return undefined;\n}\n\nconst passthrough = (v: unknown) => v;\n\nfunction decodeCbor(cbor: Uint8Array): Map<number, unknown> {\n // Connection payloads often wrap the inner hdkey/output structures in semantic CBOR tags\n // (script expressions, crypto-output, vendor-specific wrappers, etc.). For connection\n // parsing we only care about the inner map/bytes shape, so tags are treated as annotations\n // and stripped during decode. Required structure is validated explicitly later.\n const tags = new Proxy([] as TagDecoder[], {\n get(target, property, receiver) {\n if (typeof property === \"string\" && /^\\d+$/.test(property)) {\n return passthrough;\n }\n return Reflect.get(target, property, receiver);\n },\n });\n\n return cborDecode(cbor, {\n useMaps: true,\n tags,\n }) as Map<number, unknown>;\n}\n\nfunction isCborMap(value: unknown): value is CborMap {\n return value instanceof Map;\n}\n\nfunction assertCryptoHdKeyShape(value: unknown): CborMap {\n if (!isCborMap(value)) {\n throw new Error(\"crypto-hdkey entry must be a CBOR map\");\n }\n\n if (!value.has(3) || !value.has(4)) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n return value;\n}\n\nfunction parseCryptoHdKey(map: CborMap, raw: string, fallbackName?: string): ParsedXpub {\n const keyData = bytesFromCbor(get(map, 3));\n const chainCode = bytesFromCbor(get(map, 4));\n\n if (!keyData || !chainCode) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n let purpose: number | undefined;\n let coinType: number | undefined;\n let sourceFingerprint: number | undefined;\n const origin = get(map, 6);\n if (origin) {\n const components = get(origin, 1);\n if (Array.isArray(components)) {\n if (components.length >= 1) purpose = components[0] as number;\n if (components.length >= 3) coinType = components[2] as number;\n }\n sourceFingerprint = get(origin, 2) as number | undefined;\n }\n\n const name = (get(map, 9) as string | undefined) ?? fallbackName;\n\n const hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, name, raw };\n}\n\nfunction parseScannedUR(scanned: ScannedUR): ParsedXpub | ParsedXpub[] {\n const { type, cbor } = scanned;\n const raw = `ur:${type}`;\n\n if (\n type !== \"crypto-hdkey\" &&\n type !== \"crypto-account\" &&\n type !== \"crypto-multi-accounts\"\n ) {\n throw new Error(`Unsupported UR type: ${type}`);\n }\n\n const map = decodeCbor(cbor);\n\n if (type === \"crypto-hdkey\") {\n return parseCryptoHdKey(assertCryptoHdKeyShape(map), raw);\n }\n\n const accounts = map.get(2) as unknown[] | undefined;\n if (!Array.isArray(accounts) || accounts.length === 0) {\n throw new Error(`${type} contains no keys`);\n }\n const fallbackName =\n type === \"crypto-multi-accounts\" ? (map.get(3) as string | undefined) : undefined;\n return accounts.map((entry) =>\n parseCryptoHdKey(assertCryptoHdKeyShape(entry), raw, fallbackName),\n );\n}\n\nexport function parseXpub(input: ScannedUR | string): ParsedXpub[] {\n if (typeof input !== \"string\") {\n const result = parseScannedUR(input);\n return Array.isArray(result) ? result : [result];\n }\n // Raw base58 xpub string — @scure/bip32 handles decoding directly\n const hdKey = HDKey.fromExtendedKey(input.trim());\n return [\n {\n hdKey,\n type: \"xpub\",\n purpose: undefined,\n coinType: undefined,\n sourceFingerprint: undefined,\n raw: input.trim(),\n },\n ];\n}\n","import { deriveBtcAccount } from \"./btc/deriveAccount.js\";\nimport { deriveEvmAccount } from \"./eth/deriveAccount.js\";\nimport { parseXpub } from \"./parseXpub.js\";\nimport type {\n Account,\n BtcAccount,\n Chain,\n EvmAccount,\n QRKitConfig,\n ScannedUR,\n} from \"./types.js\";\n\nconst ALL_CHAINS: Chain[] = [\"evm\", \"btc\"];\n\n/**\n * Parse a connection QR (crypto-hdkey or crypto-account) and return\n * only the accounts for the chains configured in QRKitConfig.\n *\n * A dApp configured with `chains: [\"evm\"]` will never see BTC accounts,\n * and vice versa. Both chains can be enabled with `chains: [\"evm\", \"btc\"]`.\n * If `chains` is omitted, all supported chains are tried.\n */\nexport function parseConnection(\n scannedUR: ScannedUR,\n config: QRKitConfig = {},\n): Account[] {\n const chains = config.chains ?? ALL_CHAINS;\n const parsed = parseXpub(scannedUR);\n const accounts: Account[] = [];\n\n if (chains.includes(\"evm\")) {\n for (const account of deriveEvmAccount(parsed)) {\n accounts.push({ chain: \"evm\", ...account } satisfies EvmAccount);\n }\n }\n\n if (chains.includes(\"btc\")) {\n for (const account of deriveBtcAccount(parsed)) {\n accounts.push({ chain: \"btc\", ...account } satisfies BtcAccount);\n }\n }\n\n return accounts;\n}\n","// Minimal CBOR encoder.\n// Supports only the types needed for eth-sign-request: uint, bytes, text, array, map (integer keys), bool, tag.\n\nfunction majorType(major: number, n: number): number[] {\n const base = major << 5;\n if (n <= 23) return [base | n];\n if (n <= 0xff) return [base | 0x18, n];\n if (n <= 0xffff) return [base | 0x19, (n >> 8) & 0xff, n & 0xff];\n return [base | 0x1a, (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];\n}\n\nexport function encodeItem(value: unknown): number[] {\n if (typeof value === \"boolean\") {\n return [value ? 0xf5 : 0xf4];\n }\n if (typeof value === \"number\") {\n return majorType(0, value);\n }\n if (value instanceof Uint8Array) {\n return [...majorType(2, value.length), ...value];\n }\n if (typeof value === \"string\") {\n const bytes = new TextEncoder().encode(value);\n return [...majorType(3, bytes.length), ...bytes];\n }\n if (Array.isArray(value)) {\n return [...majorType(4, value.length), ...value.flatMap(encodeItem)];\n }\n if (value instanceof CborTag) {\n return [...majorType(6, value.tag), ...encodeItem(value.value)];\n }\n if (value instanceof Map) {\n const entries = [...value.entries()];\n return [\n ...majorType(5, entries.length),\n ...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)]),\n ];\n }\n throw new Error(`Unsupported CBOR value: ${typeof value}`);\n}\n\nexport class CborTag {\n tag: number;\n value: unknown;\n constructor(tag: number, value: unknown) {\n this.tag = tag;\n this.value = value;\n }\n}\n\nexport function encode(value: unknown): Uint8Array {\n return new Uint8Array(encodeItem(value));\n}\n","import { UR, UrFountainEncoder } from \"@qrkit/bc-ur-web\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n const ur = UR.fromCbor({ type, payload: cbor });\n const encoder = new UrFountainEncoder(ur, maxFragmentLength);\n return encoder.getAllPartsUr().map((part) => part.toString().toUpperCase());\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\n// ERC-4527 eth-sign-request data types\nexport const EthDataType = {\n /** Legacy transaction (RLP-encoded). Wallet applies EIP-155: v = 35 + 2*chainId + recId */\n LegacyTransaction: 1,\n /** EIP-712 typed data bytes. Wallet hashes internally. */\n TypedData: 2,\n /** Personal message (EIP-191). Wallet prepends \"\\x19Ethereum Signed Message:\\n{len}\". */\n PersonalMessage: 3,\n /** EIP-1559 transaction (RLP-encoded). Wallet uses v = recId. */\n TypedTransaction: 4,\n} as const;\n\nexport type EthDataTypeValue = (typeof EthDataType)[keyof typeof EthDataType];\n\n// CBOR tag for crypto-keypath\nconst TAG_KEYPATH = 304;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction buildKeypath(\n purpose: number,\n coinType: number,\n sourceFingerprint: number | undefined,\n): CborTag {\n // m/purpose'/coinType'/0'/0/0\n const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface EthSignRequestParams {\n /** Raw sign data. For PersonalMessage, a string is UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n /** ERC-4527 data type. Defaults to PersonalMessage (3). Use EthDataType constants. */\n dataType?: number;\n address: string;\n sourceFingerprint: number | undefined;\n /** Chain ID — required by the wallet for v-value encoding on LegacyTransaction (type 1). */\n chainId?: number;\n origin?: string;\n}\n\nfunction buildEthSignRequestCbor(params: EthSignRequestParams): Uint8Array {\n const {\n signData,\n dataType = EthDataType.PersonalMessage,\n address,\n sourceFingerprint,\n chainId,\n origin = \"qrkit\",\n } = params;\n\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(44, 60, sourceFingerprint);\n\n const addrHex = address.replace(/^0x/i, \"\");\n const addrBytes = new Uint8Array(addrHex.match(/.{2}/g)!.map((b) => parseInt(b, 16)));\n\n // Keys must be in strictly ascending order for zcbor (Shell firmware decoder).\n const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, dataType], // data-type\n ]);\n\n if (chainId !== undefined) {\n map.set(4, chainId); // chain-id — must come before key 5\n }\n\n map.set(5, keypath); // derivation-path\n map.set(6, addrBytes); // address\n map.set(7, origin); // origin\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(params: EthSignRequestParams): string[] {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\");\n}\n\nexport function buildEthSignRequestUR(params: EthSignRequestParams): string {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport type { ScannedUR } from \"../types.js\";\n\nexport function parseEthSignature(scanned: ScannedUR): string {\n if (scanned.type !== \"eth-signature\") {\n throw new Error(`Expected eth-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const sigBytes = map.get(2) as Uint8Array | undefined;\n if (!sigBytes || sigBytes.length < 64) {\n throw new Error(\"Invalid or missing signature bytes\");\n }\n\n return `0x${bytesToHex(sigBytes)}`;\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\nimport type { BtcScriptType } from \"./address.js\";\n\nexport const BtcDataType = {\n Message: 1,\n} as const;\n\nexport type BtcDataTypeValue = (typeof BtcDataType)[keyof typeof BtcDataType];\n\nconst TAG_KEYPATH = 304;\nconst PURPOSE_BY_SCRIPT_TYPE = {\n p2wpkh: 84,\n \"p2sh-p2wpkh\": 49,\n p2pkh: 44,\n} as const satisfies Record<BtcScriptType, number>;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction purposeFromScriptType(scriptType: BtcScriptType): number {\n return PURPOSE_BY_SCRIPT_TYPE[scriptType];\n}\n\nfunction buildKeypath(\n scriptType: BtcScriptType,\n sourceFingerprint: number | undefined,\n): CborTag {\n const purpose = purposeFromScriptType(scriptType);\n // m/purpose'/0'/0'/0/0\n const components = [purpose, true, 0, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface BtcSignRequestParams {\n /** Raw message data. Strings are UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n address: string;\n /** BTC script type for choosing m/44', m/49', or m/84'. */\n scriptType: BtcScriptType;\n sourceFingerprint: number | undefined;\n origin?: string;\n}\n\nfunction buildBtcSignRequestCbor(params: BtcSignRequestParams): Uint8Array {\n const { signData, address, scriptType, sourceFingerprint, origin = \"qrkit\" } = params;\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(scriptType, sourceFingerprint);\n\n return encode(\n new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, BtcDataType.Message], // data-type\n [4, [keypath]], // btc-derivation-paths\n [5, [address]], // btc-addresses\n [6, origin], // origin\n ]),\n );\n}\n\nexport function buildBtcSignRequestURParts(params: BtcSignRequestParams): string[] {\n return encodeURParts(buildBtcSignRequestCbor(params), \"btc-sign-request\");\n}\n\nexport function buildBtcSignRequestUR(params: BtcSignRequestParams): string {\n return encodeURParts(buildBtcSignRequestCbor(params), \"btc-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport { bytesToBase64, bytesToHex } from \"../bytes.js\";\nimport type { ScannedUR } from \"../types.js\";\n\nexport interface BtcSignature {\n signature: string;\n publicKey: string;\n requestId: string | undefined;\n}\n\nexport function parseBtcSignature(scanned: ScannedUR): BtcSignature {\n if (scanned.type !== \"btc-signature\") {\n throw new Error(`Expected btc-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const requestId = map.get(1) as Uint8Array | undefined;\n const sigBytes = map.get(2) as Uint8Array | undefined;\n const publicKeyBytes = map.get(3) as Uint8Array | undefined;\n\n if (!sigBytes || sigBytes.length !== 65) {\n throw new Error(\"Invalid or missing BTC signature bytes\");\n }\n if (!publicKeyBytes || publicKeyBytes.length !== 33) {\n throw new Error(\"Invalid or missing BTC public key bytes\");\n }\n\n return {\n signature: bytesToBase64(sigBytes),\n publicKey: bytesToHex(publicKeyBytes),\n requestId: requestId ? bytesToHex(requestId) : undefined,\n };\n}\n","import { decode as cborDecode } from \"cborg\";\n\nimport { bytesToHex, hexToBytes } from \"../bytes.js\";\nimport { encode } from \"../cbor.js\";\nimport type { ScannedUR } from \"../types.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\nexport interface CryptoPsbt {\n psbt: Uint8Array;\n psbtHex: string;\n}\n\nfunction normalizePsbt(psbt: Uint8Array | string): Uint8Array {\n try {\n return typeof psbt === \"string\" ? hexToBytes(psbt) : psbt;\n } catch (error) {\n throw new Error(\"Invalid PSBT hex\", { cause: error });\n }\n}\n\nexport function parseCryptoPsbt(scanned: ScannedUR): CryptoPsbt {\n if (scanned.type !== \"crypto-psbt\") {\n throw new Error(`Expected crypto-psbt, got: ${scanned.type}`);\n }\n\n const psbt = cborDecode(scanned.cbor) as unknown;\n if (!(psbt instanceof Uint8Array)) {\n throw new Error(\"Invalid crypto-psbt payload\");\n }\n\n return {\n psbt,\n psbtHex: bytesToHex(psbt),\n };\n}\n\nexport function buildCryptoPsbtURParts(psbt: Uint8Array | string): string[] {\n return encodeURParts(encode(normalizePsbt(psbt)), \"crypto-psbt\");\n}\n\nexport function buildCryptoPsbtUR(psbt: Uint8Array | string): string {\n return buildCryptoPsbtURParts(psbt)[0];\n}\n"],"mappings":";AAAA,SAAS,QAAQ,yBAAyB;AAC1C,SAAS,iBAAiB;AAC1B,SAAS,cAAc;AAIvB,IAAM,cAAc,kBAAkB,MAAM;AAE5C,SAAS,QAAQ,OAA+B;AAC9C,SAAO,UAAU,OAAO,KAAK,CAAC;AAChC;AAGO,SAAS,eAAe,kBAAsC;AACnE,QAAM,IAAI,QAAQ,gBAAgB;AAClC,QAAM,QAAQ,OAAO,QAAQ,CAAC;AAC9B,SAAO,OAAO,OAAO,MAAM,CAAC,GAAG,GAAG,KAAK,CAAC;AAC1C;AAGO,SAAS,mBAAmB,kBAAsC;AACvE,QAAM,IAAI,QAAQ,gBAAgB;AAElC,QAAM,eAAe,IAAI,WAAW,EAAE;AACtC,eAAa,CAAC,IAAI;AAClB,eAAa,CAAC,IAAI;AAClB,eAAa,IAAI,GAAG,CAAC;AACrB,QAAM,aAAa,QAAQ,YAAY;AACvC,QAAM,UAAU,IAAI,WAAW,EAA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,MAAM,wCAAwC;AAAA,EAC1D;AACA,MAAI,CAAC,kBAAkB,eAAe,WAAW,IAAI;AACnD,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL,WAAW,cAAc,QAAQ;AAAA,IACjC,WAAW,WAAW,cAAc;AAAA,IACpC,WAAW,YAAY,WAAW,SAAS,IAAI;AAAA,EACjD;AACF;;;ACrCA,SAAS,UAAUC,mBAAkB;AAYrC,SAAS,cAAc,MAAuC;AAC5D,MAAI;AACF,WAAO,OAAO,SAAS,WAAW,WAAW,IAAI,IAAI;AAAA,EACvD,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,oBAAoB,EAAE,OAAO,MAAM,CAAC;AAAA,EACtD;AACF;AAEO,SAAS,gBAAgB,SAAgC;AAC9D,MAAI,QAAQ,SAAS,eAAe;AAClC,UAAM,IAAI,MAAM,8BAA8B,QAAQ,IAAI,EAAE;AAAA,EAC9D;AAEA,QAAM,OAAOC,YAAW,QAAQ,IAAI;AACpC,MAAI,EAAE,gBAAgB,aAAa;AACjC,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AAEA,SAAO;AAAA,IACL;AAAA,IACA,SAAS,WAAW,IAAI;AAAA,EAC1B;AACF;AAEO,SAAS,uBAAuB,MAAqC;AAC1E,SAAO,cAAc,OAAO,cAAc,IAAI,CAAC,GAAG,aAAa;AACjE;AAEO,SAAS,kBAAkB,MAAmC;AACnE,SAAO,uBAAuB,IAAI,EAAE,CAAC;AACvC;","names":["firstChild","cborDecode","cborDecode","TAG_KEYPATH","randomBytes","buildKeypath","cborDecode","cborDecode","cborDecode","cborDecode"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qrkit/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Protocol core for QR-based airgapped wallet flows — UR decoding, xpub parsing, address derivation, sign request and signature handling.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -18,9 +18,10 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@
|
|
21
|
+
"@noble/curves": "^2.0.1",
|
|
22
22
|
"@noble/hashes": "^2.0.1",
|
|
23
|
-
"@
|
|
23
|
+
"@qrkit/bc-ur-web": "^2.0.1",
|
|
24
|
+
"@scure/base": "^2.0.0",
|
|
24
25
|
"@scure/bip32": "^2.0.1",
|
|
25
26
|
"cborg": "^4.5.8"
|
|
26
27
|
},
|
|
@@ -37,6 +38,9 @@
|
|
|
37
38
|
"test": "vitest run",
|
|
38
39
|
"typecheck": "tsc --noEmit",
|
|
39
40
|
"lint": "eslint src",
|
|
40
|
-
"example": "tsx examples/eth-flow.ts"
|
|
41
|
+
"example": "tsx examples/eth-flow.ts",
|
|
42
|
+
"example:eth": "tsx examples/eth-flow.ts",
|
|
43
|
+
"example:btc-message": "tsx examples/btc-message-flow.ts",
|
|
44
|
+
"example:btc-psbt": "tsx examples/btc-psbt-flow.ts"
|
|
41
45
|
}
|
|
42
46
|
}
|