@qrkit/core 0.1.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/LICENSE +190 -0
- package/README.md +61 -0
- package/dist/index.cjs +309 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached with the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submit" means any form of electronic, verbal, or
|
|
51
|
+
written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source lists, defect tracking systems, and issue tracking systems that
|
|
54
|
+
are managed by, or on behalf of, the Licensor for the purpose of
|
|
55
|
+
discussing and improving the Work, but excluding communication that is
|
|
56
|
+
conspicuously marked or designated in writing by the copyright owner as
|
|
57
|
+
"Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any patent
|
|
81
|
+
claim embodied in the Work constitutes direct or patent infringement,
|
|
82
|
+
then any patent rights granted to You under this License for that Work
|
|
83
|
+
shall terminate as of the date such litigation is filed.
|
|
84
|
+
|
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
86
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
87
|
+
modifications, and in Source or Object form, provided that You
|
|
88
|
+
meet the following conditions:
|
|
89
|
+
|
|
90
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
91
|
+
Works a copy of this License; and
|
|
92
|
+
|
|
93
|
+
(b) You must cause any modified files to carry prominent notices
|
|
94
|
+
stating that You changed the files; and
|
|
95
|
+
|
|
96
|
+
(c) You must retain, in all Source or Derivative Works that You
|
|
97
|
+
distribute, all copyright, patent, trademark, and attribution
|
|
98
|
+
notices from the Source form of the Work, excluding those notices
|
|
99
|
+
that do not pertain to any part of the Derivative Works; and
|
|
100
|
+
|
|
101
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
102
|
+
distribution, then any Derivative Works that You distribute must
|
|
103
|
+
include a readable copy of the attribution notices contained
|
|
104
|
+
within such NOTICE file, except for those notices that do not
|
|
105
|
+
pertain to any part of the Derivative Works, in any part of
|
|
106
|
+
the Derivative Works in which You reproduce, separately or as
|
|
107
|
+
an addendum to the NOTICE text from the Work, provided that
|
|
108
|
+
such additional attribution notices cannot be construed as
|
|
109
|
+
modifying the License.
|
|
110
|
+
|
|
111
|
+
You may add Your own attribution notices within Derivative Works
|
|
112
|
+
that You distribute, alongside or in addition to the NOTICE text
|
|
113
|
+
from the Work, provided that such additional attribution notices
|
|
114
|
+
cannot be construed as modifying the License.
|
|
115
|
+
|
|
116
|
+
You may add Your own copyright statement to Your modifications and
|
|
117
|
+
may provide additional grant of rights to use, reproduce, prepare
|
|
118
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
119
|
+
and distribute those Modifications, as in this License.
|
|
120
|
+
|
|
121
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
122
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
123
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
124
|
+
this License, without any additional terms or conditions.
|
|
125
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
126
|
+
the terms of any separate license agreement you may have executed
|
|
127
|
+
with Licensor regarding such Contributions.
|
|
128
|
+
|
|
129
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
130
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
131
|
+
except as required for reasonable and customary use in describing the
|
|
132
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
133
|
+
|
|
134
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
135
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
136
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
137
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
138
|
+
implied, including, without limitation, any conditions of title,
|
|
139
|
+
MERCHANTIBILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely
|
|
140
|
+
responsible for determining the appropriateness of using or
|
|
141
|
+
reproducing the Work and assume any risks associated with Your
|
|
142
|
+
exercise of permissions under this License.
|
|
143
|
+
|
|
144
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
145
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
146
|
+
unless required by applicable law (such as deliberate and grossly
|
|
147
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
148
|
+
liable to You for damages, including any direct, indirect, special,
|
|
149
|
+
incidental, or exemplary damages of any character arising as a
|
|
150
|
+
result of this License or out of the use or inability to use the
|
|
151
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
152
|
+
work stoppage, computer failure or malfunction, or all other
|
|
153
|
+
commercial damages or losses), even if such Contributor has been
|
|
154
|
+
advised of the possibility of such damages.
|
|
155
|
+
|
|
156
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
157
|
+
the Work or Derivative Works thereof, You may wish to offer, and
|
|
158
|
+
charge a fee for, acceptance of support, warranty, indemnity, or
|
|
159
|
+
other liability obligations and/or rights consistent of this License.
|
|
160
|
+
However, in accepting such obligations, You may offer such conditions
|
|
161
|
+
only on Your own behalf and on Your sole responsibility, not on behalf
|
|
162
|
+
of any other Contributor, and only if You agree to indemnify, defend,
|
|
163
|
+
and hold each Contributor harmless for any liability incurred by, or
|
|
164
|
+
claims asserted against, such Contributor by reason of your accepting
|
|
165
|
+
any such warranty or additional liability.
|
|
166
|
+
|
|
167
|
+
END OF TERMS AND CONDITIONS
|
|
168
|
+
|
|
169
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
170
|
+
|
|
171
|
+
To apply the Apache License to your work, attach the following
|
|
172
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
173
|
+
replaced with your own identifying information. (Don't include
|
|
174
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
175
|
+
comment syntax for the format in question; you may also recommend
|
|
176
|
+
that file header to be included with the copyright notice:
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Mladen Milankovic
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# @qrkit/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic protocol core for QR-based airgapped wallet flows. Designed for use in browser-based dApps.
|
|
4
|
+
|
|
5
|
+
Handles the ERC-4527 / UR / CBOR stack: decoding scanned QR exports, deriving EVM addresses, building sign requests, and parsing signature responses. No DOM, no React, no external services.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @qrkit/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { parseXpub, deriveEvmAccount, buildEthSignRequestURParts, parseEthSignature } from '@qrkit/core'
|
|
17
|
+
|
|
18
|
+
// 1. Parse the connection QR exported from the wallet (crypto-hdkey or crypto-account)
|
|
19
|
+
const parsed = parseXpub(scannedUR)
|
|
20
|
+
const account = deriveEvmAccount(parsed)
|
|
21
|
+
// account.address → EIP-55 checksummed address
|
|
22
|
+
// account.publicKey → compressed pubkey hex
|
|
23
|
+
// account.sourceFingerprint → required by Shell for signing
|
|
24
|
+
|
|
25
|
+
// 2. Build a sign request — returns UR parts to display as QR codes
|
|
26
|
+
const parts = buildEthSignRequestURParts(message, account.address, account.sourceFingerprint)
|
|
27
|
+
// parts.length === 1 for short messages, >1 for animated QR
|
|
28
|
+
|
|
29
|
+
// 3. After the user scans the wallet's response QR
|
|
30
|
+
const signature = parseEthSignature(scannedResponseUR)
|
|
31
|
+
// → '0x...' hex string
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Browser usage
|
|
35
|
+
|
|
36
|
+
This package is designed to run in the browser. It depends on [`@ngraveio/bc-ur`](https://github.com/ngraveio/bc-ur) which internally uses the Node.js `Buffer` API.
|
|
37
|
+
|
|
38
|
+
Modern bundlers (Vite, webpack, etc.) polyfill `Buffer` automatically. If you see a `Buffer is not defined` error, add the [`buffer`](https://www.npmjs.com/package/buffer) package and configure your bundler to inject it:
|
|
39
|
+
|
|
40
|
+
**Vite:**
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// vite.config.ts
|
|
44
|
+
import { defineConfig } from 'vite'
|
|
45
|
+
import { nodePolyfills } from 'vite-plugin-node-polyfills'
|
|
46
|
+
|
|
47
|
+
export default defineConfig({
|
|
48
|
+
plugins: [nodePolyfills({ include: ['buffer'] })],
|
|
49
|
+
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Or manually in your entry file:**
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { Buffer } from 'buffer'
|
|
56
|
+
globalThis.Buffer = Buffer
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
[Apache 2.0](../../LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
buildEthSignRequestUR: () => buildEthSignRequestUR,
|
|
34
|
+
buildEthSignRequestURParts: () => buildEthSignRequestURParts,
|
|
35
|
+
parseConnection: () => parseConnection,
|
|
36
|
+
parseEthSignature: () => parseEthSignature
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/parseXpub.ts
|
|
41
|
+
var import_bip32 = require("@scure/bip32");
|
|
42
|
+
var import_cborg = require("cborg");
|
|
43
|
+
function get(m, k) {
|
|
44
|
+
if (m instanceof Map) return m.get(k);
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
var passthrough = (v) => v;
|
|
48
|
+
function decodeCbor(cbor) {
|
|
49
|
+
return (0, import_cborg.decode)(cbor, {
|
|
50
|
+
useMaps: true,
|
|
51
|
+
tags: Object.assign([], {
|
|
52
|
+
303: passthrough,
|
|
53
|
+
// crypto-hdkey
|
|
54
|
+
304: passthrough,
|
|
55
|
+
// crypto-keypath
|
|
56
|
+
305: passthrough
|
|
57
|
+
// crypto-coin-info
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function parseCryptoHdKey(map, raw) {
|
|
62
|
+
const keyData = get(map, 3);
|
|
63
|
+
const chainCode = get(map, 4);
|
|
64
|
+
if (!keyData || !chainCode) {
|
|
65
|
+
throw new Error("crypto-hdkey missing key-data or chain-code");
|
|
66
|
+
}
|
|
67
|
+
let purpose;
|
|
68
|
+
let coinType;
|
|
69
|
+
let sourceFingerprint;
|
|
70
|
+
const origin = get(map, 6);
|
|
71
|
+
if (origin) {
|
|
72
|
+
const components = get(origin, 1);
|
|
73
|
+
if (Array.isArray(components)) {
|
|
74
|
+
if (components.length >= 1) purpose = components[0];
|
|
75
|
+
if (components.length >= 3) coinType = components[2];
|
|
76
|
+
}
|
|
77
|
+
sourceFingerprint = get(origin, 2);
|
|
78
|
+
}
|
|
79
|
+
const hdKey = new import_bip32.HDKey({ publicKey: keyData, chainCode });
|
|
80
|
+
return { hdKey, type: "xpub", purpose, coinType, sourceFingerprint, raw };
|
|
81
|
+
}
|
|
82
|
+
function parseScannedUR(scanned) {
|
|
83
|
+
const { type, cbor } = scanned;
|
|
84
|
+
const raw = `ur:${type}`;
|
|
85
|
+
if (type !== "crypto-hdkey" && type !== "crypto-account") {
|
|
86
|
+
throw new Error(`Unsupported UR type: ${type}`);
|
|
87
|
+
}
|
|
88
|
+
const map = decodeCbor(cbor);
|
|
89
|
+
if (type === "crypto-hdkey") {
|
|
90
|
+
return parseCryptoHdKey(map, raw);
|
|
91
|
+
}
|
|
92
|
+
const accounts = map.get(2);
|
|
93
|
+
if (!Array.isArray(accounts) || accounts.length === 0) {
|
|
94
|
+
throw new Error("crypto-account contains no keys");
|
|
95
|
+
}
|
|
96
|
+
return accounts.map((entry) => parseCryptoHdKey(entry, raw));
|
|
97
|
+
}
|
|
98
|
+
function parseXpub(input) {
|
|
99
|
+
if (typeof input !== "string") {
|
|
100
|
+
const result = parseScannedUR(input);
|
|
101
|
+
return Array.isArray(result) ? result : [result];
|
|
102
|
+
}
|
|
103
|
+
const hdKey = import_bip32.HDKey.fromExtendedKey(input.trim());
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
hdKey,
|
|
107
|
+
type: "xpub",
|
|
108
|
+
purpose: void 0,
|
|
109
|
+
coinType: void 0,
|
|
110
|
+
sourceFingerprint: void 0,
|
|
111
|
+
raw: input.trim()
|
|
112
|
+
}
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/eth/address.ts
|
|
117
|
+
var import_sha3 = require("@noble/hashes/sha3.js");
|
|
118
|
+
var secp = __toESM(require("@noble/secp256k1"), 1);
|
|
119
|
+
function pubKeyToEthAddress(compressedPubKey) {
|
|
120
|
+
const uncompressed = secp.Point.fromBytes(compressedPubKey).toBytes(false);
|
|
121
|
+
const hash = (0, import_sha3.keccak_256)(uncompressed.slice(1));
|
|
122
|
+
const hex = [...hash.slice(12)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
123
|
+
return toChecksumAddress(hex);
|
|
124
|
+
}
|
|
125
|
+
function toChecksumAddress(hex) {
|
|
126
|
+
const checksumHash = (0, import_sha3.keccak_256)(new TextEncoder().encode(hex));
|
|
127
|
+
return "0x" + [...hex].map((c, i) => {
|
|
128
|
+
if (c >= "0" && c <= "9") return c;
|
|
129
|
+
const nibble = i % 2 === 0 ? checksumHash[Math.floor(i / 2)] >> 4 & 15 : checksumHash[Math.floor(i / 2)] & 15;
|
|
130
|
+
return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();
|
|
131
|
+
}).join("");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/eth/deriveAccount.ts
|
|
135
|
+
function firstChild(accountKey) {
|
|
136
|
+
return accountKey.deriveChild(0).deriveChild(0);
|
|
137
|
+
}
|
|
138
|
+
function toHex(bytes) {
|
|
139
|
+
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
140
|
+
}
|
|
141
|
+
function deriveEvmAccount(parsed) {
|
|
142
|
+
for (const entry of parsed) {
|
|
143
|
+
const { hdKey, purpose, coinType, type, sourceFingerprint } = entry;
|
|
144
|
+
const isEvm = purpose === 44 && coinType === 60 || purpose === void 0 && type === "xpub";
|
|
145
|
+
if (!isEvm) continue;
|
|
146
|
+
const child = firstChild(hdKey);
|
|
147
|
+
if (!child.publicKey) continue;
|
|
148
|
+
return {
|
|
149
|
+
address: pubKeyToEthAddress(child.publicKey),
|
|
150
|
+
publicKey: toHex(child.publicKey),
|
|
151
|
+
sourceFingerprint
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/parseConnection.ts
|
|
158
|
+
var ALL_CHAINS = ["evm", "btc"];
|
|
159
|
+
function parseConnection(scannedUR, config = {}) {
|
|
160
|
+
const chains = config.chains ?? ALL_CHAINS;
|
|
161
|
+
const parsed = parseXpub(scannedUR);
|
|
162
|
+
const accounts = [];
|
|
163
|
+
if (chains.includes("evm")) {
|
|
164
|
+
const account = deriveEvmAccount(parsed);
|
|
165
|
+
if (account) {
|
|
166
|
+
accounts.push({ chain: "evm", ...account });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return accounts;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/cbor.ts
|
|
173
|
+
function majorType(major, n) {
|
|
174
|
+
const base = major << 5;
|
|
175
|
+
if (n <= 23) return [base | n];
|
|
176
|
+
if (n <= 255) return [base | 24, n];
|
|
177
|
+
if (n <= 65535) return [base | 25, n >> 8 & 255, n & 255];
|
|
178
|
+
return [base | 26, n >> 24 & 255, n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
179
|
+
}
|
|
180
|
+
function encodeItem(value) {
|
|
181
|
+
if (typeof value === "boolean") {
|
|
182
|
+
return [value ? 245 : 244];
|
|
183
|
+
}
|
|
184
|
+
if (typeof value === "number") {
|
|
185
|
+
return majorType(0, value);
|
|
186
|
+
}
|
|
187
|
+
if (value instanceof Uint8Array) {
|
|
188
|
+
return [...majorType(2, value.length), ...value];
|
|
189
|
+
}
|
|
190
|
+
if (typeof value === "string") {
|
|
191
|
+
const bytes = new TextEncoder().encode(value);
|
|
192
|
+
return [...majorType(3, bytes.length), ...bytes];
|
|
193
|
+
}
|
|
194
|
+
if (Array.isArray(value)) {
|
|
195
|
+
return [...majorType(4, value.length), ...value.flatMap(encodeItem)];
|
|
196
|
+
}
|
|
197
|
+
if (value instanceof CborTag) {
|
|
198
|
+
return [...majorType(6, value.tag), ...encodeItem(value.value)];
|
|
199
|
+
}
|
|
200
|
+
if (value instanceof Map) {
|
|
201
|
+
const entries = [...value.entries()];
|
|
202
|
+
return [
|
|
203
|
+
...majorType(5, entries.length),
|
|
204
|
+
...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)])
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
throw new Error(`Unsupported CBOR value: ${typeof value}`);
|
|
208
|
+
}
|
|
209
|
+
var CborTag = class {
|
|
210
|
+
tag;
|
|
211
|
+
value;
|
|
212
|
+
constructor(tag, value) {
|
|
213
|
+
this.tag = tag;
|
|
214
|
+
this.value = value;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
function encode(value) {
|
|
218
|
+
return new Uint8Array(encodeItem(value));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/urEncoding.ts
|
|
222
|
+
var import_bc_ur = require("@ngraveio/bc-ur");
|
|
223
|
+
function encodeURParts(cbor, type, maxFragmentLength = 200) {
|
|
224
|
+
const ur = new import_bc_ur.UR(Buffer.from(cbor), type);
|
|
225
|
+
const encoder = new import_bc_ur.UREncoder(ur, maxFragmentLength);
|
|
226
|
+
return Array.from(
|
|
227
|
+
{ length: encoder.fragmentsLength },
|
|
228
|
+
() => encoder.nextPart().toUpperCase()
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
function encodeURFirstPart(cbor, type, maxFragmentLength = 200) {
|
|
232
|
+
return encodeURParts(cbor, type, maxFragmentLength)[0];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/eth/signRequest.ts
|
|
236
|
+
var DATA_TYPE_ETH_RAW_BYTES = 3;
|
|
237
|
+
var TAG_KEYPATH = 304;
|
|
238
|
+
function randomBytes(n) {
|
|
239
|
+
const buf = new Uint8Array(n);
|
|
240
|
+
crypto.getRandomValues(buf);
|
|
241
|
+
return buf;
|
|
242
|
+
}
|
|
243
|
+
function buildKeypath(purpose, coinType, sourceFingerprint) {
|
|
244
|
+
const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];
|
|
245
|
+
const keypathMap = /* @__PURE__ */ new Map([[1, components]]);
|
|
246
|
+
if (sourceFingerprint !== void 0) {
|
|
247
|
+
keypathMap.set(2, sourceFingerprint);
|
|
248
|
+
}
|
|
249
|
+
return new CborTag(TAG_KEYPATH, keypathMap);
|
|
250
|
+
}
|
|
251
|
+
function buildEthSignRequestCbor(message, address, sourceFingerprint, origin = "qrkit") {
|
|
252
|
+
const requestId = randomBytes(16);
|
|
253
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
254
|
+
const keypath = buildKeypath(44, 60, sourceFingerprint);
|
|
255
|
+
const addrHex = address.replace(/^0x/i, "");
|
|
256
|
+
const addrBytes = new Uint8Array(addrHex.match(/.{2}/g).map((b) => parseInt(b, 16)));
|
|
257
|
+
const map = /* @__PURE__ */ new Map([
|
|
258
|
+
[1, new CborTag(37, requestId)],
|
|
259
|
+
// request-id: uuid = #6.37(bstr)
|
|
260
|
+
[2, messageBytes],
|
|
261
|
+
// sign-data
|
|
262
|
+
[3, DATA_TYPE_ETH_RAW_BYTES],
|
|
263
|
+
// data-type: eth-raw-bytes for EIP-191
|
|
264
|
+
[5, keypath],
|
|
265
|
+
// derivation-path
|
|
266
|
+
[6, addrBytes],
|
|
267
|
+
// address
|
|
268
|
+
[7, origin]
|
|
269
|
+
// origin
|
|
270
|
+
]);
|
|
271
|
+
return encode(map);
|
|
272
|
+
}
|
|
273
|
+
function buildEthSignRequestURParts(message, address, sourceFingerprint, origin) {
|
|
274
|
+
return encodeURParts(
|
|
275
|
+
buildEthSignRequestCbor(message, address, sourceFingerprint, origin),
|
|
276
|
+
"eth-sign-request"
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
function buildEthSignRequestUR(message, address, sourceFingerprint, origin) {
|
|
280
|
+
return encodeURFirstPart(
|
|
281
|
+
buildEthSignRequestCbor(message, address, sourceFingerprint, origin),
|
|
282
|
+
"eth-sign-request"
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/eth/signature.ts
|
|
287
|
+
var import_cborg2 = require("cborg");
|
|
288
|
+
function parseEthSignature(scanned) {
|
|
289
|
+
if (scanned.type !== "eth-signature") {
|
|
290
|
+
throw new Error(`Expected eth-signature, got: ${scanned.type}`);
|
|
291
|
+
}
|
|
292
|
+
const map = (0, import_cborg2.decode)(scanned.cbor, {
|
|
293
|
+
useMaps: true,
|
|
294
|
+
tags: Object.assign([], { 37: (v) => v })
|
|
295
|
+
});
|
|
296
|
+
const sigBytes = map.get(2);
|
|
297
|
+
if (!sigBytes || sigBytes.length < 64) {
|
|
298
|
+
throw new Error("Invalid or missing signature bytes");
|
|
299
|
+
}
|
|
300
|
+
return "0x" + [...sigBytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
301
|
+
}
|
|
302
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
303
|
+
0 && (module.exports = {
|
|
304
|
+
buildEthSignRequestUR,
|
|
305
|
+
buildEthSignRequestURParts,
|
|
306
|
+
parseConnection,
|
|
307
|
+
parseEthSignature
|
|
308
|
+
});
|
|
309
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../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":["// Types\nexport type { ScannedUR, Chain, QRKitConfig, Account, EvmAccount } from \"./types.js\";\nexport type { ParsedXpub, XpubType } from \"./parseXpub.js\";\n\n// Connection\nexport { parseConnection } from \"./parseConnection.js\";\n\n// EVM signing\nexport { buildEthSignRequestUR, buildEthSignRequestURParts } from \"./eth/signRequest.js\";\nexport { parseEthSignature } from \"./eth/signature.js\";\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 */\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 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 hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, 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}\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 } = 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 };\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, UREncoder } from \"@ngraveio/bc-ur\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n // @ngraveio/bc-ur requires a Buffer. In Node.js it is a global; in browsers the\n // bundler must provide the `buffer` polyfill (same requirement as the prototype).\n const ur = new UR(Buffer.from(cbor), type);\n const encoder = new UREncoder(ur, maxFragmentLength);\n return Array.from({ length: encoder.fragmentsLength }, () =>\n encoder.nextPart().toUpperCase(),\n );\n}\n\nexport function encodeURFirstPart(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string {\n return encodeURParts(cbor, type, maxFragmentLength)[0];\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURFirstPart, encodeURParts } from \"../urEncoding.js\";\n\n// EIP-191 personal_sign = eth-raw-bytes (ERC-4527 data type 3)\nconst DATA_TYPE_ETH_RAW_BYTES = 3;\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\nfunction buildEthSignRequestCbor(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin = \"qrkit\",\n): Uint8Array {\n const requestId = randomBytes(16);\n const messageBytes = new TextEncoder().encode(message);\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 const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, messageBytes], // sign-data\n [3, DATA_TYPE_ETH_RAW_BYTES], // data-type: eth-raw-bytes for EIP-191\n [5, keypath], // derivation-path\n [6, addrBytes], // address\n [7, origin], // origin\n ]);\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin?: string,\n): string[] {\n return encodeURParts(\n buildEthSignRequestCbor(message, address, sourceFingerprint, origin),\n \"eth-sign-request\",\n );\n}\n\nexport function buildEthSignRequestUR(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin?: string,\n): string {\n return encodeURFirstPart(\n buildEthSignRequestCbor(message, address, sourceFingerprint, origin),\n \"eth-sign-request\",\n );\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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AACtB,mBAAsD;AAkBtD,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,aAAO,aAAAA,QAAW,MAAM;AAAA,IACtB,SAAS;AAAA,IACT,MAAM,OAAO,OAAO,CAAC,GAAmB;AAAA,MACtC,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AAG,IAAI,QAAQ,IAAI,SAAS,CAAC;AAAA;AAAA,IAC9B,CAAC,GAAG,YAAY;AAAA;AAAA,IAChB,CAAC,GAAG,uBAAuB;AAAA;AAAA,IAC3B,CAAC,GAAG,OAAO;AAAA;AAAA,IACX,CAAC,GAAG,SAAS;AAAA;AAAA,IACb,CAAC,GAAG,MAAM;AAAA;AAAA,EACZ,CAAC;AAED,SAAO,OAAO,GAAG;AACnB;AAEO,SAAS,2BACd,SACA,SACA,mBACA,QACU;AACV,SAAO;AAAA,IACL,wBAAwB,SAAS,SAAS,mBAAmB,MAAM;AAAA,IACnE;AAAA,EACF;AACF;AAEO,SAAS,sBACd,SACA,SACA,mBACA,QACQ;AACR,SAAO;AAAA,IACL,wBAAwB,SAAS,SAAS,mBAAmB,MAAM;AAAA,IACnE;AAAA,EACF;AACF;;;AC5EA,IAAAC,gBAAsD;AAI/C,SAAS,kBAAkB,SAA4B;AAC5D,MAAI,QAAQ,SAAS,iBAAiB;AACpC,UAAM,IAAI,MAAM,gCAAgC,QAAQ,IAAI,EAAE;AAAA,EAChE;AAEA,QAAM,UAAM,cAAAC,QAAW,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","import_cborg","cborDecode"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
|
|
3
|
+
interface ScannedUR {
|
|
4
|
+
type: string;
|
|
5
|
+
cbor: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
type Chain = "evm" | "btc";
|
|
8
|
+
interface QRKitConfig {
|
|
9
|
+
/** Which chains the dApp supports. Accounts for other chains are excluded.
|
|
10
|
+
* If omitted, all supported chains are tried. */
|
|
11
|
+
chains?: Chain[];
|
|
12
|
+
}
|
|
13
|
+
interface EvmAccount {
|
|
14
|
+
chain: "evm";
|
|
15
|
+
address: string;
|
|
16
|
+
publicKey: string;
|
|
17
|
+
/** source-fingerprint from the scanned xpub — required by Shell for signing */
|
|
18
|
+
sourceFingerprint: number | undefined;
|
|
19
|
+
}
|
|
20
|
+
type Account = EvmAccount;
|
|
21
|
+
|
|
22
|
+
type XpubType = "xpub";
|
|
23
|
+
interface ParsedXpub {
|
|
24
|
+
hdKey: HDKey;
|
|
25
|
+
type: XpubType;
|
|
26
|
+
/** BIP-44 purpose index: 44 for EVM */
|
|
27
|
+
purpose: number | undefined;
|
|
28
|
+
/** BIP-44 coin type: 60 = ETH */
|
|
29
|
+
coinType: number | undefined;
|
|
30
|
+
/** source-fingerprint from the origin keypath — required by Shell for signing */
|
|
31
|
+
sourceFingerprint: number | undefined;
|
|
32
|
+
raw: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse a connection QR (crypto-hdkey or crypto-account) and return
|
|
37
|
+
* only the accounts for the chains configured in QRKitConfig.
|
|
38
|
+
*
|
|
39
|
+
* A dApp configured with `chains: ["evm"]` will never see BTC accounts,
|
|
40
|
+
* and vice versa. Both chains can be enabled with `chains: ["evm", "btc"]`.
|
|
41
|
+
* If `chains` is omitted, all supported chains are tried.
|
|
42
|
+
*/
|
|
43
|
+
declare function parseConnection(scannedUR: ScannedUR, config?: QRKitConfig): Account[];
|
|
44
|
+
|
|
45
|
+
declare function buildEthSignRequestURParts(message: string, address: string, sourceFingerprint: number | undefined, origin?: string): string[];
|
|
46
|
+
declare function buildEthSignRequestUR(message: string, address: string, sourceFingerprint: number | undefined, origin?: string): string;
|
|
47
|
+
|
|
48
|
+
declare function parseEthSignature(scanned: ScannedUR): string;
|
|
49
|
+
|
|
50
|
+
export { type Account, type Chain, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildEthSignRequestUR, buildEthSignRequestURParts, parseConnection, parseEthSignature };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HDKey } from '@scure/bip32';
|
|
2
|
+
|
|
3
|
+
interface ScannedUR {
|
|
4
|
+
type: string;
|
|
5
|
+
cbor: Uint8Array;
|
|
6
|
+
}
|
|
7
|
+
type Chain = "evm" | "btc";
|
|
8
|
+
interface QRKitConfig {
|
|
9
|
+
/** Which chains the dApp supports. Accounts for other chains are excluded.
|
|
10
|
+
* If omitted, all supported chains are tried. */
|
|
11
|
+
chains?: Chain[];
|
|
12
|
+
}
|
|
13
|
+
interface EvmAccount {
|
|
14
|
+
chain: "evm";
|
|
15
|
+
address: string;
|
|
16
|
+
publicKey: string;
|
|
17
|
+
/** source-fingerprint from the scanned xpub — required by Shell for signing */
|
|
18
|
+
sourceFingerprint: number | undefined;
|
|
19
|
+
}
|
|
20
|
+
type Account = EvmAccount;
|
|
21
|
+
|
|
22
|
+
type XpubType = "xpub";
|
|
23
|
+
interface ParsedXpub {
|
|
24
|
+
hdKey: HDKey;
|
|
25
|
+
type: XpubType;
|
|
26
|
+
/** BIP-44 purpose index: 44 for EVM */
|
|
27
|
+
purpose: number | undefined;
|
|
28
|
+
/** BIP-44 coin type: 60 = ETH */
|
|
29
|
+
coinType: number | undefined;
|
|
30
|
+
/** source-fingerprint from the origin keypath — required by Shell for signing */
|
|
31
|
+
sourceFingerprint: number | undefined;
|
|
32
|
+
raw: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse a connection QR (crypto-hdkey or crypto-account) and return
|
|
37
|
+
* only the accounts for the chains configured in QRKitConfig.
|
|
38
|
+
*
|
|
39
|
+
* A dApp configured with `chains: ["evm"]` will never see BTC accounts,
|
|
40
|
+
* and vice versa. Both chains can be enabled with `chains: ["evm", "btc"]`.
|
|
41
|
+
* If `chains` is omitted, all supported chains are tried.
|
|
42
|
+
*/
|
|
43
|
+
declare function parseConnection(scannedUR: ScannedUR, config?: QRKitConfig): Account[];
|
|
44
|
+
|
|
45
|
+
declare function buildEthSignRequestURParts(message: string, address: string, sourceFingerprint: number | undefined, origin?: string): string[];
|
|
46
|
+
declare function buildEthSignRequestUR(message: string, address: string, sourceFingerprint: number | undefined, origin?: string): string;
|
|
47
|
+
|
|
48
|
+
declare function parseEthSignature(scanned: ScannedUR): string;
|
|
49
|
+
|
|
50
|
+
export { type Account, type Chain, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildEthSignRequestUR, buildEthSignRequestURParts, parseConnection, parseEthSignature };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// src/parseXpub.ts
|
|
2
|
+
import { HDKey } from "@scure/bip32";
|
|
3
|
+
import { decode as cborDecode } from "cborg";
|
|
4
|
+
function get(m, k) {
|
|
5
|
+
if (m instanceof Map) return m.get(k);
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
8
|
+
var passthrough = (v) => v;
|
|
9
|
+
function decodeCbor(cbor) {
|
|
10
|
+
return cborDecode(cbor, {
|
|
11
|
+
useMaps: true,
|
|
12
|
+
tags: Object.assign([], {
|
|
13
|
+
303: passthrough,
|
|
14
|
+
// crypto-hdkey
|
|
15
|
+
304: passthrough,
|
|
16
|
+
// crypto-keypath
|
|
17
|
+
305: passthrough
|
|
18
|
+
// crypto-coin-info
|
|
19
|
+
})
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function parseCryptoHdKey(map, raw) {
|
|
23
|
+
const keyData = get(map, 3);
|
|
24
|
+
const chainCode = get(map, 4);
|
|
25
|
+
if (!keyData || !chainCode) {
|
|
26
|
+
throw new Error("crypto-hdkey missing key-data or chain-code");
|
|
27
|
+
}
|
|
28
|
+
let purpose;
|
|
29
|
+
let coinType;
|
|
30
|
+
let sourceFingerprint;
|
|
31
|
+
const origin = get(map, 6);
|
|
32
|
+
if (origin) {
|
|
33
|
+
const components = get(origin, 1);
|
|
34
|
+
if (Array.isArray(components)) {
|
|
35
|
+
if (components.length >= 1) purpose = components[0];
|
|
36
|
+
if (components.length >= 3) coinType = components[2];
|
|
37
|
+
}
|
|
38
|
+
sourceFingerprint = get(origin, 2);
|
|
39
|
+
}
|
|
40
|
+
const hdKey = new HDKey({ publicKey: keyData, chainCode });
|
|
41
|
+
return { hdKey, type: "xpub", purpose, coinType, sourceFingerprint, raw };
|
|
42
|
+
}
|
|
43
|
+
function parseScannedUR(scanned) {
|
|
44
|
+
const { type, cbor } = scanned;
|
|
45
|
+
const raw = `ur:${type}`;
|
|
46
|
+
if (type !== "crypto-hdkey" && type !== "crypto-account") {
|
|
47
|
+
throw new Error(`Unsupported UR type: ${type}`);
|
|
48
|
+
}
|
|
49
|
+
const map = decodeCbor(cbor);
|
|
50
|
+
if (type === "crypto-hdkey") {
|
|
51
|
+
return parseCryptoHdKey(map, raw);
|
|
52
|
+
}
|
|
53
|
+
const accounts = map.get(2);
|
|
54
|
+
if (!Array.isArray(accounts) || accounts.length === 0) {
|
|
55
|
+
throw new Error("crypto-account contains no keys");
|
|
56
|
+
}
|
|
57
|
+
return accounts.map((entry) => parseCryptoHdKey(entry, raw));
|
|
58
|
+
}
|
|
59
|
+
function parseXpub(input) {
|
|
60
|
+
if (typeof input !== "string") {
|
|
61
|
+
const result = parseScannedUR(input);
|
|
62
|
+
return Array.isArray(result) ? result : [result];
|
|
63
|
+
}
|
|
64
|
+
const hdKey = HDKey.fromExtendedKey(input.trim());
|
|
65
|
+
return [
|
|
66
|
+
{
|
|
67
|
+
hdKey,
|
|
68
|
+
type: "xpub",
|
|
69
|
+
purpose: void 0,
|
|
70
|
+
coinType: void 0,
|
|
71
|
+
sourceFingerprint: void 0,
|
|
72
|
+
raw: input.trim()
|
|
73
|
+
}
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/eth/address.ts
|
|
78
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
79
|
+
import * as secp from "@noble/secp256k1";
|
|
80
|
+
function pubKeyToEthAddress(compressedPubKey) {
|
|
81
|
+
const uncompressed = secp.Point.fromBytes(compressedPubKey).toBytes(false);
|
|
82
|
+
const hash = keccak_256(uncompressed.slice(1));
|
|
83
|
+
const hex = [...hash.slice(12)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
84
|
+
return toChecksumAddress(hex);
|
|
85
|
+
}
|
|
86
|
+
function toChecksumAddress(hex) {
|
|
87
|
+
const checksumHash = keccak_256(new TextEncoder().encode(hex));
|
|
88
|
+
return "0x" + [...hex].map((c, i) => {
|
|
89
|
+
if (c >= "0" && c <= "9") return c;
|
|
90
|
+
const nibble = i % 2 === 0 ? checksumHash[Math.floor(i / 2)] >> 4 & 15 : checksumHash[Math.floor(i / 2)] & 15;
|
|
91
|
+
return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();
|
|
92
|
+
}).join("");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/eth/deriveAccount.ts
|
|
96
|
+
function firstChild(accountKey) {
|
|
97
|
+
return accountKey.deriveChild(0).deriveChild(0);
|
|
98
|
+
}
|
|
99
|
+
function toHex(bytes) {
|
|
100
|
+
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
101
|
+
}
|
|
102
|
+
function deriveEvmAccount(parsed) {
|
|
103
|
+
for (const entry of parsed) {
|
|
104
|
+
const { hdKey, purpose, coinType, type, sourceFingerprint } = entry;
|
|
105
|
+
const isEvm = purpose === 44 && coinType === 60 || purpose === void 0 && type === "xpub";
|
|
106
|
+
if (!isEvm) continue;
|
|
107
|
+
const child = firstChild(hdKey);
|
|
108
|
+
if (!child.publicKey) continue;
|
|
109
|
+
return {
|
|
110
|
+
address: pubKeyToEthAddress(child.publicKey),
|
|
111
|
+
publicKey: toHex(child.publicKey),
|
|
112
|
+
sourceFingerprint
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/parseConnection.ts
|
|
119
|
+
var ALL_CHAINS = ["evm", "btc"];
|
|
120
|
+
function parseConnection(scannedUR, config = {}) {
|
|
121
|
+
const chains = config.chains ?? ALL_CHAINS;
|
|
122
|
+
const parsed = parseXpub(scannedUR);
|
|
123
|
+
const accounts = [];
|
|
124
|
+
if (chains.includes("evm")) {
|
|
125
|
+
const account = deriveEvmAccount(parsed);
|
|
126
|
+
if (account) {
|
|
127
|
+
accounts.push({ chain: "evm", ...account });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return accounts;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/cbor.ts
|
|
134
|
+
function majorType(major, n) {
|
|
135
|
+
const base = major << 5;
|
|
136
|
+
if (n <= 23) return [base | n];
|
|
137
|
+
if (n <= 255) return [base | 24, n];
|
|
138
|
+
if (n <= 65535) return [base | 25, n >> 8 & 255, n & 255];
|
|
139
|
+
return [base | 26, n >> 24 & 255, n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
140
|
+
}
|
|
141
|
+
function encodeItem(value) {
|
|
142
|
+
if (typeof value === "boolean") {
|
|
143
|
+
return [value ? 245 : 244];
|
|
144
|
+
}
|
|
145
|
+
if (typeof value === "number") {
|
|
146
|
+
return majorType(0, value);
|
|
147
|
+
}
|
|
148
|
+
if (value instanceof Uint8Array) {
|
|
149
|
+
return [...majorType(2, value.length), ...value];
|
|
150
|
+
}
|
|
151
|
+
if (typeof value === "string") {
|
|
152
|
+
const bytes = new TextEncoder().encode(value);
|
|
153
|
+
return [...majorType(3, bytes.length), ...bytes];
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(value)) {
|
|
156
|
+
return [...majorType(4, value.length), ...value.flatMap(encodeItem)];
|
|
157
|
+
}
|
|
158
|
+
if (value instanceof CborTag) {
|
|
159
|
+
return [...majorType(6, value.tag), ...encodeItem(value.value)];
|
|
160
|
+
}
|
|
161
|
+
if (value instanceof Map) {
|
|
162
|
+
const entries = [...value.entries()];
|
|
163
|
+
return [
|
|
164
|
+
...majorType(5, entries.length),
|
|
165
|
+
...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)])
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
throw new Error(`Unsupported CBOR value: ${typeof value}`);
|
|
169
|
+
}
|
|
170
|
+
var CborTag = class {
|
|
171
|
+
tag;
|
|
172
|
+
value;
|
|
173
|
+
constructor(tag, value) {
|
|
174
|
+
this.tag = tag;
|
|
175
|
+
this.value = value;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
function encode(value) {
|
|
179
|
+
return new Uint8Array(encodeItem(value));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/urEncoding.ts
|
|
183
|
+
import { UR, UREncoder } from "@ngraveio/bc-ur";
|
|
184
|
+
function encodeURParts(cbor, type, maxFragmentLength = 200) {
|
|
185
|
+
const ur = new UR(Buffer.from(cbor), type);
|
|
186
|
+
const encoder = new UREncoder(ur, maxFragmentLength);
|
|
187
|
+
return Array.from(
|
|
188
|
+
{ length: encoder.fragmentsLength },
|
|
189
|
+
() => encoder.nextPart().toUpperCase()
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
function encodeURFirstPart(cbor, type, maxFragmentLength = 200) {
|
|
193
|
+
return encodeURParts(cbor, type, maxFragmentLength)[0];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/eth/signRequest.ts
|
|
197
|
+
var DATA_TYPE_ETH_RAW_BYTES = 3;
|
|
198
|
+
var TAG_KEYPATH = 304;
|
|
199
|
+
function randomBytes(n) {
|
|
200
|
+
const buf = new Uint8Array(n);
|
|
201
|
+
crypto.getRandomValues(buf);
|
|
202
|
+
return buf;
|
|
203
|
+
}
|
|
204
|
+
function buildKeypath(purpose, coinType, sourceFingerprint) {
|
|
205
|
+
const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];
|
|
206
|
+
const keypathMap = /* @__PURE__ */ new Map([[1, components]]);
|
|
207
|
+
if (sourceFingerprint !== void 0) {
|
|
208
|
+
keypathMap.set(2, sourceFingerprint);
|
|
209
|
+
}
|
|
210
|
+
return new CborTag(TAG_KEYPATH, keypathMap);
|
|
211
|
+
}
|
|
212
|
+
function buildEthSignRequestCbor(message, address, sourceFingerprint, origin = "qrkit") {
|
|
213
|
+
const requestId = randomBytes(16);
|
|
214
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
215
|
+
const keypath = buildKeypath(44, 60, sourceFingerprint);
|
|
216
|
+
const addrHex = address.replace(/^0x/i, "");
|
|
217
|
+
const addrBytes = new Uint8Array(addrHex.match(/.{2}/g).map((b) => parseInt(b, 16)));
|
|
218
|
+
const map = /* @__PURE__ */ new Map([
|
|
219
|
+
[1, new CborTag(37, requestId)],
|
|
220
|
+
// request-id: uuid = #6.37(bstr)
|
|
221
|
+
[2, messageBytes],
|
|
222
|
+
// sign-data
|
|
223
|
+
[3, DATA_TYPE_ETH_RAW_BYTES],
|
|
224
|
+
// data-type: eth-raw-bytes for EIP-191
|
|
225
|
+
[5, keypath],
|
|
226
|
+
// derivation-path
|
|
227
|
+
[6, addrBytes],
|
|
228
|
+
// address
|
|
229
|
+
[7, origin]
|
|
230
|
+
// origin
|
|
231
|
+
]);
|
|
232
|
+
return encode(map);
|
|
233
|
+
}
|
|
234
|
+
function buildEthSignRequestURParts(message, address, sourceFingerprint, origin) {
|
|
235
|
+
return encodeURParts(
|
|
236
|
+
buildEthSignRequestCbor(message, address, sourceFingerprint, origin),
|
|
237
|
+
"eth-sign-request"
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
function buildEthSignRequestUR(message, address, sourceFingerprint, origin) {
|
|
241
|
+
return encodeURFirstPart(
|
|
242
|
+
buildEthSignRequestCbor(message, address, sourceFingerprint, origin),
|
|
243
|
+
"eth-sign-request"
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/eth/signature.ts
|
|
248
|
+
import { decode as cborDecode2 } from "cborg";
|
|
249
|
+
function parseEthSignature(scanned) {
|
|
250
|
+
if (scanned.type !== "eth-signature") {
|
|
251
|
+
throw new Error(`Expected eth-signature, got: ${scanned.type}`);
|
|
252
|
+
}
|
|
253
|
+
const map = cborDecode2(scanned.cbor, {
|
|
254
|
+
useMaps: true,
|
|
255
|
+
tags: Object.assign([], { 37: (v) => v })
|
|
256
|
+
});
|
|
257
|
+
const sigBytes = map.get(2);
|
|
258
|
+
if (!sigBytes || sigBytes.length < 64) {
|
|
259
|
+
throw new Error("Invalid or missing signature bytes");
|
|
260
|
+
}
|
|
261
|
+
return "0x" + [...sigBytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
262
|
+
}
|
|
263
|
+
export {
|
|
264
|
+
buildEthSignRequestUR,
|
|
265
|
+
buildEthSignRequestURParts,
|
|
266
|
+
parseConnection,
|
|
267
|
+
parseEthSignature
|
|
268
|
+
};
|
|
269
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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 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 hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, 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}\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 } = 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 };\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, UREncoder } from \"@ngraveio/bc-ur\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n // @ngraveio/bc-ur requires a Buffer. In Node.js it is a global; in browsers the\n // bundler must provide the `buffer` polyfill (same requirement as the prototype).\n const ur = new UR(Buffer.from(cbor), type);\n const encoder = new UREncoder(ur, maxFragmentLength);\n return Array.from({ length: encoder.fragmentsLength }, () =>\n encoder.nextPart().toUpperCase(),\n );\n}\n\nexport function encodeURFirstPart(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string {\n return encodeURParts(cbor, type, maxFragmentLength)[0];\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURFirstPart, encodeURParts } from \"../urEncoding.js\";\n\n// EIP-191 personal_sign = eth-raw-bytes (ERC-4527 data type 3)\nconst DATA_TYPE_ETH_RAW_BYTES = 3;\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\nfunction buildEthSignRequestCbor(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin = \"qrkit\",\n): Uint8Array {\n const requestId = randomBytes(16);\n const messageBytes = new TextEncoder().encode(message);\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 const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, messageBytes], // sign-data\n [3, DATA_TYPE_ETH_RAW_BYTES], // data-type: eth-raw-bytes for EIP-191\n [5, keypath], // derivation-path\n [6, addrBytes], // address\n [7, origin], // origin\n ]);\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin?: string,\n): string[] {\n return encodeURParts(\n buildEthSignRequestCbor(message, address, sourceFingerprint, origin),\n \"eth-sign-request\",\n );\n}\n\nexport function buildEthSignRequestUR(\n message: string,\n address: string,\n sourceFingerprint: number | undefined,\n origin?: string,\n): string {\n return encodeURFirstPart(\n buildEthSignRequestCbor(message, address, sourceFingerprint, origin),\n \"eth-sign-request\",\n );\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;AAkBtD,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,QAAQ,IAAI,MAAM,EAAE,WAAW,SAAS,UAAU,CAAC;AACzD,SAAO,EAAE,OAAO,MAAM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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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qrkit/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Protocol core for QR-based airgapped wallet flows — UR decoding, xpub parsing, address derivation, sign request and signature handling.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@ngraveio/bc-ur": "^1.1.13",
|
|
22
|
+
"@noble/hashes": "^2.0.1",
|
|
23
|
+
"@noble/secp256k1": "^3.0.0",
|
|
24
|
+
"@scure/bip32": "^2.0.1",
|
|
25
|
+
"cborg": "^4.5.8"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"buffer": ">=6"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"buffer": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"eslint": "^10.2.0",
|
|
37
|
+
"tsup": "^8.4.0",
|
|
38
|
+
"tsx": "^4.21.0",
|
|
39
|
+
"typescript": "~5.9.3",
|
|
40
|
+
"vitest": "^4.1.2"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"dev": "tsup --watch",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"lint": "eslint src",
|
|
48
|
+
"example": "tsx examples/eth-flow.ts"
|
|
49
|
+
}
|
|
50
|
+
}
|