@btc-vision/wallet-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +24 -0
  3. package/es/bundle.js +2 -0
  4. package/es/bundle.js.LICENSE.txt +48 -0
  5. package/lib/address/index.d.ts +37 -0
  6. package/lib/address/index.js +235 -0
  7. package/lib/bitcoin-core.d.ts +5 -0
  8. package/lib/bitcoin-core.js +37 -0
  9. package/lib/constants.d.ts +1 -0
  10. package/lib/constants.js +5 -0
  11. package/lib/error.d.ts +20 -0
  12. package/lib/error.js +29 -0
  13. package/lib/index.d.ts +14 -0
  14. package/lib/index.js +50 -0
  15. package/lib/keyring/hd-keyring.d.ts +50 -0
  16. package/lib/keyring/hd-keyring.js +238 -0
  17. package/lib/keyring/index.d.ts +4 -0
  18. package/lib/keyring/index.js +20 -0
  19. package/lib/keyring/interfaces/SimpleKeyringOptions.d.ts +53 -0
  20. package/lib/keyring/interfaces/SimpleKeyringOptions.js +85 -0
  21. package/lib/keyring/keystone-keyring.d.ts +82 -0
  22. package/lib/keyring/keystone-keyring.js +377 -0
  23. package/lib/keyring/simple-keyring.d.ts +11 -0
  24. package/lib/keyring/simple-keyring.js +66 -0
  25. package/lib/message/bip322-simple.d.ts +19 -0
  26. package/lib/message/bip322-simple.js +182 -0
  27. package/lib/message/deterministic-ecdsa.d.ts +2 -0
  28. package/lib/message/deterministic-ecdsa.js +83 -0
  29. package/lib/message/ecdsa.d.ts +3 -0
  30. package/lib/message/ecdsa.js +32 -0
  31. package/lib/message/index.d.ts +3 -0
  32. package/lib/message/index.js +19 -0
  33. package/lib/network/index.d.ts +14 -0
  34. package/lib/network/index.js +40 -0
  35. package/lib/runes/index.d.ts +1 -0
  36. package/lib/runes/index.js +17 -0
  37. package/lib/runes/rund_id.d.ts +11 -0
  38. package/lib/runes/rund_id.js +30 -0
  39. package/lib/runes/varint.d.ts +15 -0
  40. package/lib/runes/varint.js +45 -0
  41. package/lib/src/address/index.d.ts +37 -0
  42. package/lib/src/bitcoin-core.d.ts +5 -0
  43. package/lib/src/constants.d.ts +1 -0
  44. package/lib/src/error.d.ts +20 -0
  45. package/lib/src/index.d.ts +14 -0
  46. package/lib/src/keyring/hd-keyring.d.ts +50 -0
  47. package/lib/src/keyring/index.d.ts +4 -0
  48. package/lib/src/keyring/interfaces/SimpleKeyringOptions.d.ts +53 -0
  49. package/lib/src/keyring/keystone-keyring.d.ts +82 -0
  50. package/lib/src/keyring/simple-keyring.d.ts +11 -0
  51. package/lib/src/message/bip322-simple.d.ts +19 -0
  52. package/lib/src/message/deterministic-ecdsa.d.ts +2 -0
  53. package/lib/src/message/ecdsa.d.ts +3 -0
  54. package/lib/src/message/index.d.ts +3 -0
  55. package/lib/src/network/index.d.ts +14 -0
  56. package/lib/src/runes/index.d.ts +1 -0
  57. package/lib/src/runes/rund_id.d.ts +11 -0
  58. package/lib/src/runes/varint.d.ts +15 -0
  59. package/lib/src/transaction/index.d.ts +3 -0
  60. package/lib/src/transaction/inscription-utxo.d.ts +33 -0
  61. package/lib/src/transaction/transaction.d.ts +52 -0
  62. package/lib/src/transaction/utxo.d.ts +35 -0
  63. package/lib/src/tx-helpers/index.d.ts +8 -0
  64. package/lib/src/tx-helpers/send-atomicals-ft.d.ts +16 -0
  65. package/lib/src/tx-helpers/send-atomicals-nft.d.ts +14 -0
  66. package/lib/src/tx-helpers/send-btc.d.ts +28 -0
  67. package/lib/src/tx-helpers/send-inscription.d.ts +16 -0
  68. package/lib/src/tx-helpers/send-inscriptions.d.ts +14 -0
  69. package/lib/src/tx-helpers/send-runes.d.ts +19 -0
  70. package/lib/src/tx-helpers/split-inscription-utxo.d.ts +15 -0
  71. package/lib/src/types.d.ts +59 -0
  72. package/lib/src/utils.d.ts +23 -0
  73. package/lib/src/wallet/abstract-wallet.d.ts +6 -0
  74. package/lib/src/wallet/estimate-wallet.d.ts +23 -0
  75. package/lib/src/wallet/index.d.ts +3 -0
  76. package/lib/src/wallet/local-wallet.d.ts +23 -0
  77. package/lib/test/address/address.test.d.ts +1 -0
  78. package/lib/test/keyring/hd-keyring.test.d.ts +1 -0
  79. package/lib/test/keyring/keystone-keyring.test.d.ts +1 -0
  80. package/lib/test/keyring/simple-keyring.test.d.ts +1 -0
  81. package/lib/test/message/message.test.d.ts +1 -0
  82. package/lib/test/runes/varint.test.d.ts +1 -0
  83. package/lib/test/transaction/transaction.test.d.ts +1 -0
  84. package/lib/test/transaction/utxo.test.d.ts +1 -0
  85. package/lib/test/tx-helpers/send-atomicals-ft.test.d.ts +1 -0
  86. package/lib/test/tx-helpers/send-atomicals-nft.test.d.ts +1 -0
  87. package/lib/test/tx-helpers/send-btc.test.d.ts +1 -0
  88. package/lib/test/tx-helpers/send-inscription.test.d.ts +1 -0
  89. package/lib/test/tx-helpers/send-inscriptions.test.d.ts +1 -0
  90. package/lib/test/tx-helpers/send-runes.test.d.ts +1 -0
  91. package/lib/test/tx-helpers/split-inscription-utxo.test.d.ts +1 -0
  92. package/lib/test/tx-helpers/utils.d.ts +217 -0
  93. package/lib/test/utils.d.ts +4 -0
  94. package/lib/test/wallet/local-wallet.test.d.ts +1 -0
  95. package/lib/transaction/index.d.ts +3 -0
  96. package/lib/transaction/index.js +19 -0
  97. package/lib/transaction/inscription-utxo.d.ts +33 -0
  98. package/lib/transaction/inscription-utxo.js +120 -0
  99. package/lib/transaction/transaction.d.ts +52 -0
  100. package/lib/transaction/transaction.js +358 -0
  101. package/lib/transaction/utxo.d.ts +35 -0
  102. package/lib/transaction/utxo.js +107 -0
  103. package/lib/tx-helpers/index.d.ts +8 -0
  104. package/lib/tx-helpers/index.js +18 -0
  105. package/lib/tx-helpers/send-atomicals-ft.d.ts +16 -0
  106. package/lib/tx-helpers/send-atomicals-ft.js +66 -0
  107. package/lib/tx-helpers/send-atomicals-nft.d.ts +14 -0
  108. package/lib/tx-helpers/send-atomicals-nft.js +45 -0
  109. package/lib/tx-helpers/send-btc.d.ts +28 -0
  110. package/lib/tx-helpers/send-btc.js +78 -0
  111. package/lib/tx-helpers/send-inscription.d.ts +16 -0
  112. package/lib/tx-helpers/send-inscription.js +49 -0
  113. package/lib/tx-helpers/send-inscriptions.d.ts +14 -0
  114. package/lib/tx-helpers/send-inscriptions.js +45 -0
  115. package/lib/tx-helpers/send-runes.d.ts +19 -0
  116. package/lib/tx-helpers/send-runes.js +101 -0
  117. package/lib/tx-helpers/split-inscription-utxo.d.ts +15 -0
  118. package/lib/tx-helpers/split-inscription-utxo.js +58 -0
  119. package/lib/types.d.ts +59 -0
  120. package/lib/types.js +15 -0
  121. package/lib/utils.d.ts +23 -0
  122. package/lib/utils.js +71 -0
  123. package/lib/wallet/abstract-wallet.d.ts +6 -0
  124. package/lib/wallet/abstract-wallet.js +2 -0
  125. package/lib/wallet/estimate-wallet.d.ts +23 -0
  126. package/lib/wallet/estimate-wallet.js +174 -0
  127. package/lib/wallet/index.d.ts +3 -0
  128. package/lib/wallet/index.js +19 -0
  129. package/lib/wallet/local-wallet.d.ts +23 -0
  130. package/lib/wallet/local-wallet.js +183 -0
  131. package/package.json +97 -0
