@qntx/wallet-engine 0.13.0 → 0.14.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.
- package/README.md +1057 -20
- package/dist/apps-fiat24.d.mts +202 -0
- package/dist/apps-fiat24.mjs +1378 -0
- package/dist/apps-fiat24.mjs.map +1 -0
- package/dist/apps-hyperliquid.d.mts +549 -0
- package/dist/apps-hyperliquid.mjs +2742 -0
- package/dist/apps-hyperliquid.mjs.map +1 -0
- package/dist/apps-polymarket.d.mts +729 -0
- package/dist/apps-polymarket.mjs +3786 -0
- package/dist/apps-polymarket.mjs.map +1 -0
- package/dist/apps.d.mts +136 -0
- package/dist/apps.mjs +535 -0
- package/dist/apps.mjs.map +1 -0
- package/dist/connect-BY1C5ZRI.mjs +210 -0
- package/dist/connect-BY1C5ZRI.mjs.map +1 -0
- package/dist/connect.d.mts +17 -9
- package/dist/connect.mjs +3 -185
- package/dist/{errors-CQnlTc1i.d.mts → controller-DpNVHVsj.d.mts} +1093 -758
- package/dist/controller-rMnaxKAH.mjs +46 -0
- package/dist/controller-rMnaxKAH.mjs.map +1 -0
- package/dist/decimal-COJ4g8qy.mjs +38 -0
- package/dist/decimal-COJ4g8qy.mjs.map +1 -0
- package/dist/eligibility-BfM9ybYZ.mjs +48 -0
- package/dist/eligibility-BfM9ybYZ.mjs.map +1 -0
- package/dist/engine-Dic_kkEr.d.mts +1829 -0
- package/dist/errors-B8W-hoYW.d.mts +7 -0
- package/dist/errors-horUy5k1.mjs +24 -0
- package/dist/errors-horUy5k1.mjs.map +1 -0
- package/dist/gateway-contract.d.mts +3 -608
- package/dist/gateway-contract.mjs +1467 -161
- package/dist/gateway-contract.mjs.map +1 -1
- package/dist/idb.d.mts +5 -0
- package/dist/idb.mjs +122 -46
- package/dist/idb.mjs.map +1 -1
- package/dist/index-vBgfOFIh.d.mts +2227 -0
- package/dist/index.d.mts +58 -618
- package/dist/index.mjs +21963 -6836
- package/dist/index.mjs.map +1 -1
- package/dist/json-BDwV38cj.mjs +15 -0
- package/dist/json-BDwV38cj.mjs.map +1 -0
- package/dist/link-Ceq8Dggp.mjs +139 -0
- package/dist/link-Ceq8Dggp.mjs.map +1 -0
- package/dist/primitives-DbSHLZKq.mjs +31 -0
- package/dist/primitives-DbSHLZKq.mjs.map +1 -0
- package/dist/push-message-AQuzoNFY.d.mts +84 -0
- package/dist/push-message-kbnH1A5w.mjs +142 -0
- package/dist/push-message-kbnH1A5w.mjs.map +1 -0
- package/dist/push.d.mts +13 -0
- package/dist/push.mjs +70 -0
- package/dist/push.mjs.map +1 -0
- package/dist/qr.d.mts +292 -0
- package/dist/qr.mjs +1725 -0
- package/dist/qr.mjs.map +1 -0
- package/dist/registry-CbtwcxkX.mjs +374 -0
- package/dist/registry-CbtwcxkX.mjs.map +1 -0
- package/dist/risk-DyABgg4W.mjs +757 -0
- package/dist/risk-DyABgg4W.mjs.map +1 -0
- package/dist/session-CNJFLarP.mjs +18 -0
- package/dist/session-CNJFLarP.mjs.map +1 -0
- package/dist/socket-ChG9JRT_.mjs +110 -0
- package/dist/socket-ChG9JRT_.mjs.map +1 -0
- package/dist/spi-BrTw5s98.d.mts +3 -0
- package/dist/timer-Cdk5rGM2.mjs +43 -0
- package/dist/timer-Cdk5rGM2.mjs.map +1 -0
- package/dist/types-BpvioFGo.d.mts +258 -0
- package/dist/types-DbftVZ3I.mjs +90 -0
- package/dist/types-DbftVZ3I.mjs.map +1 -0
- package/dist/walletconnect.d.mts +86 -0
- package/dist/walletconnect.mjs +1507 -0
- package/dist/walletconnect.mjs.map +1 -0
- package/dist/webln-BgrtPXpG.mjs +2593 -0
- package/dist/webln-BgrtPXpG.mjs.map +1 -0
- package/dist/webln-Bllk_xJ2.d.mts +666 -0
- package/package.json +92 -19
- package/dist/connect.mjs.map +0 -1
- package/dist/registry-DwguKWvt.d.mts +0 -27
- package/dist/registry-oorLoGtz.mjs +0 -120
- package/dist/registry-oorLoGtz.mjs.map +0 -1
- package/dist/webln-DKqCnqx1.mjs +0 -3677
- package/dist/webln-DKqCnqx1.mjs.map +0 -1
package/dist/qr.mjs
ADDED
|
@@ -0,0 +1,1725 @@
|
|
|
1
|
+
import { a as keypath, c as QrError, d as resultMismatch, f as unsupported, i as QR_FINGERPRINT, l as malformed, m as TronBindingError, n as BITCOIN_TESTNET, o as pathComponents, p as SolanaClusterBindingError, r as PURPOSE_TEMPLATE, s as qrLinkPath, t as BITCOIN_MAINNET, u as qrGuard, v as safeDisplayText } from "./link-Ceq8Dggp.mjs";
|
|
2
|
+
import { parseTransaction } from "viem";
|
|
3
|
+
import { bytesToHex as bytesToHex$1, concatBytes, hexToBytes } from "@noble/hashes/utils.js";
|
|
4
|
+
import { assembleEvm, decodeEvm, evmAddressFromPublicKey, evmSigningPreimage, parseTypedData, planEvm, recoverEvmSigner } from "@qntx/wallet-chains/evm";
|
|
5
|
+
import { decodeSolana, parseCompiledMessage, solanaAddressFromPublicKey, verifySolanaSignature } from "@qntx/wallet-chains/solana";
|
|
6
|
+
import { decodeTron, isTronAddress, recoverTronMessageSigner, recoverTronTransactionSigner, tronAddressFromBytes, tronAddressFromPublicKey, tronAddressToBytes, tronMessageDigest } from "@qntx/wallet-chains/tron";
|
|
7
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
8
|
+
import { OutScript, Transaction } from "@scure/btc-signer";
|
|
9
|
+
import { BITCOIN_NETWORKS, bitcoinAddressFromXpub, bitcoinNetwork, decodeBitcoinPsbt, samePsbtContent } from "@qntx/wallet-chains/bitcoin";
|
|
10
|
+
import { base58 } from "@scure/base";
|
|
11
|
+
import { qrParts as qrFrames } from "@qntx/wallet/hardware";
|
|
12
|
+
import { UrDecoder, isUrType } from "@qntx/ur";
|
|
13
|
+
import { cryptoMultiAccountsCodec, ethSignRequestCodec, ethSignatureCodec, oneKeyTronSignRequestCodec, solSignRequestCodec, solSignatureCodec, tronSignRequestCodec, tronSignatureCodec } from "@qntx/ur/interop";
|
|
14
|
+
import { CoinType, Network, cryptoAccountCodec, cryptoHdKeyCodec, cryptoPsbtCodec, psbtCodec } from "@qntx/ur/registry";
|
|
15
|
+
import { Ur, fromUr, toUr } from "@qntx/ur/typed";
|
|
16
|
+
import { Transaction as Transaction$1 } from "@scure/btc-signer/transaction.js";
|
|
17
|
+
import * as TypedData from "ox/TypedData";
|
|
18
|
+
import { HDKey } from "@scure/bip32";
|
|
19
|
+
import * as Hex from "ox/Hex";
|
|
20
|
+
import * as PublicKey from "ox/PublicKey";
|
|
21
|
+
import * as Secp256k1 from "ox/Secp256k1";
|
|
22
|
+
import * as Signature from "ox/Signature";
|
|
23
|
+
// src/qr/scan.ts
|
|
24
|
+
/**
|
|
25
|
+
* The bounded incremental QR scanner: `@qntx/ur`'s fountain decoder under the engine's own caps — a
|
|
26
|
+
* UR payload is public, untrusted bytes, so every frame counts toward a frame budget and the
|
|
27
|
+
* deadline runs from the first frame. Rejected and duplicate frames still consume the budget.
|
|
28
|
+
*/
|
|
29
|
+
const QR_SCAN_LIMITS = {
|
|
30
|
+
maxMessageBytes: 65536,
|
|
31
|
+
maxFragments: 512,
|
|
32
|
+
maxFragmentBytes: 2048,
|
|
33
|
+
maxUriBytes: 4096,
|
|
34
|
+
maxFrames: 4096,
|
|
35
|
+
timeoutMs: 12e4
|
|
36
|
+
};
|
|
37
|
+
const ACCEPT = {
|
|
38
|
+
request: [
|
|
39
|
+
"eth-sign-request",
|
|
40
|
+
"sol-sign-request",
|
|
41
|
+
"tron-sign-request",
|
|
42
|
+
"crypto-psbt",
|
|
43
|
+
"psbt"
|
|
44
|
+
],
|
|
45
|
+
export: [
|
|
46
|
+
"crypto-hdkey",
|
|
47
|
+
"crypto-account",
|
|
48
|
+
"crypto-multi-accounts"
|
|
49
|
+
],
|
|
50
|
+
response: [
|
|
51
|
+
"eth-signature",
|
|
52
|
+
"sol-signature",
|
|
53
|
+
"tron-signature",
|
|
54
|
+
"crypto-psbt",
|
|
55
|
+
"psbt"
|
|
56
|
+
]
|
|
57
|
+
};
|
|
58
|
+
const createQrScanner = (options) => {
|
|
59
|
+
const merged = {
|
|
60
|
+
...QR_SCAN_LIMITS,
|
|
61
|
+
...options.limits
|
|
62
|
+
};
|
|
63
|
+
for (const key of [
|
|
64
|
+
"maxMessageBytes",
|
|
65
|
+
"maxFragments",
|
|
66
|
+
"maxFragmentBytes",
|
|
67
|
+
"maxUriBytes",
|
|
68
|
+
"maxFrames",
|
|
69
|
+
"timeoutMs"
|
|
70
|
+
]) if (merged[key] > QR_SCAN_LIMITS[key]) throw new QrError("unsupported", `${key} above the default bound`);
|
|
71
|
+
const limits = {
|
|
72
|
+
maxMessageLength: merged.maxMessageBytes,
|
|
73
|
+
maxFragmentCount: merged.maxFragments,
|
|
74
|
+
maxFragmentLength: merged.maxFragmentBytes,
|
|
75
|
+
maxUriLength: merged.maxUriBytes
|
|
76
|
+
};
|
|
77
|
+
const accept = ACCEPT[options.kind].map((type) => {
|
|
78
|
+
if (!isUrType(type)) throw new QrError("unsupported", `bad UR type ${type}`);
|
|
79
|
+
return type;
|
|
80
|
+
});
|
|
81
|
+
const decoder = new UrDecoder({
|
|
82
|
+
accept,
|
|
83
|
+
limits
|
|
84
|
+
});
|
|
85
|
+
let frames = 0;
|
|
86
|
+
let deadline;
|
|
87
|
+
let done;
|
|
88
|
+
return { receive: (text) => {
|
|
89
|
+
if (done !== void 0) return done;
|
|
90
|
+
frames += 1;
|
|
91
|
+
if (frames > merged.maxFrames) throw new QrError("scan-exhausted", "frame budget exceeded");
|
|
92
|
+
if (deadline === void 0) deadline = options.now() + merged.timeoutMs;
|
|
93
|
+
else if (options.now() > deadline) throw new QrError("scan-timeout", "scan deadline exceeded");
|
|
94
|
+
const outcome = decoder.receive(text);
|
|
95
|
+
if (outcome.status === "fatal") throw new QrError("scan-failed", outcome.error.message);
|
|
96
|
+
const { state } = decoder;
|
|
97
|
+
if (outcome.status === "rejected" && outcome.error.code === "UnexpectedType" && state.phase === "collecting") throw new QrError("scan-failed", "UR type changed mid-scan");
|
|
98
|
+
if (state.phase === "complete") {
|
|
99
|
+
done = {
|
|
100
|
+
status: "complete",
|
|
101
|
+
payload: {
|
|
102
|
+
type: state.value.type,
|
|
103
|
+
cbor: state.value.message
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return done;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
status: "progress",
|
|
110
|
+
ratio: decoder.progress.ratio
|
|
111
|
+
};
|
|
112
|
+
} };
|
|
113
|
+
};
|
|
114
|
+
// src/qr/request.ts
|
|
115
|
+
/**
|
|
116
|
+
* `readQrRequest` — a scanned sign-request UR payload to the engine's `QrRequest`: strictly decoded
|
|
117
|
+
* through `@qntx/ur`'s interop codecs (Keystone layout only for TRON), with every foreign failure
|
|
118
|
+
* folded into `QrError`. The result is still untrusted: the kernel re-normalizes whatever sign
|
|
119
|
+
* request the pipeline builds from it.
|
|
120
|
+
*/
|
|
121
|
+
const HARDENED$3 = 2147483648;
|
|
122
|
+
/** `ur`'s body decoded by `codec`; the codec's own tag must name the payload type. */
|
|
123
|
+
const decode$1 = (payload, codec) => qrGuard("malformed", () => fromUr(Ur.fromCborData(payload.type, payload.cbor), codec));
|
|
124
|
+
/** A BIP-32 keypath as the kernel's KeyPath numbers; wildcards and ranges are refused. */
|
|
125
|
+
const base = (path, origin) => {
|
|
126
|
+
if (path.components.length === 0) malformed("empty derivation path");
|
|
127
|
+
const steps = [];
|
|
128
|
+
for (const component of path.components) {
|
|
129
|
+
if (component.kind !== "index") malformed("non-concrete derivation path");
|
|
130
|
+
steps.push(component.index + (component.hardened ? HARDENED$3 : 0));
|
|
131
|
+
}
|
|
132
|
+
if (path.sourceFingerprint === void 0) throw new QrError("fingerprint-missing", "request carries no source fingerprint");
|
|
133
|
+
const declared = origin === void 0 ? void 0 : safeDisplayText(origin, 64);
|
|
134
|
+
return {
|
|
135
|
+
path: steps,
|
|
136
|
+
xfp: path.sourceFingerprint,
|
|
137
|
+
...declared === void 0 ? {} : { declared }
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
const requestId = (bytes) => {
|
|
141
|
+
if (bytes === void 0) return;
|
|
142
|
+
if (bytes.length !== 16) malformed("requestId must be a 16-byte UUID");
|
|
143
|
+
return bytes;
|
|
144
|
+
};
|
|
145
|
+
const ethChainId = (value) => {
|
|
146
|
+
if (value === void 0) return;
|
|
147
|
+
if (!Number.isSafeInteger(value) || value < 1) malformed("chainId must be a safe integer in [1, 2^53)");
|
|
148
|
+
return value;
|
|
149
|
+
};
|
|
150
|
+
const evmAddress = (bytes) => {
|
|
151
|
+
if (bytes === void 0) return;
|
|
152
|
+
if (bytes.length !== 20) malformed("address must be 20 bytes");
|
|
153
|
+
return `0x${bytesToHex$1(bytes)}`;
|
|
154
|
+
};
|
|
155
|
+
const accessList = (list) => (list ?? []).map((entry) => ({
|
|
156
|
+
address: entry.address,
|
|
157
|
+
storageKeys: [...entry.storageKeys]
|
|
158
|
+
}));
|
|
159
|
+
const bigintField = (value, name) => {
|
|
160
|
+
if (value === void 0) malformed(`transaction is missing ${name}`);
|
|
161
|
+
return `0x${value.toString(16)}`;
|
|
162
|
+
};
|
|
163
|
+
const evmTx = (signData) => {
|
|
164
|
+
const [first] = signData;
|
|
165
|
+
if (first === 4) unsupported("eip7702 envelopes are not accepted over QR");
|
|
166
|
+
const parsed = qrGuard("malformed", () => parseTransaction(`0x${bytesToHex$1(signData)}`));
|
|
167
|
+
const type = parsed.type ?? "legacy";
|
|
168
|
+
if (type !== "legacy" && type !== "eip2930" && type !== "eip1559") unsupported(`envelope type ${type}`);
|
|
169
|
+
if (type === "legacy" && parsed.chainId === void 0) unsupported("a legacy transaction without an EIP-155 chain id is replayable");
|
|
170
|
+
const shared = {
|
|
171
|
+
chainId: parsed.chainId ?? malformed("typed transaction is missing chainId"),
|
|
172
|
+
nonce: parsed.nonce ?? malformed("transaction is missing nonce"),
|
|
173
|
+
gas: bigintField(parsed.gas, "gas"),
|
|
174
|
+
value: `0x${(parsed.value ?? 0n).toString(16)}`,
|
|
175
|
+
data: parsed.data ?? "0x",
|
|
176
|
+
...parsed.to === void 0 || parsed.to === null ? {} : { to: parsed.to }
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
tx: type === "legacy" ? {
|
|
180
|
+
...shared,
|
|
181
|
+
type,
|
|
182
|
+
gasPrice: bigintField(parsed.gasPrice, "gasPrice")
|
|
183
|
+
} : type === "eip2930" ? {
|
|
184
|
+
...shared,
|
|
185
|
+
type,
|
|
186
|
+
gasPrice: bigintField(parsed.gasPrice, "gasPrice"),
|
|
187
|
+
accessList: accessList(parsed.accessList)
|
|
188
|
+
} : {
|
|
189
|
+
...shared,
|
|
190
|
+
type,
|
|
191
|
+
accessList: accessList(parsed.accessList),
|
|
192
|
+
maxFeePerGas: bigintField(parsed.maxFeePerGas, "maxFeePerGas"),
|
|
193
|
+
maxPriorityFeePerGas: bigintField(parsed.maxPriorityFeePerGas, "maxPriorityFeePerGas")
|
|
194
|
+
},
|
|
195
|
+
typeByte: type === "legacy" ? "legacy" : first ?? 0
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
const utf8 = (bytes) => qrGuard("malformed", () => new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
199
|
+
const nonEmpty = (bytes, what) => {
|
|
200
|
+
if (bytes.length === 0) malformed(`${what} must not be empty`);
|
|
201
|
+
return bytes;
|
|
202
|
+
};
|
|
203
|
+
const readEth = (payload) => {
|
|
204
|
+
const req = decode$1(payload, ethSignRequestCodec);
|
|
205
|
+
nonEmpty(req.signData, "signData");
|
|
206
|
+
const head = base(req.derivationPath, req.origin);
|
|
207
|
+
const id = requestId(req.requestId);
|
|
208
|
+
const address = evmAddress(req.address);
|
|
209
|
+
const chainId = ethChainId(req.chainId);
|
|
210
|
+
const tail = {
|
|
211
|
+
...id === void 0 ? {} : { requestId: id },
|
|
212
|
+
...chainId === void 0 ? {} : { chainId },
|
|
213
|
+
...address === void 0 ? {} : { address }
|
|
214
|
+
};
|
|
215
|
+
if (req.dataType === "transaction" || req.dataType === "typedTransaction") {
|
|
216
|
+
const { tx } = evmTx(req.signData);
|
|
217
|
+
if (bytesToHex$1(evmSigningPreimage(tx)) !== bytesToHex$1(req.signData)) malformed("transaction does not re-serialize to the signed bytes");
|
|
218
|
+
return {
|
|
219
|
+
...head,
|
|
220
|
+
...tail,
|
|
221
|
+
family: "evm",
|
|
222
|
+
kind: "transaction",
|
|
223
|
+
tx,
|
|
224
|
+
data: req.signData
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if (req.dataType === "typedData") return {
|
|
228
|
+
...head,
|
|
229
|
+
...tail,
|
|
230
|
+
family: "evm",
|
|
231
|
+
kind: "typed-data",
|
|
232
|
+
typedData: utf8(req.signData)
|
|
233
|
+
};
|
|
234
|
+
return {
|
|
235
|
+
...head,
|
|
236
|
+
...tail,
|
|
237
|
+
family: "evm",
|
|
238
|
+
kind: "personal-message",
|
|
239
|
+
message: req.signData
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
const solanaAddress = (bytes) => {
|
|
243
|
+
if (bytes === void 0) return;
|
|
244
|
+
if (bytes.length === 32) return base58.encode(bytes);
|
|
245
|
+
const text = qrGuard("malformed", () => new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
246
|
+
if (!/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(text)) malformed("solana address is neither 32 bytes nor base58 text");
|
|
247
|
+
return text;
|
|
248
|
+
};
|
|
249
|
+
const readSol = (payload) => {
|
|
250
|
+
const req = decode$1(payload, solSignRequestCodec);
|
|
251
|
+
if (req.signType !== "transaction" && req.signType !== "message") unsupported(`solana signType ${req.signType}`);
|
|
252
|
+
nonEmpty(req.signData, "signData");
|
|
253
|
+
const id = requestId(req.requestId);
|
|
254
|
+
const address = solanaAddress(req.address);
|
|
255
|
+
return {
|
|
256
|
+
...base(req.derivationPath, req.origin),
|
|
257
|
+
...id === void 0 ? {} : { requestId: id },
|
|
258
|
+
...address === void 0 ? {} : { address },
|
|
259
|
+
family: "solana",
|
|
260
|
+
kind: req.signType === "transaction" ? "transaction" : "message",
|
|
261
|
+
data: req.signData
|
|
262
|
+
};
|
|
263
|
+
};
|
|
264
|
+
const tronAddress = (bytes) => {
|
|
265
|
+
if (bytes === void 0) return;
|
|
266
|
+
if (bytes.length === 21 && bytes[0] === 65) return tronAddressFromBytes(bytes);
|
|
267
|
+
const text = qrGuard("malformed", () => new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
268
|
+
if (!isTronAddress(text)) malformed("tron address is neither 21 raw bytes nor base58check text");
|
|
269
|
+
return text;
|
|
270
|
+
};
|
|
271
|
+
const readTron = (payload) => {
|
|
272
|
+
let req;
|
|
273
|
+
try {
|
|
274
|
+
req = decode$1(payload, tronSignRequestCodec);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error instanceof QrError && error.code === "malformed") {
|
|
277
|
+
try {
|
|
278
|
+
decode$1(payload, oneKeyTronSignRequestCodec);
|
|
279
|
+
} catch {
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
return unsupported("OneKey tron-sign-request layout");
|
|
283
|
+
}
|
|
284
|
+
throw error;
|
|
285
|
+
}
|
|
286
|
+
if (req.dataType === "messageV1") unsupported("tron messageV1");
|
|
287
|
+
nonEmpty(req.signData, "signData");
|
|
288
|
+
const id = requestId(req.requestId);
|
|
289
|
+
const address = tronAddress(req.address);
|
|
290
|
+
return {
|
|
291
|
+
...base(req.derivationPath, req.origin),
|
|
292
|
+
...id === void 0 ? {} : { requestId: id },
|
|
293
|
+
...address === void 0 ? {} : { address },
|
|
294
|
+
family: "tron",
|
|
295
|
+
kind: req.dataType === "transaction" ? "transaction" : "message",
|
|
296
|
+
data: req.signData
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
const readPsbt = (payload) => {
|
|
300
|
+
const codec = payload.type === "crypto-psbt" ? cryptoPsbtCodec : psbtCodec;
|
|
301
|
+
const { bytes } = decode$1(payload, codec);
|
|
302
|
+
return {
|
|
303
|
+
family: "bitcoin",
|
|
304
|
+
kind: "psbt",
|
|
305
|
+
urType: payload.type === "crypto-psbt" ? "crypto-psbt" : "psbt",
|
|
306
|
+
psbt: bytes
|
|
307
|
+
};
|
|
308
|
+
};
|
|
309
|
+
const readQrRequest = (payload) => {
|
|
310
|
+
switch (payload.type) {
|
|
311
|
+
case "eth-sign-request": return readEth(payload);
|
|
312
|
+
case "sol-sign-request": return readSol(payload);
|
|
313
|
+
case "tron-sign-request": return readTron(payload);
|
|
314
|
+
case "crypto-psbt":
|
|
315
|
+
case "psbt": return readPsbt(payload);
|
|
316
|
+
default: return unsupported(`UR type ${payload.type} is not a sign request`);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
// src/qr/response.ts
|
|
320
|
+
/**
|
|
321
|
+
* `qrResponse` — a verified `chain.sign` result as the UR signature the watching wallet expects.
|
|
322
|
+
* Before anything is encoded the result is re-verified against the request's own bytes (recovered
|
|
323
|
+
* signer for EVM/TRON, ed25519 verify for Solana, unchanged-PSBT-plus-new-signature for Bitcoin),
|
|
324
|
+
* so a wrong result ends in `result-mismatch` and produces no payload.
|
|
325
|
+
*/
|
|
326
|
+
const encode$1 = (type, value, codec) => qrGuard("result-mismatch", () => ({
|
|
327
|
+
type,
|
|
328
|
+
cbor: toUr(value, codec).toCborData()
|
|
329
|
+
}));
|
|
330
|
+
const fixed32 = (value) => {
|
|
331
|
+
const bytes = hexToBytes((value ?? "0x").slice(2).padStart(64, "0"));
|
|
332
|
+
if (bytes.length !== 32) resultMismatch("signature component is not 32 bytes");
|
|
333
|
+
return bytes;
|
|
334
|
+
};
|
|
335
|
+
/** Big-endian, leading-zero-free — the minimal `v` Keystone firmware writes for legacy txs. */
|
|
336
|
+
const minimalInt = (value) => {
|
|
337
|
+
const hex = value.toString(16);
|
|
338
|
+
return hexToBytes(hex.length % 2 === 0 ? hex : `0${hex}`);
|
|
339
|
+
};
|
|
340
|
+
/** A `chain.sign`-shaped request for `recoverEvmSigner`; account/network fields are unused there. */
|
|
341
|
+
const evmProbe$1 = (request) => {
|
|
342
|
+
const network = `eip155:${(request.kind === "transaction" ? request.tx.chainId : request.chainId) ?? 1}`;
|
|
343
|
+
const account = {
|
|
344
|
+
kind: "hd",
|
|
345
|
+
walletId: "",
|
|
346
|
+
groupIndex: 0,
|
|
347
|
+
template: "evm.bip44"
|
|
348
|
+
};
|
|
349
|
+
if (request.kind === "transaction") return {
|
|
350
|
+
kind: "evm.transaction",
|
|
351
|
+
network,
|
|
352
|
+
account,
|
|
353
|
+
tx: request.tx
|
|
354
|
+
};
|
|
355
|
+
if (request.kind === "typed-data") return {
|
|
356
|
+
kind: "evm.typed-data",
|
|
357
|
+
network,
|
|
358
|
+
account,
|
|
359
|
+
typedData: request.typedData
|
|
360
|
+
};
|
|
361
|
+
return {
|
|
362
|
+
kind: "evm.personal-message",
|
|
363
|
+
network,
|
|
364
|
+
account,
|
|
365
|
+
message: request.message
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
const evmResponse = (request, result, signer) => {
|
|
369
|
+
if (request.family !== "evm") resultMismatch("not an evm request");
|
|
370
|
+
let signature;
|
|
371
|
+
if (request.kind === "transaction") {
|
|
372
|
+
if (result.kind !== "evm.transaction") resultMismatch("expected an evm.transaction result");
|
|
373
|
+
const parsed = qrGuard("result-mismatch", () => parseTransaction(`0x${bytesToHex$1(result.signedTransaction)}`));
|
|
374
|
+
if (parsed.r === void 0 || parsed.s === void 0 || parsed.yParity === void 0) resultMismatch("the signed envelope carries no signature");
|
|
375
|
+
if (qrGuard("result-mismatch", () => recoverEvmSigner(evmProbe$1(request), { signedTransaction: result.signedTransaction })).toLowerCase() !== signer.address.toLowerCase()) resultMismatch("signature does not recover to the signer");
|
|
376
|
+
const v = request.tx.type === "legacy" ? minimalInt(parsed.yParity + 27 + request.tx.chainId * 2 + 8) : new Uint8Array([parsed.yParity]);
|
|
377
|
+
signature = concatBytes(fixed32(parsed.r), fixed32(parsed.s), v);
|
|
378
|
+
} else {
|
|
379
|
+
if (result.kind !== "evm.typed-data" && result.kind !== "evm.personal-message") resultMismatch("expected an evm signature result");
|
|
380
|
+
if (result.signature.length !== 65) resultMismatch("expected a 65-byte signature");
|
|
381
|
+
if (qrGuard("result-mismatch", () => recoverEvmSigner(evmProbe$1(request), { signature: result.signature })).toLowerCase() !== signer.address.toLowerCase()) resultMismatch("signature does not recover to the signer");
|
|
382
|
+
const [last] = result.signature.subarray(64);
|
|
383
|
+
if (last === void 0) resultMismatch("unexpected recovery id");
|
|
384
|
+
const recid = last - 27;
|
|
385
|
+
if (recid !== 0 && recid !== 1) resultMismatch("unexpected recovery id");
|
|
386
|
+
signature = concatBytes(result.signature.subarray(0, 64), new Uint8Array([recid + 27]));
|
|
387
|
+
}
|
|
388
|
+
return encode$1("eth-signature", {
|
|
389
|
+
...request.requestId === void 0 ? {} : { requestId: request.requestId },
|
|
390
|
+
signature
|
|
391
|
+
}, ethSignatureCodec);
|
|
392
|
+
};
|
|
393
|
+
const solanaResponse = (request, result, signer) => {
|
|
394
|
+
if (request.family !== "solana") resultMismatch("not a solana request");
|
|
395
|
+
const want = request.kind === "transaction" ? "solana.transaction" : "solana.message";
|
|
396
|
+
if (result.kind !== want) resultMismatch(`expected a ${want} result`);
|
|
397
|
+
if (result.signature.length !== 64) resultMismatch("expected a 64-byte signature");
|
|
398
|
+
if (!verifySolanaSignature(request.data, result.signature, signer.address)) resultMismatch("signature does not verify under the signer");
|
|
399
|
+
return encode$1("sol-signature", {
|
|
400
|
+
...request.requestId === void 0 ? {} : { requestId: request.requestId },
|
|
401
|
+
signature: result.signature
|
|
402
|
+
}, solSignatureCodec);
|
|
403
|
+
};
|
|
404
|
+
const tronResponse = (request, result, signer) => {
|
|
405
|
+
if (request.family !== "tron") resultMismatch("not a tron request");
|
|
406
|
+
const want = request.kind === "transaction" ? "tron.transaction" : "tron.message";
|
|
407
|
+
if (result.kind !== want) resultMismatch(`expected a ${want} result`);
|
|
408
|
+
if (result.signature.length !== 65) resultMismatch("expected a 65-byte signature");
|
|
409
|
+
if ((request.kind === "transaction" ? qrGuard("result-mismatch", () => recoverTronTransactionSigner(request.data, result.signature)) : qrGuard("result-mismatch", () => recoverTronMessageSigner(request.data, result.signature))) !== signer.address) resultMismatch("signature does not recover to the signer");
|
|
410
|
+
const [last] = result.signature.subarray(64);
|
|
411
|
+
if (last === void 0) resultMismatch("unexpected recovery id");
|
|
412
|
+
const recid = last - 27;
|
|
413
|
+
if (recid !== 0 && recid !== 1) resultMismatch("unexpected recovery id");
|
|
414
|
+
const v = request.kind === "transaction" ? recid : recid + 27;
|
|
415
|
+
return encode$1("tron-signature", {
|
|
416
|
+
...request.requestId === void 0 ? {} : { requestId: request.requestId },
|
|
417
|
+
signature: concatBytes(result.signature.subarray(0, 64), new Uint8Array([v]))
|
|
418
|
+
}, tronSignatureCodec);
|
|
419
|
+
};
|
|
420
|
+
const inputSignatures = (input) => (input.tapKeySig === void 0 ? 0 : 1) + (input.partialSig?.length ?? 0) + (input.tapScriptSig?.length ?? 0);
|
|
421
|
+
const bitcoinResponse = (request, result) => {
|
|
422
|
+
if (request.family !== "bitcoin") resultMismatch("not a bitcoin request");
|
|
423
|
+
if (result.kind !== "bitcoin.psbt") resultMismatch("expected a bitcoin.psbt result");
|
|
424
|
+
if (!samePsbtContent(request.psbt, result.signedPsbt)) resultMismatch("the signed PSBT does not match the request");
|
|
425
|
+
const [requested, signed] = qrGuard("result-mismatch", () => [Transaction$1.fromPSBT(request.psbt), Transaction$1.fromPSBT(result.signedPsbt)]);
|
|
426
|
+
let fresh = 0;
|
|
427
|
+
for (let index = 0; index < signed.inputsLength; index += 1) fresh += inputSignatures(signed.getInput(index)) - inputSignatures(requested.getInput(index));
|
|
428
|
+
if (fresh < 1) resultMismatch("the signed PSBT carries no new signature");
|
|
429
|
+
return encode$1(request.urType, { bytes: result.signedPsbt }, request.urType === "crypto-psbt" ? cryptoPsbtCodec : psbtCodec);
|
|
430
|
+
};
|
|
431
|
+
const qrResponse = (request, result, signer) => {
|
|
432
|
+
const signerOrThrow = () => {
|
|
433
|
+
if (signer === void 0) resultMismatch("the request bound no signer to verify against");
|
|
434
|
+
return signer;
|
|
435
|
+
};
|
|
436
|
+
switch (request.family) {
|
|
437
|
+
case "evm": return evmResponse(request, result, signerOrThrow());
|
|
438
|
+
case "solana": return solanaResponse(request, result, signerOrThrow());
|
|
439
|
+
case "tron": return tronResponse(request, result, signerOrThrow());
|
|
440
|
+
case "bitcoin": return bitcoinResponse(request, result);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
// src/qr/export.ts
|
|
444
|
+
/**
|
|
445
|
+
* `qrExport` — the public material `chain.exportAccount` produced, encoded as the account UR a
|
|
446
|
+
* watch-only wallet reads: `crypto-hdkey` for an EVM account node (MetaMask/Rabby),
|
|
447
|
+
* `crypto-account` for a Bitcoin account descriptor (BlueWallet/Sparrow) and
|
|
448
|
+
* `crypto-multi-accounts` for the Web wallet's per-leaf form. Only public fields are written;
|
|
449
|
+
* `device`/`name`, where a reader expects one, is the constant `"qntx"`.
|
|
450
|
+
*/
|
|
451
|
+
const fingerprint = (hex) => {
|
|
452
|
+
if (!/^[0-9a-f]{8}$/.test(hex)) resultMismatch("fingerprint must be 8 lowercase hex digits");
|
|
453
|
+
return Number.parseInt(hex, 16);
|
|
454
|
+
};
|
|
455
|
+
const accountFields = (key) => {
|
|
456
|
+
const { chainCode, parentFingerprint } = key;
|
|
457
|
+
if (chainCode === void 0 || parentFingerprint === void 0) resultMismatch("an account node carries a chain code and parent fingerprint");
|
|
458
|
+
if (key.publicKey.length !== 33 || chainCode.length !== 32) resultMismatch("bad account node key lengths");
|
|
459
|
+
return {
|
|
460
|
+
chainCode,
|
|
461
|
+
parent: fingerprint(parentFingerprint)
|
|
462
|
+
};
|
|
463
|
+
};
|
|
464
|
+
const single = (result, kind, form) => {
|
|
465
|
+
const [key] = result.keys;
|
|
466
|
+
if (result.keys.length !== 1 || key === void 0 || key.node.kind !== kind) unsupported(`${form} exports exactly one ${kind} key`);
|
|
467
|
+
return key;
|
|
468
|
+
};
|
|
469
|
+
const hdkeyExport = (result) => {
|
|
470
|
+
const key = single(result, "evm.account", "crypto-hdkey");
|
|
471
|
+
const { chainCode, parent } = accountFields(key);
|
|
472
|
+
const mfp = fingerprint(result.fingerprint);
|
|
473
|
+
const hd = {
|
|
474
|
+
kind: "derived",
|
|
475
|
+
isPrivate: false,
|
|
476
|
+
keyData: key.publicKey,
|
|
477
|
+
chainCode,
|
|
478
|
+
origin: keypath(key.path, mfp),
|
|
479
|
+
children: { components: [{
|
|
480
|
+
kind: "index",
|
|
481
|
+
index: 0,
|
|
482
|
+
hardened: false
|
|
483
|
+
}, {
|
|
484
|
+
kind: "wildcard",
|
|
485
|
+
hardened: false
|
|
486
|
+
}] },
|
|
487
|
+
parentFingerprint: parent,
|
|
488
|
+
name: "qntx",
|
|
489
|
+
note: "account.standard"
|
|
490
|
+
};
|
|
491
|
+
return {
|
|
492
|
+
payload: qrGuard("result-mismatch", () => ({
|
|
493
|
+
type: "crypto-hdkey",
|
|
494
|
+
cbor: toUr(hd, cryptoHdKeyCodec).toCborData()
|
|
495
|
+
})),
|
|
496
|
+
discloses: "evm-all-addresses"
|
|
497
|
+
};
|
|
498
|
+
};
|
|
499
|
+
const DESCRIPTOR = {
|
|
500
|
+
"btc.bip44": "pkh(@0)",
|
|
501
|
+
"btc.bip49": "sh(wpkh(@0))",
|
|
502
|
+
"btc.bip84": "wpkh(@0)",
|
|
503
|
+
"btc.bip86": "tr(@0)"
|
|
504
|
+
};
|
|
505
|
+
const accountExport = (result) => {
|
|
506
|
+
const key = single(result, "btc.account", "crypto-account");
|
|
507
|
+
if (key.node.kind !== "btc.account") unsupported("crypto-account exports a btc.account key");
|
|
508
|
+
const source = DESCRIPTOR[key.node.template];
|
|
509
|
+
if (source === void 0) unsupported(`account template ${key.node.template}`);
|
|
510
|
+
const { chainCode, parent } = accountFields(key);
|
|
511
|
+
const mfp = fingerprint(result.fingerprint);
|
|
512
|
+
const { network } = key.node;
|
|
513
|
+
const net = qrGuard("result-mismatch", () => bitcoinNetwork(network));
|
|
514
|
+
const account = {
|
|
515
|
+
masterFingerprint: mfp,
|
|
516
|
+
outputDescriptors: [{
|
|
517
|
+
source,
|
|
518
|
+
keys: [{
|
|
519
|
+
kind: "hdkey",
|
|
520
|
+
key: {
|
|
521
|
+
kind: "derived",
|
|
522
|
+
isPrivate: false,
|
|
523
|
+
keyData: key.publicKey,
|
|
524
|
+
chainCode,
|
|
525
|
+
useInfo: {
|
|
526
|
+
type: CoinType.btc,
|
|
527
|
+
network: net.coinType === 0 ? Network.mainnet : Network.btcTestnet
|
|
528
|
+
},
|
|
529
|
+
origin: keypath(key.path, mfp),
|
|
530
|
+
parentFingerprint: parent
|
|
531
|
+
}
|
|
532
|
+
}]
|
|
533
|
+
}]
|
|
534
|
+
};
|
|
535
|
+
return {
|
|
536
|
+
payload: qrGuard("result-mismatch", () => ({
|
|
537
|
+
type: "crypto-account",
|
|
538
|
+
cbor: toUr(account, cryptoAccountCodec).toCborData()
|
|
539
|
+
})),
|
|
540
|
+
discloses: "account-addresses"
|
|
541
|
+
};
|
|
542
|
+
};
|
|
543
|
+
const multiKey = (key, mfp) => {
|
|
544
|
+
if (key.node.kind === "evm.account") unsupported("evm.account belongs in crypto-hdkey, not crypto-multi-accounts");
|
|
545
|
+
if (key.node.kind === "leaf") {
|
|
546
|
+
if (key.node.template === "solana.bip44-change") {
|
|
547
|
+
if (key.curve !== "ed25519" || key.publicKey.length !== 32) resultMismatch("solana leaf must be a 32-byte ed25519 key");
|
|
548
|
+
return {
|
|
549
|
+
curve: "ed25519",
|
|
550
|
+
key: {
|
|
551
|
+
keyData: key.publicKey,
|
|
552
|
+
origin: keypath(key.path, mfp)
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
if (key.curve !== "secp256k1" || key.publicKey.length !== 33) resultMismatch("a leaf must be a 33-byte secp256k1 key");
|
|
557
|
+
return {
|
|
558
|
+
curve: "secp256k1",
|
|
559
|
+
key: {
|
|
560
|
+
kind: "derived",
|
|
561
|
+
isPrivate: false,
|
|
562
|
+
keyData: key.publicKey,
|
|
563
|
+
origin: keypath(key.path, mfp)
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
if (key.node.kind === "btc.account") {
|
|
568
|
+
const { chainCode, parent } = accountFields(key);
|
|
569
|
+
return {
|
|
570
|
+
curve: "secp256k1",
|
|
571
|
+
key: {
|
|
572
|
+
kind: "derived",
|
|
573
|
+
isPrivate: false,
|
|
574
|
+
keyData: key.publicKey,
|
|
575
|
+
chainCode,
|
|
576
|
+
origin: keypath(key.path, mfp),
|
|
577
|
+
parentFingerprint: parent
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
return unsupported("export node is not encodable as crypto-multi-accounts");
|
|
582
|
+
};
|
|
583
|
+
const multiExport = (result) => {
|
|
584
|
+
if (result.keys.length === 0) unsupported("crypto-multi-accounts exports at least one key");
|
|
585
|
+
const mfp = fingerprint(result.fingerprint);
|
|
586
|
+
const accounts = {
|
|
587
|
+
masterFingerprint: mfp,
|
|
588
|
+
keys: result.keys.map((key) => multiKey(key, mfp)),
|
|
589
|
+
device: "qntx"
|
|
590
|
+
};
|
|
591
|
+
return {
|
|
592
|
+
payload: qrGuard("result-mismatch", () => ({
|
|
593
|
+
type: "crypto-multi-accounts",
|
|
594
|
+
cbor: toUr(accounts, cryptoMultiAccountsCodec).toCborData()
|
|
595
|
+
})),
|
|
596
|
+
discloses: result.keys.some((key) => key.node.kind === "btc.account") ? "account-addresses" : "single-addresses"
|
|
597
|
+
};
|
|
598
|
+
};
|
|
599
|
+
const qrExport = (result, form) => {
|
|
600
|
+
switch (form) {
|
|
601
|
+
case "crypto-hdkey": return hdkeyExport(result);
|
|
602
|
+
case "crypto-account": return accountExport(result);
|
|
603
|
+
case "crypto-multi-accounts": return multiExport(result);
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
// src/qr/bind.ts
|
|
607
|
+
/**
|
|
608
|
+
* Pure QR request binding: a decoded `QrRequest` is matched against the selected account's
|
|
609
|
+
* authenticated facts — fingerprint, derivation path, declared address, enabled networks — and
|
|
610
|
+
* turned into candidate `RequestInput`s for the existing request pipeline. Solana and TRON
|
|
611
|
+
* transactions produce one candidate per enabled network (mainnet first): a lifetime/cluster
|
|
612
|
+
* binding failure there means trying the next candidate.
|
|
613
|
+
*
|
|
614
|
+
* No wallet I/O happens here — `QrAccountFacts` carries everything the wallet already proved.
|
|
615
|
+
*/
|
|
616
|
+
const HARDENED$2 = 2147483648;
|
|
617
|
+
/** The leaf path each family's account template derives at for `groupIndex`. */
|
|
618
|
+
const leafPath = (family, groupIndex) => family === "evm" ? [
|
|
619
|
+
2147483692,
|
|
620
|
+
2147483708,
|
|
621
|
+
HARDENED$2,
|
|
622
|
+
0,
|
|
623
|
+
groupIndex
|
|
624
|
+
] : family === "tron" ? [
|
|
625
|
+
2147483692,
|
|
626
|
+
2147483843,
|
|
627
|
+
HARDENED$2,
|
|
628
|
+
0,
|
|
629
|
+
groupIndex
|
|
630
|
+
] : [
|
|
631
|
+
2147483692,
|
|
632
|
+
2147484149,
|
|
633
|
+
groupIndex + HARDENED$2,
|
|
634
|
+
HARDENED$2
|
|
635
|
+
];
|
|
636
|
+
const samePath = (a, b) => a.length === b.length && a.every((step, index) => step === b[index]);
|
|
637
|
+
const mismatch = (what) => new QrError("account-mismatch", `the request's ${what} is not this account's`);
|
|
638
|
+
/**
|
|
639
|
+
* The fingerprint, the leaf path and — when the request declares one — the address must all be the
|
|
640
|
+
* selected account's, or the request was meant for another key entirely.
|
|
641
|
+
*/
|
|
642
|
+
const requireAccount = (request, facts, family) => {
|
|
643
|
+
if (request.xfp !== facts.fingerprint) throw mismatch("fingerprint");
|
|
644
|
+
if (!samePath(request.path, leafPath(family, facts.groupIndex))) throw mismatch("derivation path");
|
|
645
|
+
const { address } = request;
|
|
646
|
+
if (address === void 0) return;
|
|
647
|
+
const own = facts.addresses[family];
|
|
648
|
+
if (family === "evm" ? address.toLowerCase() !== own?.toLowerCase() : address !== own) throw mismatch("address");
|
|
649
|
+
};
|
|
650
|
+
const qrOrigin = (declared) => ({
|
|
651
|
+
kind: "qr",
|
|
652
|
+
...declared === void 0 ? {} : { declared }
|
|
653
|
+
});
|
|
654
|
+
/** `domain.chainId` as a number, when present — the same reading wallet-chains' normalize uses. */
|
|
655
|
+
const typedDataChainId = (typedData) => {
|
|
656
|
+
const { json } = qrGuard("malformed", () => parseTypedData(typedData));
|
|
657
|
+
const { chainId } = json.domain;
|
|
658
|
+
if (chainId === void 0) return;
|
|
659
|
+
const numeric = typeof chainId === "number" ? chainId : typeof chainId === "string" && /^(?:[0-9]+|0x[0-9a-fA-F]+)$/.test(chainId) ? Number(chainId) : NaN;
|
|
660
|
+
if (!Number.isSafeInteger(numeric) || numeric < 0) throw new QrError("malformed", "typed-data domain.chainId is malformed");
|
|
661
|
+
return numeric;
|
|
662
|
+
};
|
|
663
|
+
const evmCandidates = (request, facts) => {
|
|
664
|
+
requireAccount(request, facts, "evm");
|
|
665
|
+
const account = {
|
|
666
|
+
kind: "hd",
|
|
667
|
+
walletId: facts.walletId,
|
|
668
|
+
groupIndex: facts.groupIndex,
|
|
669
|
+
template: "evm.bip44"
|
|
670
|
+
};
|
|
671
|
+
const origin = qrOrigin(request.declared);
|
|
672
|
+
if (request.kind === "transaction") {
|
|
673
|
+
const { tx } = request;
|
|
674
|
+
if (tx.type === "eip7702") throw new QrError("unsupported", "an EIP-7702 authorization cannot be a QR request");
|
|
675
|
+
if (tx.to === void 0) throw new QrError("contract-creation", "a QR request cannot create a contract");
|
|
676
|
+
if (request.chainId !== void 0 && request.chainId !== tx.chainId) throw new QrError("chain-mismatch", "the request's chain id is not its transaction's");
|
|
677
|
+
const network = `eip155:${tx.chainId}`;
|
|
678
|
+
if (!facts.networks.evm.includes(network)) throw new QrError("network-unknown", `network ${network} is not enabled`);
|
|
679
|
+
if (tx.data !== "0x" && tx.to.toLowerCase() === facts.addresses.evm?.toLowerCase()) throw new QrError("self-call", "a QR request cannot call the signing account");
|
|
680
|
+
return {
|
|
681
|
+
method: "evm-sign",
|
|
682
|
+
origin,
|
|
683
|
+
account,
|
|
684
|
+
network,
|
|
685
|
+
request: {
|
|
686
|
+
kind: "evm.transaction",
|
|
687
|
+
network,
|
|
688
|
+
account,
|
|
689
|
+
tx
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
if (request.kind === "typed-data") {
|
|
694
|
+
const domainChain = typedDataChainId(request.typedData);
|
|
695
|
+
if (domainChain !== void 0 && request.chainId !== void 0 && domainChain !== request.chainId) throw new QrError("chain-mismatch", "the request's chain id is not its domain's");
|
|
696
|
+
const bound = domainChain ?? request.chainId;
|
|
697
|
+
const network = `eip155:${bound ?? 1}`;
|
|
698
|
+
if (bound !== void 0 && !facts.networks.evm.includes(network)) throw new QrError("network-unknown", `network ${network} is not enabled`);
|
|
699
|
+
return {
|
|
700
|
+
method: "evm-sign",
|
|
701
|
+
origin,
|
|
702
|
+
account,
|
|
703
|
+
network,
|
|
704
|
+
request: {
|
|
705
|
+
kind: "evm.typed-data",
|
|
706
|
+
network,
|
|
707
|
+
account,
|
|
708
|
+
typedData: request.typedData
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
const network = `eip155:${request.chainId ?? 1}`;
|
|
713
|
+
return {
|
|
714
|
+
method: "evm-sign",
|
|
715
|
+
origin,
|
|
716
|
+
account,
|
|
717
|
+
network,
|
|
718
|
+
request: {
|
|
719
|
+
kind: "evm.personal-message",
|
|
720
|
+
network,
|
|
721
|
+
account,
|
|
722
|
+
message: request.message
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
};
|
|
726
|
+
/** Bitcoin-style shortvec: 7-bit groups, low to high, continuation bit on all but the last. */
|
|
727
|
+
const shortvec = (n) => {
|
|
728
|
+
const out = [];
|
|
729
|
+
let rest = n;
|
|
730
|
+
do {
|
|
731
|
+
let byte = rest % 128;
|
|
732
|
+
rest = Math.floor(rest / 128);
|
|
733
|
+
if (rest > 0) byte += 128;
|
|
734
|
+
out.push(byte);
|
|
735
|
+
} while (rest > 0);
|
|
736
|
+
return new Uint8Array(out);
|
|
737
|
+
};
|
|
738
|
+
const solanaCandidates = (request, facts) => {
|
|
739
|
+
requireAccount(request, facts, "solana");
|
|
740
|
+
const address = facts.addresses.solana;
|
|
741
|
+
if (address === void 0) throw new QrError("wallet-unsupported", "the account has no solana address");
|
|
742
|
+
const account = {
|
|
743
|
+
walletId: facts.walletId,
|
|
744
|
+
groupIndex: facts.groupIndex,
|
|
745
|
+
address
|
|
746
|
+
};
|
|
747
|
+
const origin = qrOrigin(request.declared);
|
|
748
|
+
if (request.kind === "message") {
|
|
749
|
+
const [network] = facts.networks.solana;
|
|
750
|
+
if (network === void 0) throw new QrError("network-unknown", "no solana network is enabled");
|
|
751
|
+
return [{
|
|
752
|
+
method: "solana-sign",
|
|
753
|
+
origin,
|
|
754
|
+
account,
|
|
755
|
+
network,
|
|
756
|
+
request: {
|
|
757
|
+
kind: "message",
|
|
758
|
+
message: request.data
|
|
759
|
+
}
|
|
760
|
+
}];
|
|
761
|
+
}
|
|
762
|
+
const parsed = qrGuard("malformed", () => parseCompiledMessage(request.data));
|
|
763
|
+
if (!parsed.staticAccounts.slice(0, parsed.numRequiredSignatures).includes(account.address)) throw mismatch("signer");
|
|
764
|
+
const count = shortvec(parsed.numRequiredSignatures);
|
|
765
|
+
const wire = new Uint8Array(count.length + 64 * parsed.numRequiredSignatures + request.data.length);
|
|
766
|
+
wire.set(count, 0);
|
|
767
|
+
wire.set(request.data, count.length + 64 * parsed.numRequiredSignatures);
|
|
768
|
+
return facts.networks.solana.map((network) => ({
|
|
769
|
+
method: "solana-sign",
|
|
770
|
+
origin,
|
|
771
|
+
account,
|
|
772
|
+
network,
|
|
773
|
+
request: {
|
|
774
|
+
kind: "transactions",
|
|
775
|
+
transactions: [wire],
|
|
776
|
+
send: false
|
|
777
|
+
}
|
|
778
|
+
}));
|
|
779
|
+
};
|
|
780
|
+
const tronCandidates = (request, facts) => {
|
|
781
|
+
requireAccount(request, facts, "tron");
|
|
782
|
+
const account = {
|
|
783
|
+
kind: "hd",
|
|
784
|
+
walletId: facts.walletId,
|
|
785
|
+
groupIndex: facts.groupIndex,
|
|
786
|
+
template: "tron.bip44"
|
|
787
|
+
};
|
|
788
|
+
const origin = qrOrigin(request.declared);
|
|
789
|
+
if (request.kind === "message") {
|
|
790
|
+
const [network] = facts.networks.tron;
|
|
791
|
+
if (network === void 0) throw new QrError("network-unknown", "no tron network is enabled");
|
|
792
|
+
return [{
|
|
793
|
+
method: "tron-sign",
|
|
794
|
+
origin,
|
|
795
|
+
account,
|
|
796
|
+
network,
|
|
797
|
+
request: {
|
|
798
|
+
kind: "tron.message",
|
|
799
|
+
network,
|
|
800
|
+
account,
|
|
801
|
+
message: request.data
|
|
802
|
+
}
|
|
803
|
+
}];
|
|
804
|
+
}
|
|
805
|
+
return facts.networks.tron.map((network) => ({
|
|
806
|
+
method: "tron-sign",
|
|
807
|
+
origin,
|
|
808
|
+
account,
|
|
809
|
+
network,
|
|
810
|
+
request: {
|
|
811
|
+
kind: "tron.transaction",
|
|
812
|
+
network,
|
|
813
|
+
account,
|
|
814
|
+
rawData: request.data
|
|
815
|
+
}
|
|
816
|
+
}));
|
|
817
|
+
};
|
|
818
|
+
const SIGHASH_ALL = 1;
|
|
819
|
+
const SIGHASH_DEFAULT = 0;
|
|
820
|
+
const SIGHASH_SINGLE_ANYONECANPAY = 131;
|
|
821
|
+
/** The script type that template's prevout must carry — `OutScript.decode` names are lowercase. */
|
|
822
|
+
const PURPOSE_SCRIPT = {
|
|
823
|
+
44: "pkh",
|
|
824
|
+
49: "sh",
|
|
825
|
+
84: "wpkh",
|
|
826
|
+
86: "tr"
|
|
827
|
+
};
|
|
828
|
+
const inputEntries = (input) => [...(input.bip32Derivation ?? []).map(([, der]) => ({
|
|
829
|
+
fingerprint: der.fingerprint,
|
|
830
|
+
path: der.path,
|
|
831
|
+
tap: false,
|
|
832
|
+
leafHashes: 0
|
|
833
|
+
})), ...(input.tapBip32Derivation ?? []).map(([, entry]) => ({
|
|
834
|
+
fingerprint: entry.der.fingerprint,
|
|
835
|
+
path: entry.der.path,
|
|
836
|
+
tap: true,
|
|
837
|
+
leafHashes: entry.hashes.length
|
|
838
|
+
}))];
|
|
839
|
+
/**
|
|
840
|
+
* The prevout script a signable input spends: `witnessUtxo` when present, else the output its
|
|
841
|
+
* `nonWitnessUtxo` (the decoded previous transaction) points at — needed to check the path's
|
|
842
|
+
* purpose against the script type.
|
|
843
|
+
*/
|
|
844
|
+
const prevoutScript = (input) => {
|
|
845
|
+
const witness = input.witnessUtxo?.script;
|
|
846
|
+
if (witness !== void 0) return witness;
|
|
847
|
+
const prev = input.nonWitnessUtxo;
|
|
848
|
+
const vout = input.index;
|
|
849
|
+
if (prev === void 0 || vout === void 0 || vout >= prev.outputs.length) return;
|
|
850
|
+
const script = prev.outputs[vout]?.script;
|
|
851
|
+
return script instanceof Uint8Array ? script : void 0;
|
|
852
|
+
};
|
|
853
|
+
const multisig = (why) => new QrError("multisig-unsupported", `the kernel signs single-sig only — ${why}`);
|
|
854
|
+
const bitcoinCandidates = (request, facts) => {
|
|
855
|
+
const tx = qrGuard("malformed", () => Transaction.fromPSBT(request.psbt));
|
|
856
|
+
const sign = [];
|
|
857
|
+
const coins = /* @__PURE__ */ new Set();
|
|
858
|
+
for (let index = 0; index < tx.inputsLength; index += 1) {
|
|
859
|
+
const input = tx.getInput(index);
|
|
860
|
+
const ours = inputEntries(input).filter((entry) => entry.fingerprint === facts.fingerprint);
|
|
861
|
+
if (ours.length === 0) continue;
|
|
862
|
+
if (input.witnessScript !== void 0 && input.witnessScript.length > 0) throw multisig("a witness script on this input cannot be signed");
|
|
863
|
+
for (const entry of ours) {
|
|
864
|
+
const [purpose, coin, group, change, i] = entry.path;
|
|
865
|
+
if (entry.path.length !== 5 || purpose === void 0 || coin === void 0 || group === void 0 || change === void 0 || i === void 0 || purpose < HARDENED$2 || coin < HARDENED$2 || coin - HARDENED$2 > 1 || group < HARDENED$2 || group - HARDENED$2 !== facts.groupIndex || change >= HARDENED$2 || change > 1 || i >= HARDENED$2) throw mismatch("derivation path");
|
|
866
|
+
const p = purpose - HARDENED$2;
|
|
867
|
+
const c = coin - HARDENED$2;
|
|
868
|
+
const template = PURPOSE_TEMPLATE[p];
|
|
869
|
+
if (template === void 0) throw mismatch("derivation path");
|
|
870
|
+
if (entry.leafHashes > 0) throw multisig("a taproot script-path entry cannot be signed");
|
|
871
|
+
const script = prevoutScript(input);
|
|
872
|
+
const decoded = script === void 0 ? void 0 : qrGuard("malformed", () => OutScript.decode(script));
|
|
873
|
+
if (decoded === void 0) throw mismatch("script");
|
|
874
|
+
if (decoded.type === "ms" || decoded.type === "wsh" || decoded.type === "tr_ms" || decoded.type === "tr_ns") throw multisig("the input's script is not a single-key output");
|
|
875
|
+
if (decoded.type !== PURPOSE_SCRIPT[p]) throw mismatch("script");
|
|
876
|
+
if (decoded.type === "sh") {
|
|
877
|
+
const redeem = input.redeemScript;
|
|
878
|
+
const inner = redeem === void 0 ? void 0 : qrGuard("malformed", () => OutScript.decode(redeem));
|
|
879
|
+
if (inner === void 0 || inner.type !== "wpkh") throw multisig("the redeem script is not a p2wpkh wrapper");
|
|
880
|
+
}
|
|
881
|
+
const { sighashType } = input;
|
|
882
|
+
const sigHash = sighashType === void 0 || sighashType === SIGHASH_ALL || sighashType === SIGHASH_DEFAULT && entry.tap ? "ALL" : sighashType === SIGHASH_SINGLE_ANYONECANPAY ? "SINGLE|ANYONECANPAY" : (() => {
|
|
883
|
+
throw new QrError("sighash-unsupported", `input ${index} requests an unsupported sighash`);
|
|
884
|
+
})();
|
|
885
|
+
coins.add(c);
|
|
886
|
+
sign.push({
|
|
887
|
+
index,
|
|
888
|
+
sigHash,
|
|
889
|
+
template
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
if (sign.length === 0) throw new QrError("nothing-to-sign", "no input carries this wallet's fingerprint");
|
|
894
|
+
const coinSet = [...coins];
|
|
895
|
+
if (coinSet.length !== 1) throw new QrError("network-ambiguous", "the inputs span several bitcoin networks");
|
|
896
|
+
const [coin] = coinSet;
|
|
897
|
+
let network;
|
|
898
|
+
if (coin === 0) {
|
|
899
|
+
network = BITCOIN_MAINNET;
|
|
900
|
+
if (!facts.networks.bitcoin.includes(network)) throw new QrError("network-unknown", `network ${network} is not enabled`);
|
|
901
|
+
} else {
|
|
902
|
+
const testnets = facts.networks.bitcoin.filter((n) => n !== BITCOIN_MAINNET);
|
|
903
|
+
const [testnet] = testnets;
|
|
904
|
+
if (testnets.length !== 1 || testnet === void 0) throw new QrError("network-ambiguous", "the inputs need a test network but none or several are enabled");
|
|
905
|
+
network = testnet;
|
|
906
|
+
}
|
|
907
|
+
const input = {
|
|
908
|
+
psbt: request.psbt,
|
|
909
|
+
sign
|
|
910
|
+
};
|
|
911
|
+
return [{
|
|
912
|
+
method: "bitcoin-sign",
|
|
913
|
+
origin: qrOrigin(void 0),
|
|
914
|
+
account: {
|
|
915
|
+
walletId: facts.walletId,
|
|
916
|
+
groupIndex: facts.groupIndex
|
|
917
|
+
},
|
|
918
|
+
network,
|
|
919
|
+
request: {
|
|
920
|
+
kind: "psbt",
|
|
921
|
+
psbts: [input],
|
|
922
|
+
send: false
|
|
923
|
+
}
|
|
924
|
+
}];
|
|
925
|
+
};
|
|
926
|
+
/**
|
|
927
|
+
* The candidate request inputs for a decoded QR request, in try order — one, except Solana and TRON
|
|
928
|
+
* transactions which produce one per enabled network for the facade to try in order. All
|
|
929
|
+
* account/chain/network checks happen here; the pipeline repeats the semantic ones.
|
|
930
|
+
*/
|
|
931
|
+
const bindQrRequest = (request, facts) => request.family === "evm" ? [evmCandidates(request, facts)] : request.family === "solana" ? solanaCandidates(request, facts) : request.family === "tron" ? tronCandidates(request, facts) : bitcoinCandidates(request, facts);
|
|
932
|
+
// src/qr/inspect.ts
|
|
933
|
+
/**
|
|
934
|
+
* `inspectQrRequest` — wallet-free inspection of a decoded QR sign request: the display the host
|
|
935
|
+
* can render plus the digests a signing device would show. The digests prove the meaning of the
|
|
936
|
+
* scanned bytes, not that the desktop sent the same bytes to the device — compare the digest shown
|
|
937
|
+
* on the device.
|
|
938
|
+
*/
|
|
939
|
+
const hex$1 = (bytes) => `0x${bytesToHex$1(bytes)}`;
|
|
940
|
+
/**
|
|
941
|
+
* `planEvm` only copies the task key into the plan — the digest depends on the request bytes alone
|
|
942
|
+
* — so inspection runs with the request path and a fixed placeholder point.
|
|
943
|
+
*/
|
|
944
|
+
const probeKey = (request) => ({
|
|
945
|
+
path: request.path,
|
|
946
|
+
publicKey: hexToBytes(`02${"00".repeat(32)}`)
|
|
947
|
+
});
|
|
948
|
+
const account = {
|
|
949
|
+
kind: "hd",
|
|
950
|
+
walletId: "",
|
|
951
|
+
groupIndex: 0,
|
|
952
|
+
template: ""
|
|
953
|
+
};
|
|
954
|
+
const evmNetwork = (request) => {
|
|
955
|
+
return `eip155:${(request.kind === "transaction" ? request.tx.chainId : request.chainId) ?? 1}`;
|
|
956
|
+
};
|
|
957
|
+
const solanaProbe = (request) => request.kind === "transaction" ? {
|
|
958
|
+
kind: "solana.transaction",
|
|
959
|
+
network: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
960
|
+
account,
|
|
961
|
+
message: request.data
|
|
962
|
+
} : {
|
|
963
|
+
kind: "solana.message",
|
|
964
|
+
network: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
965
|
+
account,
|
|
966
|
+
message: request.data,
|
|
967
|
+
format: "raw"
|
|
968
|
+
};
|
|
969
|
+
const tronProbe = (request) => request.kind === "transaction" ? {
|
|
970
|
+
kind: "tron.transaction",
|
|
971
|
+
network: "tron:728126428",
|
|
972
|
+
account,
|
|
973
|
+
rawData: request.data
|
|
974
|
+
} : {
|
|
975
|
+
kind: "tron.message",
|
|
976
|
+
network: "tron:728126428",
|
|
977
|
+
account,
|
|
978
|
+
message: request.data
|
|
979
|
+
};
|
|
980
|
+
const COIN1_ENCODINGS = (() => {
|
|
981
|
+
const groups = /* @__PURE__ */ new Map();
|
|
982
|
+
for (const network of Object.values(BITCOIN_NETWORKS)) {
|
|
983
|
+
if (network.coinType !== 1) continue;
|
|
984
|
+
const list = groups.get(network.params.bech32) ?? [];
|
|
985
|
+
list.push(network.caip2);
|
|
986
|
+
groups.set(network.params.bech32, list);
|
|
987
|
+
}
|
|
988
|
+
return [...groups.values()];
|
|
989
|
+
})();
|
|
990
|
+
const HARDENED$1 = 2147483648;
|
|
991
|
+
/**
|
|
992
|
+
* The network labels a PSBT's derivations permit, one entry per address encoding: coin type 0' is
|
|
993
|
+
* mainnet alone; coin type 1' is ambiguous between the `tb1…` test networks and regtest's `bcrt1…`,
|
|
994
|
+
* so both encodings are returned. A derivation with no hardened coin type, a coin type this wallet
|
|
995
|
+
* never serves, derivations across several coin types, or no derivations at all leave the network
|
|
996
|
+
* unreadable — refuse rather than show outputs on a guessed one (the same `network-ambiguous`
|
|
997
|
+
* `bindQrRequest` raises).
|
|
998
|
+
*/
|
|
999
|
+
const psbtEncodings = (psbt) => {
|
|
1000
|
+
const tx = qrGuard("malformed", () => Transaction.fromPSBT(psbt));
|
|
1001
|
+
const coins = /* @__PURE__ */ new Set();
|
|
1002
|
+
const collect = (io, side, index) => {
|
|
1003
|
+
for (const entry of inputEntries(io)) {
|
|
1004
|
+
const [, coin] = entry.path;
|
|
1005
|
+
if (entry.path.length < 2 || coin === void 0 || coin < HARDENED$1) throw new QrError("network-ambiguous", `a ${side} ${index} derivation carries no coin type`);
|
|
1006
|
+
coins.add(coin - HARDENED$1);
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
for (let index = 0; index < tx.inputsLength; index += 1) collect(qrGuard("malformed", () => tx.getInput(index)), "input", index);
|
|
1010
|
+
for (let index = 0; index < tx.outputsLength; index += 1) collect(qrGuard("malformed", () => tx.getOutput(index)), "output", index);
|
|
1011
|
+
if (coins.size !== 1) throw new QrError("network-ambiguous", coins.size === 0 ? "the PSBT carries no derivation to read a network from" : "the derivations span several bitcoin coin types");
|
|
1012
|
+
const [coin] = coins;
|
|
1013
|
+
if (coin === 0) return [[BITCOIN_MAINNET]];
|
|
1014
|
+
if (coin === 1) return COIN1_ENCODINGS;
|
|
1015
|
+
throw new QrError("network-ambiguous", `coin type ${coin ?? "unknown"}' is not a bitcoin network we serve`);
|
|
1016
|
+
};
|
|
1017
|
+
const bitcoinProbe = (request, network) => ({
|
|
1018
|
+
kind: "bitcoin.psbt",
|
|
1019
|
+
network,
|
|
1020
|
+
account: {
|
|
1021
|
+
kind: "hd",
|
|
1022
|
+
walletId: "",
|
|
1023
|
+
groupIndex: 0
|
|
1024
|
+
},
|
|
1025
|
+
origin: "external",
|
|
1026
|
+
psbt: request.psbt,
|
|
1027
|
+
sign: []
|
|
1028
|
+
});
|
|
1029
|
+
/** A normalized `chain.sign` request for the decoders; account fields are never read. */
|
|
1030
|
+
const probe = (request) => {
|
|
1031
|
+
switch (request.family) {
|
|
1032
|
+
case "evm": {
|
|
1033
|
+
const network = evmNetwork(request);
|
|
1034
|
+
return request.kind === "transaction" ? {
|
|
1035
|
+
kind: "evm.transaction",
|
|
1036
|
+
network,
|
|
1037
|
+
account,
|
|
1038
|
+
tx: request.tx
|
|
1039
|
+
} : request.kind === "typed-data" ? {
|
|
1040
|
+
kind: "evm.typed-data",
|
|
1041
|
+
network,
|
|
1042
|
+
account,
|
|
1043
|
+
typedData: request.typedData
|
|
1044
|
+
} : {
|
|
1045
|
+
kind: "evm.personal-message",
|
|
1046
|
+
network,
|
|
1047
|
+
account,
|
|
1048
|
+
message: request.message
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
case "solana": return solanaProbe(request);
|
|
1052
|
+
case "tron": return tronProbe(request);
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
const evmProbe = (request) => {
|
|
1056
|
+
const network = evmNetwork(request);
|
|
1057
|
+
return request.kind === "transaction" ? {
|
|
1058
|
+
kind: "evm.transaction",
|
|
1059
|
+
network,
|
|
1060
|
+
account,
|
|
1061
|
+
tx: request.tx
|
|
1062
|
+
} : request.kind === "typed-data" ? {
|
|
1063
|
+
kind: "evm.typed-data",
|
|
1064
|
+
network,
|
|
1065
|
+
account,
|
|
1066
|
+
typedData: request.typedData
|
|
1067
|
+
} : {
|
|
1068
|
+
kind: "evm.personal-message",
|
|
1069
|
+
network,
|
|
1070
|
+
account,
|
|
1071
|
+
message: request.message
|
|
1072
|
+
};
|
|
1073
|
+
};
|
|
1074
|
+
const evmDigests = (request) => {
|
|
1075
|
+
if (request.kind === "transaction") return [{
|
|
1076
|
+
kind: "evm-transaction-hash",
|
|
1077
|
+
value: hex$1(planEvm(evmProbe(request), probeKey(request)).tasks[0].digest),
|
|
1078
|
+
shownBy: ["ledger-ethereum"]
|
|
1079
|
+
}];
|
|
1080
|
+
if (request.kind === "typed-data") {
|
|
1081
|
+
const { json } = parseTypedData(request.typedData);
|
|
1082
|
+
return [
|
|
1083
|
+
{
|
|
1084
|
+
kind: "eip712-domain-hash",
|
|
1085
|
+
value: TypedData.hashDomain({
|
|
1086
|
+
domain: json.domain,
|
|
1087
|
+
types: json.types
|
|
1088
|
+
}),
|
|
1089
|
+
shownBy: ["ledger-ethereum"]
|
|
1090
|
+
},
|
|
1091
|
+
{
|
|
1092
|
+
kind: "eip712-message-hash",
|
|
1093
|
+
value: TypedData.hashStruct({
|
|
1094
|
+
data: json.message,
|
|
1095
|
+
primaryType: json.primaryType,
|
|
1096
|
+
types: json.types
|
|
1097
|
+
}),
|
|
1098
|
+
shownBy: ["ledger-ethereum"]
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
kind: "eip712-digest",
|
|
1102
|
+
value: hex$1(planEvm(evmProbe(request), probeKey(request)).tasks[0].digest),
|
|
1103
|
+
shownBy: []
|
|
1104
|
+
}
|
|
1105
|
+
];
|
|
1106
|
+
}
|
|
1107
|
+
return [{
|
|
1108
|
+
kind: "eip191-digest",
|
|
1109
|
+
value: hex$1(planEvm(evmProbe(request), probeKey(request)).tasks[0].digest),
|
|
1110
|
+
shownBy: []
|
|
1111
|
+
}];
|
|
1112
|
+
};
|
|
1113
|
+
/** One decode per network the PSBT's derivations permit; a failed decode drops that entry. */
|
|
1114
|
+
const bitcoinDisplays = (request) => {
|
|
1115
|
+
const encodings = psbtEncodings(request.psbt);
|
|
1116
|
+
const out = [];
|
|
1117
|
+
for (const networks of encodings) {
|
|
1118
|
+
const [network] = networks;
|
|
1119
|
+
if (network === void 0) continue;
|
|
1120
|
+
try {
|
|
1121
|
+
out.push({
|
|
1122
|
+
networks,
|
|
1123
|
+
intent: decodeBitcoinPsbt(bitcoinProbe(request, network))
|
|
1124
|
+
});
|
|
1125
|
+
} catch {}
|
|
1126
|
+
}
|
|
1127
|
+
return out.length === 0 ? void 0 : out;
|
|
1128
|
+
};
|
|
1129
|
+
/** The display decodes, or `undefined` when the request lacks what a decoder needs. */
|
|
1130
|
+
const displays = (request) => {
|
|
1131
|
+
if (request.family === "bitcoin") return bitcoinDisplays(request);
|
|
1132
|
+
if (request.address === void 0) return;
|
|
1133
|
+
try {
|
|
1134
|
+
switch (request.family) {
|
|
1135
|
+
case "evm": {
|
|
1136
|
+
const intent = decodeEvm(probe(request), request.address);
|
|
1137
|
+
return [{
|
|
1138
|
+
networks: [intent.network],
|
|
1139
|
+
intent
|
|
1140
|
+
}];
|
|
1141
|
+
}
|
|
1142
|
+
case "solana": {
|
|
1143
|
+
if (request.kind !== "transaction") return;
|
|
1144
|
+
const probed = solanaProbe(request);
|
|
1145
|
+
if (probed.kind !== "solana.transaction") return;
|
|
1146
|
+
const intent = decodeSolana(probed, request.address);
|
|
1147
|
+
return [{
|
|
1148
|
+
networks: [intent.network],
|
|
1149
|
+
intent
|
|
1150
|
+
}];
|
|
1151
|
+
}
|
|
1152
|
+
case "tron": {
|
|
1153
|
+
const intent = decodeTron(probe(request), request.address);
|
|
1154
|
+
return [{
|
|
1155
|
+
networks: [intent.network],
|
|
1156
|
+
intent
|
|
1157
|
+
}];
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
} catch {
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
};
|
|
1164
|
+
const displayed = (request) => {
|
|
1165
|
+
const decoded = displays(request);
|
|
1166
|
+
return decoded === void 0 ? {} : { displays: decoded };
|
|
1167
|
+
};
|
|
1168
|
+
const inspectQrRequest = (request) => {
|
|
1169
|
+
try {
|
|
1170
|
+
switch (request.family) {
|
|
1171
|
+
case "evm": return {
|
|
1172
|
+
request,
|
|
1173
|
+
...displayed(request),
|
|
1174
|
+
digests: evmDigests(request)
|
|
1175
|
+
};
|
|
1176
|
+
case "solana": return {
|
|
1177
|
+
request,
|
|
1178
|
+
...displayed(request),
|
|
1179
|
+
digests: [{
|
|
1180
|
+
kind: "solana-message-hash",
|
|
1181
|
+
value: base58.encode(sha256(request.data)),
|
|
1182
|
+
shownBy: ["ledger-solana"]
|
|
1183
|
+
}]
|
|
1184
|
+
};
|
|
1185
|
+
case "tron": return {
|
|
1186
|
+
request,
|
|
1187
|
+
...displayed(request),
|
|
1188
|
+
digests: [request.kind === "transaction" ? {
|
|
1189
|
+
kind: "tron-txid",
|
|
1190
|
+
value: hex$1(sha256(request.data)),
|
|
1191
|
+
shownBy: []
|
|
1192
|
+
} : {
|
|
1193
|
+
kind: "tron-message-digest",
|
|
1194
|
+
value: hex$1(tronMessageDigest(request.data)),
|
|
1195
|
+
shownBy: []
|
|
1196
|
+
}]
|
|
1197
|
+
};
|
|
1198
|
+
case "bitcoin": return {
|
|
1199
|
+
request,
|
|
1200
|
+
...displayed(request),
|
|
1201
|
+
digests: []
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
if (error instanceof QrError) throw error;
|
|
1206
|
+
throw new QrError("malformed", error instanceof Error ? error.message : "inspect failed");
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
// src/qr/signer.ts
|
|
1210
|
+
const isBindingRefusal = (error) => error instanceof SolanaClusterBindingError || error instanceof TronBindingError;
|
|
1211
|
+
/** The networks a QR counterpart can address: mainnet first for the binding-fallback order. */
|
|
1212
|
+
const SOLANA_MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
1213
|
+
const TRON_MAINNET = "tron:728126428";
|
|
1214
|
+
const EVM_ACCOUNT_NETWORK = "eip155:1";
|
|
1215
|
+
const mainnetFirst = (ids, mainnet) => [...ids.filter((id) => id === mainnet), ...ids.filter((id) => id !== mainnet)];
|
|
1216
|
+
/** The account template and account-network the request family's leaf address comes from. */
|
|
1217
|
+
const LEAF_ACCOUNT = {
|
|
1218
|
+
evm: {
|
|
1219
|
+
template: "evm.bip44",
|
|
1220
|
+
network: EVM_ACCOUNT_NETWORK
|
|
1221
|
+
},
|
|
1222
|
+
tron: {
|
|
1223
|
+
template: "tron.bip44",
|
|
1224
|
+
network: TRON_MAINNET
|
|
1225
|
+
},
|
|
1226
|
+
solana: {
|
|
1227
|
+
template: "solana.bip44-change",
|
|
1228
|
+
network: SOLANA_MAINNET
|
|
1229
|
+
}
|
|
1230
|
+
};
|
|
1231
|
+
/** A chain account's address — a reply without one is never bound against an empty string. */
|
|
1232
|
+
const accountAddress = (chain, family) => {
|
|
1233
|
+
if (!("address" in chain)) throw new QrError("wallet-unsupported", `the account has no ${family} address — never bind a request against an empty one`);
|
|
1234
|
+
return chain.address;
|
|
1235
|
+
};
|
|
1236
|
+
/**
|
|
1237
|
+
* The account's authenticated facts for binding: a hardware fingerprint comes from the device's
|
|
1238
|
+
* attested wallet info, a vault's or card's from its Bitcoin account material; the address comes
|
|
1239
|
+
* from `chain.account` — never from the request. Only the request's own family resolves: a card or
|
|
1240
|
+
* hardware wallet missing another family's pinned material must not blind this request. A missing
|
|
1241
|
+
* node surfaces the kernel's typed `device-required` unwrapped so the host can fetch accounts and
|
|
1242
|
+
* retry; nothing here wraps it in a QrError.
|
|
1243
|
+
*/
|
|
1244
|
+
const accountFacts = async (deps, account, family) => {
|
|
1245
|
+
const info = deps.wallet.getSnapshot().wallets.find((w) => w.walletId === account.walletId);
|
|
1246
|
+
if (info === void 0) throw new QrError("wallet-unsupported", "a QR signer must be a vault, card or hardware wallet — imported keys cannot sign");
|
|
1247
|
+
const { apis } = deps.engine.signers;
|
|
1248
|
+
const { walletId, groupIndex } = account;
|
|
1249
|
+
const [chain, bitcoin] = await Promise.all([family === "bitcoin" ? Promise.resolve(void 0) : apis.account({
|
|
1250
|
+
walletId,
|
|
1251
|
+
groupIndex,
|
|
1252
|
+
...LEAF_ACCOUNT[family]
|
|
1253
|
+
}), info.custody === "hardware" ? Promise.resolve(void 0) : apis.account({
|
|
1254
|
+
walletId,
|
|
1255
|
+
groupIndex,
|
|
1256
|
+
template: "btc.bip84",
|
|
1257
|
+
network: BITCOIN_MAINNET
|
|
1258
|
+
})]);
|
|
1259
|
+
const fingerprint = info.custody === "hardware" ? Number.parseInt(info.fingerprint, 16) : bitcoin === void 0 || !("fingerprint" in bitcoin) || bitcoin.fingerprint === void 0 ? NaN : Number.parseInt(bitcoin.fingerprint, 16);
|
|
1260
|
+
if (!Number.isSafeInteger(fingerprint)) throw new QrError("wallet-unsupported", "the wallet's master fingerprint is unavailable");
|
|
1261
|
+
return {
|
|
1262
|
+
walletId,
|
|
1263
|
+
groupIndex,
|
|
1264
|
+
fingerprint,
|
|
1265
|
+
addresses: chain === void 0 || family === "bitcoin" ? {} : { [family]: accountAddress(chain, family) },
|
|
1266
|
+
networks: {
|
|
1267
|
+
evm: deps.engine.networks.enabled("evm"),
|
|
1268
|
+
solana: mainnetFirst(deps.engine.networks.enabled("solana"), SOLANA_MAINNET),
|
|
1269
|
+
tron: mainnetFirst(deps.engine.networks.enabled("tron"), TRON_MAINNET),
|
|
1270
|
+
bitcoin: deps.engine.networks.enabled("bitcoin")
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
};
|
|
1274
|
+
const signerOf = (request, facts) => {
|
|
1275
|
+
if (request.family === "bitcoin") return;
|
|
1276
|
+
const address = facts.addresses[request.family];
|
|
1277
|
+
if (address === void 0) throw new QrError("wallet-unsupported", `the account has no ${request.family} address`);
|
|
1278
|
+
return { address };
|
|
1279
|
+
};
|
|
1280
|
+
/** The 64-byte ed25519 signature this account contributed to a signed transaction wire. */
|
|
1281
|
+
const solanaSignature$1 = (wire, address) => {
|
|
1282
|
+
const [count] = wire;
|
|
1283
|
+
if (count === void 0 || wire.length < 1 + 64 * count) throw new QrError("result-mismatch", "the signed wire is truncated");
|
|
1284
|
+
const index = qrGuard("result-mismatch", () => parseCompiledMessage(wire.subarray(1 + 64 * count))).staticAccounts.slice(0, count).indexOf(address);
|
|
1285
|
+
if (index === -1) throw new QrError("result-mismatch", "the signed wire carries no signature of this account");
|
|
1286
|
+
const signature = wire.subarray(1 + 64 * index, 1 + 64 * (index + 1));
|
|
1287
|
+
if (signature.length !== 64 || signature.every((byte) => byte === 0)) throw new QrError("result-mismatch", "this account's signature slot is empty");
|
|
1288
|
+
return signature;
|
|
1289
|
+
};
|
|
1290
|
+
/** A pipeline outcome as the `chain.sign` result `qrResponse` verifies against the request. */
|
|
1291
|
+
const toChainResult = (result, signer) => {
|
|
1292
|
+
if (!("kind" in result)) throw new QrError("result-mismatch", "a QR request never produces a tracked transaction");
|
|
1293
|
+
switch (result.kind) {
|
|
1294
|
+
case "evm.signed-transaction": return {
|
|
1295
|
+
kind: "evm.transaction",
|
|
1296
|
+
signedTransaction: result.signedTransaction
|
|
1297
|
+
};
|
|
1298
|
+
case "evm.personal-message":
|
|
1299
|
+
case "evm.typed-data": return {
|
|
1300
|
+
kind: result.kind,
|
|
1301
|
+
signature: result.signature
|
|
1302
|
+
};
|
|
1303
|
+
case "tron.signed-transaction": return {
|
|
1304
|
+
kind: "tron.transaction",
|
|
1305
|
+
signature: result.signature
|
|
1306
|
+
};
|
|
1307
|
+
case "tron.message": return {
|
|
1308
|
+
kind: "tron.message",
|
|
1309
|
+
signature: result.signature
|
|
1310
|
+
};
|
|
1311
|
+
case "solana.message": return {
|
|
1312
|
+
kind: "solana.message",
|
|
1313
|
+
signature: result.signature
|
|
1314
|
+
};
|
|
1315
|
+
case "solana.transactions": {
|
|
1316
|
+
const wires = result.signedTransactions;
|
|
1317
|
+
if (result.send || result.stopped !== void 0 || wires === void 0 || wires.length !== 1 || signer === void 0) throw new QrError("result-mismatch", "a QR request signs exactly one transaction");
|
|
1318
|
+
const [wire] = wires;
|
|
1319
|
+
if (wire === void 0) throw new QrError("result-mismatch", "a QR request signs exactly one transaction");
|
|
1320
|
+
return {
|
|
1321
|
+
kind: "solana.transaction",
|
|
1322
|
+
signature: solanaSignature$1(wire, signer.address)
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
case "bitcoin.psbts": {
|
|
1326
|
+
const [signed] = result.signedPsbts;
|
|
1327
|
+
if (result.stopped !== void 0 || result.signedPsbts.length !== 1 || signed === void 0) throw new QrError("result-mismatch", "a QR request signs exactly one PSBT");
|
|
1328
|
+
return {
|
|
1329
|
+
kind: "bitcoin.psbt",
|
|
1330
|
+
signedPsbt: signed
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
default: throw new QrError("result-mismatch", "the request produced an unexpected result");
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
/**
|
|
1337
|
+
* The QR signing surface: `prepare` feeds the pipeline, `respond` encodes the result back into the
|
|
1338
|
+
* counterpart's UR, `exportAccount` encodes public account material for a watch-only wallet.
|
|
1339
|
+
*/
|
|
1340
|
+
const qrSigner = (deps) => {
|
|
1341
|
+
const bound = /* @__PURE__ */ new Map();
|
|
1342
|
+
return {
|
|
1343
|
+
prepare: async (request, account) => {
|
|
1344
|
+
const facts = await accountFacts(deps, account, request.family);
|
|
1345
|
+
const candidates = bindQrRequest(request, facts);
|
|
1346
|
+
for (const candidate of candidates) try {
|
|
1347
|
+
const pending = await deps.engine.requests.prepare(candidate);
|
|
1348
|
+
bound.set(pending.id, {
|
|
1349
|
+
request,
|
|
1350
|
+
signer: signerOf(request, facts)
|
|
1351
|
+
});
|
|
1352
|
+
const { id } = pending;
|
|
1353
|
+
const releaseOnEnd = async () => {
|
|
1354
|
+
try {
|
|
1355
|
+
await deps.engine.requests.outcome(id);
|
|
1356
|
+
} catch {
|
|
1357
|
+
bound.delete(id);
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
releaseOnEnd();
|
|
1361
|
+
return pending;
|
|
1362
|
+
} catch (error) {
|
|
1363
|
+
if (isBindingRefusal(error)) continue;
|
|
1364
|
+
throw error;
|
|
1365
|
+
}
|
|
1366
|
+
throw new QrError("unbound", "the request could not be bound to any enabled network");
|
|
1367
|
+
},
|
|
1368
|
+
respond: async (id) => {
|
|
1369
|
+
const result = await deps.engine.requests.outcome(id);
|
|
1370
|
+
const entry = bound.get(id);
|
|
1371
|
+
if (entry === void 0) throw new QrError("result-mismatch", "the request was not prepared by this signer");
|
|
1372
|
+
bound.delete(id);
|
|
1373
|
+
return qrResponse(entry.request, toChainResult(result, entry.signer), entry.signer);
|
|
1374
|
+
},
|
|
1375
|
+
exportAccount: async (params) => {
|
|
1376
|
+
const { apis } = deps.engine.signers;
|
|
1377
|
+
const nodes = exportNodes(params);
|
|
1378
|
+
const result = await apis.exportAccount({
|
|
1379
|
+
walletId: params.account.walletId,
|
|
1380
|
+
groupIndex: params.account.groupIndex,
|
|
1381
|
+
nodes,
|
|
1382
|
+
verify: params.verify,
|
|
1383
|
+
...params.acknowledgeUnconfirmed === void 0 ? {} : { acknowledgeUnconfirmed: params.acknowledgeUnconfirmed }
|
|
1384
|
+
});
|
|
1385
|
+
return qrExport(result, params.form);
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1388
|
+
};
|
|
1389
|
+
const LEAF_TEMPLATES = {
|
|
1390
|
+
evm: "evm.bip44",
|
|
1391
|
+
tron: "tron.bip44",
|
|
1392
|
+
solana: "solana.bip44-change"
|
|
1393
|
+
};
|
|
1394
|
+
const exportNodes = (params) => params.form === "crypto-hdkey" ? [{ kind: "evm.account" }] : params.form === "crypto-account" ? [{
|
|
1395
|
+
kind: "btc.account",
|
|
1396
|
+
template: params.bitcoin.template,
|
|
1397
|
+
network: params.bitcoin.network
|
|
1398
|
+
}] : [...params.families.map((family) => ({
|
|
1399
|
+
kind: "leaf",
|
|
1400
|
+
template: LEAF_TEMPLATES[family]
|
|
1401
|
+
})), ...params.bitcoin === void 0 ? [] : [{
|
|
1402
|
+
kind: "btc.account",
|
|
1403
|
+
template: params.bitcoin.template,
|
|
1404
|
+
network: params.bitcoin.network
|
|
1405
|
+
}]];
|
|
1406
|
+
// src/qr/counterpart.ts
|
|
1407
|
+
/**
|
|
1408
|
+
* The watch-only side of the QR channel: a Web wallet holds no keys — `readQrExport` turns the
|
|
1409
|
+
* phone's `crypto-multi-accounts` export into per-family `QrLink`s, `qrSignRequest` writes the UR
|
|
1410
|
+
* request our own `readQrRequest` accepts and binds, and `readQrSignature` verifies the phone's UR
|
|
1411
|
+
* answer against what was sent. Every hop is frames; nothing here touches a wallet.
|
|
1412
|
+
*/
|
|
1413
|
+
const HARDENED = 2147483648;
|
|
1414
|
+
const pathString = (components) => `m/${components.map((c) => c.kind === "index" ? `${c.index}${c.hardened ? "'" : ""}` : "*").join("/")}`;
|
|
1415
|
+
const fingerprintHex = (n) => n.toString(16).padStart(8, "0");
|
|
1416
|
+
/** An exported key as the link its family shape makes it, or `unsupported`. */
|
|
1417
|
+
const keyLink = (key, mfp) => {
|
|
1418
|
+
const { origin } = key.key;
|
|
1419
|
+
if (origin === void 0 || origin.components.length === 0) malformed("an exported key carries no derivation path");
|
|
1420
|
+
if (origin.sourceFingerprint !== mfp) malformed("an exported key's fingerprint is not the export's");
|
|
1421
|
+
const path = pathString(origin.components);
|
|
1422
|
+
const fingerprint = fingerprintHex(mfp);
|
|
1423
|
+
const secp = key.curve === "secp256k1" ? key.key.keyData : void 0;
|
|
1424
|
+
const ed = key.curve === "ed25519" ? key.key.keyData : void 0;
|
|
1425
|
+
const secp33 = (what) => {
|
|
1426
|
+
if (secp === void 0) malformed(`${what} must be a secp256k1 key`);
|
|
1427
|
+
return secp;
|
|
1428
|
+
};
|
|
1429
|
+
const leaf = (family, address) => ({
|
|
1430
|
+
family,
|
|
1431
|
+
fingerprint,
|
|
1432
|
+
path,
|
|
1433
|
+
address: qrGuard("malformed", () => address)
|
|
1434
|
+
});
|
|
1435
|
+
if (qrLinkPath("evm", path)) return leaf("evm", evmAddressFromPublicKey(secp33("an evm leaf")));
|
|
1436
|
+
if (qrLinkPath("tron", path)) return leaf("tron", tronAddressFromPublicKey(secp33("a tron leaf")));
|
|
1437
|
+
if (qrLinkPath("solana", path)) {
|
|
1438
|
+
if (ed === void 0) malformed("a solana leaf must be an ed25519 key");
|
|
1439
|
+
return leaf("solana", solanaAddressFromPublicKey(ed));
|
|
1440
|
+
}
|
|
1441
|
+
if (!qrLinkPath("bitcoin", path)) return unsupported(`path ${path} is not a signing key the phone exports`);
|
|
1442
|
+
const steps = pathComponents(path) ?? malformed("unreadable derivation path");
|
|
1443
|
+
const purpose = steps[0]?.index ?? 0;
|
|
1444
|
+
const coin = steps[1]?.index ?? -1;
|
|
1445
|
+
const template = PURPOSE_TEMPLATE[purpose];
|
|
1446
|
+
if (template === void 0) return unsupported(`path ${path} is not a signing key the phone exports`);
|
|
1447
|
+
const pubkey = secp33("a bitcoin account");
|
|
1448
|
+
const { chainCode } = key.key;
|
|
1449
|
+
const { parentFingerprint } = key.key;
|
|
1450
|
+
if (chainCode === void 0) malformed("a bitcoin account carries no chain code");
|
|
1451
|
+
if (parentFingerprint === void 0) malformed("a bitcoin account carries no parent fingerprint");
|
|
1452
|
+
const network = coin === 0 ? BITCOIN_MAINNET : BITCOIN_TESTNET;
|
|
1453
|
+
const version = bitcoinNetwork(network).bip32.public;
|
|
1454
|
+
const xpub = qrGuard("malformed", () => new HDKey({
|
|
1455
|
+
versions: {
|
|
1456
|
+
private: version,
|
|
1457
|
+
public: version
|
|
1458
|
+
},
|
|
1459
|
+
depth: steps.length,
|
|
1460
|
+
parentFingerprint,
|
|
1461
|
+
index: (steps[2]?.index ?? 0) + HARDENED,
|
|
1462
|
+
chainCode,
|
|
1463
|
+
publicKey: pubkey
|
|
1464
|
+
}).publicExtendedKey);
|
|
1465
|
+
return {
|
|
1466
|
+
family: "bitcoin",
|
|
1467
|
+
fingerprint,
|
|
1468
|
+
path,
|
|
1469
|
+
address: qrGuard("malformed", () => bitcoinAddressFromXpub(xpub, template, network, 0, 0)),
|
|
1470
|
+
xpub
|
|
1471
|
+
};
|
|
1472
|
+
};
|
|
1473
|
+
/**
|
|
1474
|
+
* The phone's `crypto-multi-accounts` export as one link per family. Strict: the fingerprint is
|
|
1475
|
+
* required, every key's path must be one of the shapes a `QrLink` declares, all keys share the
|
|
1476
|
+
* master fingerprint, and at most one key per family is accepted.
|
|
1477
|
+
*/
|
|
1478
|
+
const readQrExport = (payload) => {
|
|
1479
|
+
if (payload.type !== "crypto-multi-accounts") return unsupported(`UR type ${payload.type} is not an export the Web wallet reads`);
|
|
1480
|
+
const accounts = qrGuard("malformed", () => fromUr(Ur.fromCborData(payload.type, payload.cbor), cryptoMultiAccountsCodec));
|
|
1481
|
+
const mfp = accounts.masterFingerprint;
|
|
1482
|
+
const links = [];
|
|
1483
|
+
for (const key of accounts.keys) {
|
|
1484
|
+
const link = keyLink(key, mfp);
|
|
1485
|
+
if (links.some((l) => l.family === link.family)) malformed(`the export carries two ${link.family} keys`);
|
|
1486
|
+
links.push(link);
|
|
1487
|
+
}
|
|
1488
|
+
return links;
|
|
1489
|
+
};
|
|
1490
|
+
const requestKeypath = (link) => keypath(link.path, Number.parseInt(link.fingerprint, 16));
|
|
1491
|
+
const encode = (value, codec) => qrGuard("malformed", () => {
|
|
1492
|
+
const ur = toUr(value, codec);
|
|
1493
|
+
return {
|
|
1494
|
+
type: ur.type,
|
|
1495
|
+
cbor: ur.toCborData()
|
|
1496
|
+
};
|
|
1497
|
+
});
|
|
1498
|
+
const evmAddressBytes = (address) => {
|
|
1499
|
+
const bytes = hexToBytes(address.startsWith("0x") ? address.slice(2) : address);
|
|
1500
|
+
if (bytes.length !== 20) malformed(`evm link address ${address} is not 20 bytes`);
|
|
1501
|
+
return bytes;
|
|
1502
|
+
};
|
|
1503
|
+
/** A PSBT derivation `link` owns: its fingerprint, under its account path. */
|
|
1504
|
+
const underLink = (entry, linkPath, fp) => entry.fingerprint === fp && entry.path.length >= linkPath.length && linkPath.every((step, i) => entry.path[i] === step);
|
|
1505
|
+
/** `link`'s numeric account path and fingerprint — the PSBT derivation match keys. */
|
|
1506
|
+
const linkDerivation = (link) => ({
|
|
1507
|
+
path: (pathComponents(link.path) ?? malformed(`bad link path ${link.path}`)).map((s) => s.index + (s.hardened ? HARDENED : 0)),
|
|
1508
|
+
fp: Number.parseInt(link.fingerprint, 16)
|
|
1509
|
+
});
|
|
1510
|
+
const psbtHasLinkInput = (psbt, link) => {
|
|
1511
|
+
const tx = qrGuard("malformed", () => Transaction$1.fromPSBT(psbt));
|
|
1512
|
+
const { path, fp } = linkDerivation(link);
|
|
1513
|
+
for (let index = 0; index < tx.inputsLength; index += 1) if (inputEntries(tx.getInput(index)).some((e) => underLink(e, path, fp))) return true;
|
|
1514
|
+
return false;
|
|
1515
|
+
};
|
|
1516
|
+
const evmRequest = (link, input, requestId, origin) => {
|
|
1517
|
+
const head = {
|
|
1518
|
+
requestId,
|
|
1519
|
+
derivationPath: requestKeypath(link),
|
|
1520
|
+
address: evmAddressBytes(link.address),
|
|
1521
|
+
...origin === void 0 ? {} : { origin }
|
|
1522
|
+
};
|
|
1523
|
+
if (input.kind === "transaction") {
|
|
1524
|
+
const { tx } = input;
|
|
1525
|
+
if (tx.type === "eip7702") throw new QrError("unsupported", "an EIP-7702 authorization cannot be a QR request");
|
|
1526
|
+
if (tx.to === void 0) throw new QrError("contract-creation", "a QR request cannot create a contract");
|
|
1527
|
+
const request = {
|
|
1528
|
+
...head,
|
|
1529
|
+
signData: qrGuard("malformed", () => evmSigningPreimage(tx)),
|
|
1530
|
+
dataType: tx.type === "legacy" ? "transaction" : "typedTransaction",
|
|
1531
|
+
chainId: tx.chainId
|
|
1532
|
+
};
|
|
1533
|
+
return encode(request, ethSignRequestCodec);
|
|
1534
|
+
}
|
|
1535
|
+
const request = {
|
|
1536
|
+
...head,
|
|
1537
|
+
signData: input.kind === "typed-data" ? new TextEncoder().encode(input.typedData) : input.message,
|
|
1538
|
+
dataType: input.kind === "typed-data" ? "typedData" : "personalMessage",
|
|
1539
|
+
...input.chainId === void 0 ? {} : { chainId: input.chainId }
|
|
1540
|
+
};
|
|
1541
|
+
return encode(request, ethSignRequestCodec);
|
|
1542
|
+
};
|
|
1543
|
+
/**
|
|
1544
|
+
* The watch-only wallet's sign request, encoded as the exact UR the phone accepts: Keystone layouts
|
|
1545
|
+
* (`eth`/`sol`/`tron` sign request, `crypto-psbt` for Bitcoin), the link's fingerprint and path, a
|
|
1546
|
+
* random 16-byte request id and — only when given — an origin. Inputs the phone would refuse anyway
|
|
1547
|
+
* are refused here with the same codes `bindQrRequest` raises.
|
|
1548
|
+
*/
|
|
1549
|
+
const qrSignRequest = (link, input, options) => {
|
|
1550
|
+
if (input.family !== link.family) return malformed(`a ${input.family} input cannot sign through a ${link.family} link`);
|
|
1551
|
+
if (!QR_FINGERPRINT.test(link.fingerprint)) return malformed(`bad fingerprint ${link.fingerprint}`);
|
|
1552
|
+
if (!qrLinkPath(link.family, link.path)) return malformed(`path ${link.path} is not a ${link.family} signing path`);
|
|
1553
|
+
const requestId = options.random(16);
|
|
1554
|
+
if (requestId.length !== 16) return malformed("the random source must give 16 bytes");
|
|
1555
|
+
const head = {
|
|
1556
|
+
requestId,
|
|
1557
|
+
derivationPath: requestKeypath(link),
|
|
1558
|
+
...options.origin === void 0 ? {} : { origin: options.origin }
|
|
1559
|
+
};
|
|
1560
|
+
let payload;
|
|
1561
|
+
if (input.family === "evm") payload = evmRequest(link, input, requestId, options.origin);
|
|
1562
|
+
else if (input.family === "solana") {
|
|
1563
|
+
if (input.kind === "transaction") {
|
|
1564
|
+
const parsed = qrGuard("malformed", () => parseCompiledMessage(input.data));
|
|
1565
|
+
if (!parsed.staticAccounts.slice(0, parsed.numRequiredSignatures).includes(link.address)) throw new QrError("account-mismatch", "the request's signer is not this account's");
|
|
1566
|
+
}
|
|
1567
|
+
const request = {
|
|
1568
|
+
...head,
|
|
1569
|
+
signData: input.data,
|
|
1570
|
+
address: base58.decode(link.address),
|
|
1571
|
+
signType: input.kind
|
|
1572
|
+
};
|
|
1573
|
+
payload = encode(request, solSignRequestCodec);
|
|
1574
|
+
} else if (input.family === "tron") {
|
|
1575
|
+
const request = {
|
|
1576
|
+
...head,
|
|
1577
|
+
signData: input.data,
|
|
1578
|
+
dataType: input.kind === "transaction" ? "transaction" : "messageV2",
|
|
1579
|
+
address: tronAddressToBytes(link.address)
|
|
1580
|
+
};
|
|
1581
|
+
payload = encode(request, tronSignRequestCodec);
|
|
1582
|
+
} else {
|
|
1583
|
+
if (!psbtHasLinkInput(input.psbt, link)) throw new QrError("nothing-to-sign", "no input carries this wallet's fingerprint");
|
|
1584
|
+
payload = encode({ bytes: input.psbt }, cryptoPsbtCodec);
|
|
1585
|
+
}
|
|
1586
|
+
return {
|
|
1587
|
+
link,
|
|
1588
|
+
input,
|
|
1589
|
+
requestId,
|
|
1590
|
+
payload
|
|
1591
|
+
};
|
|
1592
|
+
};
|
|
1593
|
+
const decode = (payload, codec) => qrGuard("result-mismatch", () => fromUr(Ur.fromCborData(payload.type, payload.cbor), codec));
|
|
1594
|
+
const echoRequestId = (sent, got) => {
|
|
1595
|
+
const want = sent.requestId;
|
|
1596
|
+
if (want === void 0 ? got === void 0 : got !== void 0 && bytesToHex$1(want) === bytesToHex$1(got)) return;
|
|
1597
|
+
resultMismatch("the response's request id is not the request's");
|
|
1598
|
+
};
|
|
1599
|
+
const evmTxRequest = (tx) => ({
|
|
1600
|
+
kind: "evm.transaction",
|
|
1601
|
+
network: `eip155:${tx.chainId}`,
|
|
1602
|
+
account: {
|
|
1603
|
+
kind: "hd",
|
|
1604
|
+
walletId: "",
|
|
1605
|
+
groupIndex: 0,
|
|
1606
|
+
template: ""
|
|
1607
|
+
},
|
|
1608
|
+
tx
|
|
1609
|
+
});
|
|
1610
|
+
const evmSignedTransaction = (tx, signature) => {
|
|
1611
|
+
const v = signature.subarray(64).reduce((acc, byte) => acc * 256 + byte, 0);
|
|
1612
|
+
const yParity = tx.type === "legacy" ? v - 27 - tx.chainId * 2 - 8 : v;
|
|
1613
|
+
if (yParity !== 0 && yParity !== 1) resultMismatch("unexpected recovery id");
|
|
1614
|
+
const plan = (publicKey) => planEvm(evmTxRequest(tx), {
|
|
1615
|
+
path: [],
|
|
1616
|
+
publicKey
|
|
1617
|
+
});
|
|
1618
|
+
const wire = qrGuard("result-mismatch", () => Signature.from({
|
|
1619
|
+
r: Hex.fromBytes(signature.subarray(0, 32)),
|
|
1620
|
+
s: Hex.fromBytes(signature.subarray(32, 64)),
|
|
1621
|
+
yParity
|
|
1622
|
+
}));
|
|
1623
|
+
const publicKey = qrGuard("result-mismatch", () => PublicKey.toBytes(PublicKey.compress(Secp256k1.recoverPublicKey({
|
|
1624
|
+
payload: Hex.fromBytes(plan(/* @__PURE__ */ new Uint8Array(33)).tasks[0].digest),
|
|
1625
|
+
signature: wire
|
|
1626
|
+
}))));
|
|
1627
|
+
const assembled = qrGuard("result-mismatch", () => assembleEvm(plan(publicKey), [signature.subarray(0, 64)]));
|
|
1628
|
+
if (assembled.kind !== "signed") resultMismatch("the transaction plan did not sign");
|
|
1629
|
+
return assembled.artefact;
|
|
1630
|
+
};
|
|
1631
|
+
const evmSignature = (sent, payload) => {
|
|
1632
|
+
const { requestId, signature } = decode(payload, ethSignatureCodec);
|
|
1633
|
+
echoRequestId(sent, requestId);
|
|
1634
|
+
const { input } = sent;
|
|
1635
|
+
if (input.family !== "evm") resultMismatch("not an evm request");
|
|
1636
|
+
const signedTransaction = input.kind === "transaction" ? evmSignedTransaction(input.tx, signature) : void 0;
|
|
1637
|
+
if (qrGuard("result-mismatch", () => recoverEvmSigner(evmProbe$1(input), signedTransaction === void 0 ? { signature } : { signedTransaction })).toLowerCase() !== sent.link.address.toLowerCase()) resultMismatch("signature does not recover to the signer");
|
|
1638
|
+
return signedTransaction === void 0 ? {
|
|
1639
|
+
kind: "evm.signature",
|
|
1640
|
+
signature
|
|
1641
|
+
} : {
|
|
1642
|
+
kind: "evm.transaction",
|
|
1643
|
+
signedTransaction
|
|
1644
|
+
};
|
|
1645
|
+
};
|
|
1646
|
+
const solanaSignature = (sent, payload) => {
|
|
1647
|
+
const { requestId, signature } = decode(payload, solSignatureCodec);
|
|
1648
|
+
echoRequestId(sent, requestId);
|
|
1649
|
+
const { input } = sent;
|
|
1650
|
+
if (input.family !== "solana") resultMismatch("not a solana request");
|
|
1651
|
+
if (signature.length !== 64 || !verifySolanaSignature(input.data, signature, sent.link.address)) resultMismatch("signature does not verify under the signer");
|
|
1652
|
+
return {
|
|
1653
|
+
kind: "solana.signature",
|
|
1654
|
+
signature
|
|
1655
|
+
};
|
|
1656
|
+
};
|
|
1657
|
+
const tronSignature = (sent, payload) => {
|
|
1658
|
+
const { requestId, signature } = decode(payload, tronSignatureCodec);
|
|
1659
|
+
echoRequestId(sent, requestId);
|
|
1660
|
+
const { input } = sent;
|
|
1661
|
+
if (input.family !== "tron") resultMismatch("not a tron request");
|
|
1662
|
+
const last = signature.at(-1);
|
|
1663
|
+
const normalized = input.kind === "transaction" && (last === 0 || last === 1) ? new Uint8Array([...signature.subarray(0, 64), last + 27]) : signature;
|
|
1664
|
+
if (qrGuard("result-mismatch", () => input.kind === "transaction" ? recoverTronTransactionSigner(input.data, normalized) : recoverTronMessageSigner(input.data, normalized)) !== sent.link.address) resultMismatch("signature does not recover to the signer");
|
|
1665
|
+
return {
|
|
1666
|
+
kind: "tron.signature",
|
|
1667
|
+
signature
|
|
1668
|
+
};
|
|
1669
|
+
};
|
|
1670
|
+
/** New signatures by a key whose derivation sits under `link`'s account path. */
|
|
1671
|
+
const linkSignatures = (requested, signed, linkPath, fp) => {
|
|
1672
|
+
const linkKeys = /* @__PURE__ */ new Set();
|
|
1673
|
+
for (const [pubkey, der] of signed.bip32Derivation ?? []) if (underLink(der, linkPath, fp)) linkKeys.add(bytesToHex$1(pubkey));
|
|
1674
|
+
let linkTap = false;
|
|
1675
|
+
for (const [pubkey, entry] of signed.tapBip32Derivation ?? []) if (underLink(entry.der, linkPath, fp)) {
|
|
1676
|
+
linkKeys.add(bytesToHex$1(pubkey));
|
|
1677
|
+
linkTap = true;
|
|
1678
|
+
}
|
|
1679
|
+
const requestedKeys = new Set((requested.partialSig ?? []).map(([pubkey]) => bytesToHex$1(pubkey)));
|
|
1680
|
+
let fresh = 0;
|
|
1681
|
+
for (const [pubkey] of signed.partialSig ?? []) if (linkKeys.has(bytesToHex$1(pubkey)) && !requestedKeys.has(bytesToHex$1(pubkey))) fresh += 1;
|
|
1682
|
+
if (signed.tapKeySig !== void 0 && requested.tapKeySig === void 0 && linkTap) fresh += 1;
|
|
1683
|
+
return fresh;
|
|
1684
|
+
};
|
|
1685
|
+
const bitcoinSignature = (sent, payload) => {
|
|
1686
|
+
const { input } = sent;
|
|
1687
|
+
if (input.family !== "bitcoin") resultMismatch("not a bitcoin request");
|
|
1688
|
+
const { bytes } = decode(payload, cryptoPsbtCodec);
|
|
1689
|
+
if (!samePsbtContent(input.psbt, bytes)) resultMismatch("the signed PSBT does not match the request");
|
|
1690
|
+
const { path: linkPath, fp } = linkDerivation(sent.link);
|
|
1691
|
+
const [requested, signed] = qrGuard("result-mismatch", () => [Transaction$1.fromPSBT(input.psbt), Transaction$1.fromPSBT(bytes)]);
|
|
1692
|
+
let fresh = 0;
|
|
1693
|
+
for (let index = 0; index < signed.inputsLength; index += 1) fresh += linkSignatures(requested.getInput(index), signed.getInput(index), linkPath, fp);
|
|
1694
|
+
if (fresh < 1) resultMismatch("the signed PSBT carries no new signature by this account");
|
|
1695
|
+
return {
|
|
1696
|
+
kind: "bitcoin.psbt",
|
|
1697
|
+
psbt: bytes
|
|
1698
|
+
};
|
|
1699
|
+
};
|
|
1700
|
+
/**
|
|
1701
|
+
* The phone's UR answer verified against what this session sent: the payload type must be the
|
|
1702
|
+
* response type for the request, the request id must echo, and the signature must recover or verify
|
|
1703
|
+
* to `link.address` (a PSBT must carry unchanged content plus at least one new signature by a key
|
|
1704
|
+
* under the link). Anything else is `result-mismatch`.
|
|
1705
|
+
*/
|
|
1706
|
+
const RESPONSE_TYPES = {
|
|
1707
|
+
evm: "eth-signature",
|
|
1708
|
+
solana: "sol-signature",
|
|
1709
|
+
tron: "tron-signature",
|
|
1710
|
+
bitcoin: "crypto-psbt"
|
|
1711
|
+
};
|
|
1712
|
+
const VERIFY = {
|
|
1713
|
+
evm: evmSignature,
|
|
1714
|
+
solana: solanaSignature,
|
|
1715
|
+
tron: tronSignature,
|
|
1716
|
+
bitcoin: bitcoinSignature
|
|
1717
|
+
};
|
|
1718
|
+
const readQrSignature = (sent, payload) => {
|
|
1719
|
+
const expected = RESPONSE_TYPES[sent.input.family];
|
|
1720
|
+
if (payload.type !== expected) resultMismatch(`the response type ${payload.type} is not the request's ${expected}`);
|
|
1721
|
+
return VERIFY[sent.input.family](sent, payload);
|
|
1722
|
+
};
|
|
1723
|
+
export { BITCOIN_MAINNET, QR_SCAN_LIMITS, QrError, bindQrRequest, createQrScanner, inspectQrRequest, qrExport, qrFrames, qrGuard, qrResponse, qrSignRequest, qrSigner, readQrExport, readQrRequest, readQrSignature };
|
|
1724
|
+
|
|
1725
|
+
//# sourceMappingURL=qr.mjs.map
|