@@ -0,0 +1,377 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.KeystoneKeyring = void 0;
39
+ const keystone_sdk_1 = __importStar(require("@keystonehq/keystone-sdk"));
40
+ const utils_1 = require("@keystonehq/keystone-sdk/dist/utils");
41
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
42
+ const bitcore_lib_1 = __importDefault(require("bitcore-lib"));
43
+ const message_1 = require("../message");
44
+ const SimpleKeyringOptions_1 = require("./interfaces/SimpleKeyringOptions");
45
+ const type = 'Keystone';
46
+ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
47
+ constructor(opts) {
48
+ super((opts === null || opts === void 0 ? void 0 : opts.network) || bitcoinjs_lib_1.networks.bitcoin);
49
+ this.type = type;
50
+ this.mfp = '';
51
+ this.keys = [];
52
+ this.activeIndexes = [];
53
+ this.root = null;
54
+ this.page = 0;
55
+ this.perPage = 5;
56
+ this.origin = 'UniSat Wallet';
57
+ if (opts) {
58
+ this.deserialize(opts);
59
+ }
60
+ }
61
+ initFromUR(type, cbor) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const keystoneSDK = new keystone_sdk_1.default({
64
+ origin: this.origin
65
+ });
66
+ const account = keystoneSDK.parseAccount(new keystone_sdk_1.UR(Buffer.from(cbor, 'hex'), type));
67
+ this.deserialize({
68
+ mfp: account.masterFingerprint,
69
+ keys: account.keys.map((k) => ({
70
+ path: k.path,
71
+ extendedPublicKey: k.extendedPublicKey
72
+ }))
73
+ });
74
+ });
75
+ }
76
+ getHardenedPath(hdPath) {
77
+ const paths = hdPath.split('/');
78
+ return paths.slice(0, 4).join('/');
79
+ }
80
+ getHDPublicKey(hdPath) {
81
+ const path = this.getHardenedPath(hdPath);
82
+ const key = this.keys.find((v) => v.path === path);
83
+ if (!key) {
84
+ throw new Error('Invalid path');
85
+ }
86
+ return new bitcore_lib_1.default.HDPublicKey(key.extendedPublicKey);
87
+ }
88
+ getDefaultHdPath() {
89
+ return "m/44'/0'/0'/0";
90
+ }
91
+ initRoot() {
92
+ var _a;
93
+ this.root = this.getHDPublicKey((_a = this.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath());
94
+ }
95
+ deserialize(opts) {
96
+ var _a;
97
+ this.mfp = opts.mfp;
98
+ this.keys = opts.keys;
99
+ this.hdPath = (_a = opts.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath();
100
+ this.activeIndexes = opts.activeIndexes ? [...opts.activeIndexes] : [];
101
+ this.initRoot();
102
+ if (opts.hdPath !== null &&
103
+ opts.hdPath !== undefined &&
104
+ opts.hdPath.length >= 13 &&
105
+ opts.hdPath[opts.hdPath.length - 1] === '1') {
106
+ this.root = this.root.derive(`m/1`);
107
+ }
108
+ }
109
+ serialize() {
110
+ return {
111
+ mfp: this.mfp,
112
+ keys: this.keys,
113
+ hdPath: this.hdPath,
114
+ activeIndexes: this.activeIndexes
115
+ };
116
+ }
117
+ addAccounts(numberOfAccounts = 1) {
118
+ let count = numberOfAccounts;
119
+ let i = 0;
120
+ const pubkeys = [];
121
+ while (count) {
122
+ if (this.activeIndexes.includes(i)) {
123
+ i++;
124
+ }
125
+ else {
126
+ const w = this.getWalletByIndex(i);
127
+ pubkeys.push(w.publicKey);
128
+ this.activeIndexes.push(i);
129
+ count--;
130
+ }
131
+ }
132
+ return pubkeys;
133
+ }
134
+ addChangeAddressAccounts(numberOfAccounts = 1) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ let count = numberOfAccounts;
137
+ let i = 0;
138
+ const pubkeys = [];
139
+ while (count) {
140
+ if (this.activeIndexes.includes(i)) {
141
+ i++;
142
+ }
143
+ else {
144
+ const w = this.getChangeAddressWalletByIndex(i);
145
+ pubkeys.push(w.publicKey);
146
+ this.activeIndexes.push(i);
147
+ count--;
148
+ }
149
+ }
150
+ return Promise.resolve(pubkeys);
151
+ });
152
+ }
153
+ getAccounts() {
154
+ if (this.hdPath !== null &&
155
+ this.hdPath !== undefined &&
156
+ this.hdPath.length >= 13 &&
157
+ this.hdPath[this.hdPath.length - 1] === '1') {
158
+ return this.activeIndexes.map((index) => {
159
+ const child = this.root.derive(`m/${index}`);
160
+ return child.publicKey.toString();
161
+ });
162
+ }
163
+ return this.activeIndexes.map((i) => this.getWalletByIndex(i).publicKey);
164
+ }
165
+ getAccounts2() {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ return this.activeIndexes.map((index) => {
168
+ const child = this.root.derive(`m/${index}`);
169
+ return {
170
+ index,
171
+ path: `${this.hdPath}/${index}`,
172
+ publicKey: child.publicKey.toString()
173
+ };
174
+ });
175
+ });
176
+ }
177
+ getAccountsWithBrand() {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ return this.activeIndexes.map((i) => {
180
+ const w = this.getWalletByIndex(i);
181
+ return {
182
+ address: w.publicKey,
183
+ index: i
184
+ };
185
+ });
186
+ });
187
+ }
188
+ getWalletByIndex(index) {
189
+ const child = this.root.derive(`m/0/${index}`);
190
+ return {
191
+ index,
192
+ path: `${this.hdPath}/${index}`,
193
+ publicKey: child.publicKey.toString()
194
+ };
195
+ }
196
+ getChangeAddressWalletByIndex(index) {
197
+ const child = this.root.derive(`m/1/${index}`);
198
+ return {
199
+ index,
200
+ path: `${this.hdPath}/${index}`,
201
+ publicKey: child.publicKey.toString()
202
+ };
203
+ }
204
+ removeAccount(publicKey) {
205
+ const index = this.activeIndexes.findIndex((i) => {
206
+ const w = this.getWalletByIndex(i);
207
+ return w.publicKey === publicKey;
208
+ });
209
+ if (index !== -1) {
210
+ this.activeIndexes.splice(index, 1);
211
+ }
212
+ }
213
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
214
+ exportAccount(_publicKey) {
215
+ throw new Error('Not supported');
216
+ }
217
+ getFirstPage() {
218
+ this.page = 0;
219
+ return this.getPage(1);
220
+ }
221
+ getNextPage() {
222
+ return this.getPage(1);
223
+ }
224
+ getPreviousPage() {
225
+ return this.getPage(-1);
226
+ }
227
+ getAddresses(start, end) {
228
+ const from = start;
229
+ const to = end;
230
+ const accounts = [];
231
+ for (let i = from; i < to; i++) {
232
+ const w = this.getWalletByIndex(i);
233
+ accounts.push({
234
+ address: w.publicKey,
235
+ index: i + 1
236
+ });
237
+ }
238
+ return accounts;
239
+ }
240
+ getPage(increment) {
241
+ return __awaiter(this, void 0, void 0, function* () {
242
+ this.page += increment;
243
+ if (!this.page || this.page <= 0) {
244
+ this.page = 1;
245
+ }
246
+ const from = (this.page - 1) * this.perPage;
247
+ const to = from + this.perPage;
248
+ const accounts = [];
249
+ for (let i = from; i < to; i++) {
250
+ const w = this.getWalletByIndex(i);
251
+ accounts.push({
252
+ address: w.publicKey,
253
+ index: i + 1
254
+ });
255
+ }
256
+ return accounts;
257
+ });
258
+ }
259
+ activeAccounts(indexes) {
260
+ const accounts = [];
261
+ for (const index of indexes) {
262
+ const w = this.getWalletByIndex(index);
263
+ if (!this.activeIndexes.includes(index)) {
264
+ this.activeIndexes.push(index);
265
+ }
266
+ accounts.push(w.publicKey);
267
+ }
268
+ return accounts;
269
+ }
270
+ changeHdPath(hdPath) {
271
+ this.hdPath = hdPath;
272
+ this.initRoot();
273
+ this.activeAccounts(this.activeIndexes);
274
+ }
275
+ changeChangeAddressHdPath(hdPath) {
276
+ var _a;
277
+ this.hdPath = hdPath;
278
+ this.root = this.getHDPublicKey((_a = this.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath());
279
+ this.root = this.root.derive(`m/1`);
280
+ this.activeIndexes = [];
281
+ return [];
282
+ }
283
+ getAccountByHdPath(hdPath, index) {
284
+ const root = this.getHDPublicKey(hdPath);
285
+ const child = root.derive(`m/0/${index}`);
286
+ return child.publicKey.toString();
287
+ }
288
+ getChangeAddressAccountByHdPath(hdPath, index) {
289
+ const root = this.getHDPublicKey(hdPath);
290
+ const child = root.derive(`m/1/${index}`);
291
+ return child.publicKey.toString();
292
+ }
293
+ genSignPsbtUr(psbtHex) {
294
+ return __awaiter(this, void 0, void 0, function* () {
295
+ const psbt = bitcoinjs_lib_1.Psbt.fromHex(psbtHex);
296
+ const keystoneSDK = new keystone_sdk_1.default({
297
+ origin: this.origin
298
+ });
299
+ const ur = keystoneSDK.btc.generatePSBT(psbt.data.toBuffer());
300
+ return {
301
+ type: ur.type,
302
+ cbor: ur.cbor.toString('hex')
303
+ };
304
+ });
305
+ }
306
+ parseSignPsbtUr(type, cbor) {
307
+ return __awaiter(this, void 0, void 0, function* () {
308
+ const keystoneSDK = new keystone_sdk_1.default({
309
+ origin: this.origin
310
+ });
311
+ return keystoneSDK.btc.parsePSBT(new keystone_sdk_1.UR(Buffer.from(cbor, 'hex'), type));
312
+ });
313
+ }
314
+ genSignMsgUr(publicKey, text) {
315
+ return __awaiter(this, void 0, void 0, function* () {
316
+ const keystoneSDK = new keystone_sdk_1.default({
317
+ origin: this.origin
318
+ });
319
+ let i = undefined;
320
+ if (this.hdPath !== null &&
321
+ this.hdPath !== undefined &&
322
+ this.hdPath.length >= 13 &&
323
+ this.hdPath[this.hdPath.length - 1] === '1') {
324
+ const root = this.getHDPublicKey(this.hdPath);
325
+ i = this.activeIndexes.find((i) => {
326
+ const child = root.derive(`m/1/${i}`);
327
+ if (child.publicKey.toString() === publicKey) {
328
+ return true;
329
+ }
330
+ });
331
+ }
332
+ else {
333
+ i = this.activeIndexes.find((i) => this.getWalletByIndex(i).publicKey === publicKey);
334
+ }
335
+ if (i === undefined) {
336
+ throw new Error('publicKey not found');
337
+ }
338
+ const requestId = utils_1.uuid.v4();
339
+ const ur = keystoneSDK.btc.generateSignRequest({
340
+ requestId,
341
+ signData: Buffer.from(text).toString('hex'),
342
+ dataType: keystone_sdk_1.KeystoneBitcoinSDK.DataType.message,
343
+ accounts: [
344
+ {
345
+ path: `${this.hdPath}/${i}`,
346
+ xfp: this.mfp
347
+ }
348
+ ],
349
+ origin: this.origin
350
+ });
351
+ return {
352
+ requestId,
353
+ type: ur.type,
354
+ cbor: ur.cbor.toString('hex')
355
+ };
356
+ });
357
+ }
358
+ parseSignMsgUr(type, cbor) {
359
+ return __awaiter(this, void 0, void 0, function* () {
360
+ const keystoneSDK = new keystone_sdk_1.default({
361
+ origin: this.origin
362
+ });
363
+ return keystoneSDK.btc.parseSignature(new keystone_sdk_1.UR(Buffer.from(cbor, 'hex'), type));
364
+ });
365
+ }
366
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
367
+ signMessage(publicKey, text) {
368
+ return 'Signing Message with Keystone should use genSignMsgUr and parseSignMsgUr';
369
+ }
370
+ verifyMessage(publicKey, text, sig) {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ return (0, message_1.verifyMessageOfECDSA)(publicKey, text, sig);
373
+ });
374
+ }
375
+ }
376
+ exports.KeystoneKeyring = KeystoneKeyring;
377
+ KeystoneKeyring.type = type;
@@ -0,0 +1,11 @@
1
+ import { IKeyringBase, SimpleKeyringOptions } from './interfaces/SimpleKeyringOptions';
2
+ export declare class SimpleKeyring extends IKeyringBase<SimpleKeyringOptions> {
3
+ static type: string;
4
+ type: string;
5
+ constructor(opts?: SimpleKeyringOptions);
6
+ serialize(): SimpleKeyringOptions;
7
+ deserialize(opts: SimpleKeyringOptions): void;
8
+ addAccounts(n?: number): string[];
9
+ getAccounts(): string[];
10
+ }
11
+ export declare function verifySignData(publicKey: string, hash: string, type: 'ecdsa' | 'schnorr', signature: string): boolean;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.verifySignData = exports.SimpleKeyring = void 0;
4
+ const bs58check_1 = require("bs58check");
5
+ const bitcoin_core_1 = require("../bitcoin-core");
6
+ const SimpleKeyringOptions_1 = require("./interfaces/SimpleKeyringOptions");
7
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
8
+ const type = 'Simple Key Pair';
9
+ class SimpleKeyring extends SimpleKeyringOptions_1.IKeyringBase {
10
+ constructor(opts) {
11
+ super((opts === null || opts === void 0 ? void 0 : opts.network) || bitcoinjs_lib_1.networks.bitcoin);
12
+ this.type = type;
13
+ if (opts && opts.privateKeys) {
14
+ this.deserialize(opts);
15
+ }
16
+ }
17
+ serialize() {
18
+ return {
19
+ privateKeys: this.wallets.map((wallet) => wallet.privateKey.toString('hex')),
20
+ network: this.network
21
+ };
22
+ }
23
+ deserialize(opts) {
24
+ if (Array.isArray(opts)) {
25
+ opts = { privateKeys: opts }; // compatibility
26
+ }
27
+ this.wallets = opts.privateKeys.map((key) => {
28
+ let buf;
29
+ if (key.length === 64) {
30
+ // privateKey
31
+ buf = Buffer.from(key, 'hex');
32
+ }
33
+ else {
34
+ // base58
35
+ buf = Buffer.from((0, bs58check_1.decode)(key).slice(1, 33));
36
+ }
37
+ return bitcoin_core_1.ECPair.fromPrivateKey(buf);
38
+ });
39
+ }
40
+ addAccounts(n = 1) {
41
+ const newWallets = [];
42
+ for (let i = 0; i < n; i++) {
43
+ newWallets.push(bitcoin_core_1.ECPair.makeRandom());
44
+ }
45
+ this.wallets = this.wallets.concat(newWallets);
46
+ return newWallets.map(({ publicKey }) => publicKey.toString('hex'));
47
+ }
48
+ getAccounts() {
49
+ return this.wallets.map(({ publicKey }) => publicKey.toString('hex'));
50
+ }
51
+ }
52
+ exports.SimpleKeyring = SimpleKeyring;
53
+ SimpleKeyring.type = type;
54
+ function verifySignData(publicKey, hash, type, signature) {
55
+ const keyPair = bitcoin_core_1.ECPair.fromPublicKey(Buffer.from(publicKey, 'hex'));
56
+ if (type === 'ecdsa') {
57
+ return keyPair.verify(Buffer.from(hash, 'hex'), Buffer.from(signature, 'hex'));
58
+ }
59
+ else if (type === 'schnorr') {
60
+ return keyPair.verifySchnorr(Buffer.from(hash, 'hex'), Buffer.from(signature, 'hex'));
61
+ }
62
+ else {
63
+ throw new Error('Not support type');
64
+ }
65
+ }
66
+ exports.verifySignData = verifySignData;
@@ -0,0 +1,19 @@
1
+ import { NetworkType } from '../network';
2
+ import { AbstractWallet } from '../wallet';
3
+ import * as bitcoin from 'bitcoinjs-lib';
4
+ export declare function genPsbtOfBIP322Simple({ message, address, networkType }: {
5
+ message: string;
6
+ address: string;
7
+ networkType: NetworkType;
8
+ }): bitcoin.Psbt;
9
+ export declare function getSignatureFromPsbtOfBIP322Simple(psbt: bitcoin.Psbt): string;
10
+ /**
11
+ * reference: https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki
12
+ */
13
+ export declare function signMessageOfBIP322Simple({ message, address, networkType, wallet }: {
14
+ message: string;
15
+ address: string;
16
+ networkType: NetworkType;
17
+ wallet: AbstractWallet;
18
+ }): Promise<string>;
19
+ export declare function verifyMessageOfBIP322Simple(address: string, msg: string, signature: string, networkType?: NetworkType): boolean;
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.verifyMessageOfBIP322Simple = exports.signMessageOfBIP322Simple = exports.getSignatureFromPsbtOfBIP322Simple = exports.genPsbtOfBIP322Simple = void 0;
36
+ const varuint_bitcoin_1 = require("varuint-bitcoin");
37
+ const address_1 = require("../address");
38
+ const network_1 = require("../network");
39
+ const types_1 = require("../types");
40
+ const utils_1 = require("../utils");
41
+ const bitcoin = __importStar(require("bitcoinjs-lib"));
42
+ function bip0322_hash(message) {
43
+ const { sha256 } = bitcoin.crypto;
44
+ const tag = 'BIP0322-signed-message';
45
+ const tagHash = sha256(Buffer.from(tag));
46
+ const result = sha256(Buffer.concat([tagHash, tagHash, Buffer.from(message)]));
47
+ return result.toString('hex');
48
+ }
49
+ function genPsbtOfBIP322Simple({ message, address, networkType }) {
50
+ const outputScript = (0, address_1.addressToScriptPk)(address, networkType);
51
+ const addressType = (0, address_1.getAddressType)(address, networkType);
52
+ const supportedTypes = [types_1.AddressType.P2WPKH, types_1.AddressType.P2TR, types_1.AddressType.M44_P2WPKH, types_1.AddressType.M44_P2TR];
53
+ if (supportedTypes.includes(addressType) == false) {
54
+ throw new Error('Not support address type to sign');
55
+ }
56
+ const prevoutHash = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
57
+ const prevoutIndex = 0xffffffff;
58
+ const sequence = 0;
59
+ const scriptSig = Buffer.concat([Buffer.from('0020', 'hex'), Buffer.from(bip0322_hash(message), 'hex')]);
60
+ const txToSpend = new bitcoin.Transaction();
61
+ txToSpend.version = 0;
62
+ txToSpend.addInput(prevoutHash, prevoutIndex, sequence, scriptSig);
63
+ txToSpend.addOutput(outputScript, 0);
64
+ const psbtToSign = new bitcoin.Psbt();
65
+ psbtToSign.setVersion(0);
66
+ psbtToSign.addInput({
67
+ hash: txToSpend.getHash(),
68
+ index: 0,
69
+ sequence: 0,
70
+ witnessUtxo: {
71
+ script: outputScript,
72
+ value: 0
73
+ }
74
+ });
75
+ psbtToSign.addOutput({ script: Buffer.from('6a', 'hex'), value: 0 });
76
+ return psbtToSign;
77
+ }
78
+ exports.genPsbtOfBIP322Simple = genPsbtOfBIP322Simple;
79
+ function getSignatureFromPsbtOfBIP322Simple(psbt) {
80
+ const txToSign = psbt.extractTransaction();
81
+ function encodeVarString(b) {
82
+ return Buffer.concat([(0, varuint_bitcoin_1.encode)(b.byteLength), b]);
83
+ }
84
+ const len = (0, varuint_bitcoin_1.encode)(txToSign.ins[0].witness.length);
85
+ const result = Buffer.concat([len, ...txToSign.ins[0].witness.map((w) => encodeVarString(w))]);
86
+ const signature = result.toString('base64');
87
+ return signature;
88
+ }
89
+ exports.getSignatureFromPsbtOfBIP322Simple = getSignatureFromPsbtOfBIP322Simple;
90
+ /**
91
+ * reference: https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki
92
+ */
93
+ function signMessageOfBIP322Simple({ message, address, networkType, wallet }) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const psbtToSign = genPsbtOfBIP322Simple({
96
+ message,
97
+ address,
98
+ networkType
99
+ });
100
+ yield wallet.signPsbt(psbtToSign);
101
+ return getSignatureFromPsbtOfBIP322Simple(psbtToSign);
102
+ });
103
+ }
104
+ exports.signMessageOfBIP322Simple = signMessageOfBIP322Simple;
105
+ function verifyMessageOfBIP322Simple(address, msg, signature, networkType = network_1.NetworkType.MAINNET) {
106
+ const addressType = (0, address_1.getAddressType)(address, networkType);
107
+ if (addressType === types_1.AddressType.P2WPKH || addressType === types_1.AddressType.M44_P2WPKH) {
108
+ return verifySignatureOfBIP322Simple_P2PWPKH(address, msg, signature, networkType);
109
+ }
110
+ else if (addressType === types_1.AddressType.P2TR || addressType === types_1.AddressType.M44_P2TR) {
111
+ return verifySignatureOfBIP322Simple_P2TR(address, msg, signature, networkType);
112
+ }
113
+ return false;
114
+ }
115
+ exports.verifyMessageOfBIP322Simple = verifyMessageOfBIP322Simple;
116
+ function verifySignatureOfBIP322Simple_P2TR(address, msg, sign, networkType = network_1.NetworkType.MAINNET) {
117
+ const network = (0, network_1.toPsbtNetwork)(networkType);
118
+ const outputScript = bitcoin.address.toOutputScript(address, network);
119
+ const prevoutHash = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
120
+ const prevoutIndex = 0xffffffff;
121
+ const sequence = 0;
122
+ const scriptSig = Buffer.concat([Buffer.from('0020', 'hex'), Buffer.from(bip0322_hash(msg), 'hex')]);
123
+ const txToSpend = new bitcoin.Transaction();
124
+ txToSpend.version = 0;
125
+ txToSpend.addInput(prevoutHash, prevoutIndex, sequence, scriptSig);
126
+ txToSpend.addOutput(outputScript, 0);
127
+ const data = Buffer.from(sign, 'base64');
128
+ const _res = bitcoin.script.decompile(data.slice(1));
129
+ const signature = _res[0];
130
+ const pubkey = Buffer.from('02' + outputScript.subarray(2).toString('hex'), 'hex');
131
+ const psbtToSign = new bitcoin.Psbt();
132
+ psbtToSign.setVersion(0);
133
+ psbtToSign.addInput({
134
+ hash: txToSpend.getHash(),
135
+ index: 0,
136
+ sequence: 0,
137
+ witnessUtxo: {
138
+ script: outputScript,
139
+ value: 0
140
+ }
141
+ });
142
+ psbtToSign.addOutput({ script: Buffer.from('6a', 'hex'), value: 0 });
143
+ const tapKeyHash = psbtToSign.__CACHE.__TX.hashForWitnessV1(0, [outputScript], [0], 0);
144
+ const valid = (0, utils_1.schnorrValidator)(pubkey, tapKeyHash, signature);
145
+ return valid;
146
+ }
147
+ function verifySignatureOfBIP322Simple_P2PWPKH(address, msg, sign, networkType = network_1.NetworkType.MAINNET) {
148
+ const network = (0, network_1.toPsbtNetwork)(networkType);
149
+ const outputScript = bitcoin.address.toOutputScript(address, network);
150
+ const prevoutHash = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
151
+ const prevoutIndex = 0xffffffff;
152
+ const sequence = 0;
153
+ const scriptSig = Buffer.concat([Buffer.from('0020', 'hex'), Buffer.from(bip0322_hash(msg), 'hex')]);
154
+ const txToSpend = new bitcoin.Transaction();
155
+ txToSpend.version = 0;
156
+ txToSpend.addInput(prevoutHash, prevoutIndex, sequence, scriptSig);
157
+ txToSpend.addOutput(outputScript, 0);
158
+ const data = Buffer.from(sign, 'base64');
159
+ const _res = bitcoin.script.decompile(data.slice(1));
160
+ const psbtToSign = new bitcoin.Psbt();
161
+ psbtToSign.setVersion(0);
162
+ psbtToSign.addInput({
163
+ hash: txToSpend.getHash(),
164
+ index: 0,
165
+ sequence: 0,
166
+ witnessUtxo: {
167
+ script: outputScript,
168
+ value: 0
169
+ }
170
+ });
171
+ psbtToSign.addOutput({ script: Buffer.from('6a', 'hex'), value: 0 });
172
+ psbtToSign.updateInput(0, {
173
+ partialSig: [
174
+ {
175
+ pubkey: _res[1],
176
+ signature: _res[0]
177
+ }
178
+ ]
179
+ });
180
+ const valid = psbtToSign.validateSignaturesOfAllInputs(utils_1.validator);
181
+ return valid;
182
+ }
@@ -0,0 +1,2 @@
1
+ import { ECPairInterface } from '../bitcoin-core';
2
+ export declare function signMessageOfDeterministicECDSA(ecpair: ECPairInterface, message: string): string;