@btc-vision/wallet-sdk 1.0.0 → 1.0.4

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 (129) hide show
  1. package/es/bundle.js +1 -1
  2. package/es/bundle.js.LICENSE.txt +4 -4
  3. package/lib/address/index.js +50 -85
  4. package/lib/bitcoin-core.js +7 -37
  5. package/lib/constants.js +1 -4
  6. package/lib/error.js +6 -10
  7. package/lib/index.js +14 -43
  8. package/lib/keyring/hd-keyring.js +40 -79
  9. package/lib/keyring/index.js +4 -20
  10. package/lib/keyring/interfaces/SimpleKeyringOptions.js +16 -31
  11. package/lib/keyring/keystone-keyring.js +142 -201
  12. package/lib/keyring/simple-keyring.js +13 -18
  13. package/lib/message/bip322-simple.js +31 -71
  14. package/lib/message/deterministic-ecdsa.js +19 -40
  15. package/lib/message/ecdsa.js +10 -18
  16. package/lib/message/index.js +3 -19
  17. package/lib/network/index.js +12 -17
  18. package/lib/runes/index.js +1 -17
  19. package/lib/runes/rund_id.js +8 -13
  20. package/lib/runes/varint.js +6 -12
  21. package/lib/transaction/index.js +3 -19
  22. package/lib/transaction/inscription-utxo.js +45 -38
  23. package/lib/transaction/transaction.js +134 -139
  24. package/lib/transaction/utxo.js +13 -18
  25. package/lib/tx-helpers/index.js +8 -18
  26. package/lib/tx-helpers/send-atomicals-ft.js +45 -60
  27. package/lib/tx-helpers/send-atomicals-nft.js +29 -44
  28. package/lib/tx-helpers/send-btc.js +53 -71
  29. package/lib/tx-helpers/send-inscription.js +32 -47
  30. package/lib/tx-helpers/send-inscriptions.js +28 -43
  31. package/lib/tx-helpers/send-runes.js +83 -97
  32. package/lib/tx-helpers/split-inscription-utxo.js +39 -54
  33. package/lib/types.js +2 -5
  34. package/lib/utils.js +16 -29
  35. package/lib/wallet/abstract-wallet.js +1 -2
  36. package/lib/wallet/estimate-wallet.js +130 -147
  37. package/lib/wallet/index.js +3 -19
  38. package/lib/wallet/local-wallet.js +134 -150
  39. package/package.json +103 -95
  40. package/lib/address/index.d.ts +0 -37
  41. package/lib/bitcoin-core.d.ts +0 -5
  42. package/lib/constants.d.ts +0 -1
  43. package/lib/error.d.ts +0 -20
  44. package/lib/index.d.ts +0 -14
  45. package/lib/keyring/hd-keyring.d.ts +0 -50
  46. package/lib/keyring/index.d.ts +0 -4
  47. package/lib/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -53
  48. package/lib/keyring/keystone-keyring.d.ts +0 -82
  49. package/lib/keyring/simple-keyring.d.ts +0 -11
  50. package/lib/message/bip322-simple.d.ts +0 -19
  51. package/lib/message/deterministic-ecdsa.d.ts +0 -2
  52. package/lib/message/ecdsa.d.ts +0 -3
  53. package/lib/message/index.d.ts +0 -3
  54. package/lib/network/index.d.ts +0 -14
  55. package/lib/runes/index.d.ts +0 -1
  56. package/lib/runes/rund_id.d.ts +0 -11
  57. package/lib/runes/varint.d.ts +0 -15
  58. package/lib/src/address/index.d.ts +0 -37
  59. package/lib/src/bitcoin-core.d.ts +0 -5
  60. package/lib/src/constants.d.ts +0 -1
  61. package/lib/src/error.d.ts +0 -20
  62. package/lib/src/index.d.ts +0 -14
  63. package/lib/src/keyring/hd-keyring.d.ts +0 -50
  64. package/lib/src/keyring/index.d.ts +0 -4
  65. package/lib/src/keyring/interfaces/SimpleKeyringOptions.d.ts +0 -53
  66. package/lib/src/keyring/keystone-keyring.d.ts +0 -82
  67. package/lib/src/keyring/simple-keyring.d.ts +0 -11
  68. package/lib/src/message/bip322-simple.d.ts +0 -19
  69. package/lib/src/message/deterministic-ecdsa.d.ts +0 -2
  70. package/lib/src/message/ecdsa.d.ts +0 -3
  71. package/lib/src/message/index.d.ts +0 -3
  72. package/lib/src/network/index.d.ts +0 -14
  73. package/lib/src/runes/index.d.ts +0 -1
  74. package/lib/src/runes/rund_id.d.ts +0 -11
  75. package/lib/src/runes/varint.d.ts +0 -15
  76. package/lib/src/transaction/index.d.ts +0 -3
  77. package/lib/src/transaction/inscription-utxo.d.ts +0 -33
  78. package/lib/src/transaction/transaction.d.ts +0 -52
  79. package/lib/src/transaction/utxo.d.ts +0 -35
  80. package/lib/src/tx-helpers/index.d.ts +0 -8
  81. package/lib/src/tx-helpers/send-atomicals-ft.d.ts +0 -16
  82. package/lib/src/tx-helpers/send-atomicals-nft.d.ts +0 -14
  83. package/lib/src/tx-helpers/send-btc.d.ts +0 -28
  84. package/lib/src/tx-helpers/send-inscription.d.ts +0 -16
  85. package/lib/src/tx-helpers/send-inscriptions.d.ts +0 -14
  86. package/lib/src/tx-helpers/send-runes.d.ts +0 -19
  87. package/lib/src/tx-helpers/split-inscription-utxo.d.ts +0 -15
  88. package/lib/src/types.d.ts +0 -59
  89. package/lib/src/utils.d.ts +0 -23
  90. package/lib/src/wallet/abstract-wallet.d.ts +0 -6
  91. package/lib/src/wallet/estimate-wallet.d.ts +0 -23
  92. package/lib/src/wallet/index.d.ts +0 -3
  93. package/lib/src/wallet/local-wallet.d.ts +0 -23
  94. package/lib/test/address/address.test.d.ts +0 -1
  95. package/lib/test/keyring/hd-keyring.test.d.ts +0 -1
  96. package/lib/test/keyring/keystone-keyring.test.d.ts +0 -1
  97. package/lib/test/keyring/simple-keyring.test.d.ts +0 -1
  98. package/lib/test/message/message.test.d.ts +0 -1
  99. package/lib/test/runes/varint.test.d.ts +0 -1
  100. package/lib/test/transaction/transaction.test.d.ts +0 -1
  101. package/lib/test/transaction/utxo.test.d.ts +0 -1
  102. package/lib/test/tx-helpers/send-atomicals-ft.test.d.ts +0 -1
  103. package/lib/test/tx-helpers/send-atomicals-nft.test.d.ts +0 -1
  104. package/lib/test/tx-helpers/send-btc.test.d.ts +0 -1
  105. package/lib/test/tx-helpers/send-inscription.test.d.ts +0 -1
  106. package/lib/test/tx-helpers/send-inscriptions.test.d.ts +0 -1
  107. package/lib/test/tx-helpers/send-runes.test.d.ts +0 -1
  108. package/lib/test/tx-helpers/split-inscription-utxo.test.d.ts +0 -1
  109. package/lib/test/tx-helpers/utils.d.ts +0 -217
  110. package/lib/test/utils.d.ts +0 -4
  111. package/lib/test/wallet/local-wallet.test.d.ts +0 -1
  112. package/lib/transaction/index.d.ts +0 -3
  113. package/lib/transaction/inscription-utxo.d.ts +0 -33
  114. package/lib/transaction/transaction.d.ts +0 -52
  115. package/lib/transaction/utxo.d.ts +0 -35
  116. package/lib/tx-helpers/index.d.ts +0 -8
  117. package/lib/tx-helpers/send-atomicals-ft.d.ts +0 -16
  118. package/lib/tx-helpers/send-atomicals-nft.d.ts +0 -14
  119. package/lib/tx-helpers/send-btc.d.ts +0 -28
  120. package/lib/tx-helpers/send-inscription.d.ts +0 -16
  121. package/lib/tx-helpers/send-inscriptions.d.ts +0 -14
  122. package/lib/tx-helpers/send-runes.d.ts +0 -19
  123. package/lib/tx-helpers/split-inscription-utxo.d.ts +0 -15
  124. package/lib/types.d.ts +0 -59
  125. package/lib/utils.d.ts +0 -23
  126. package/lib/wallet/abstract-wallet.d.ts +0 -6
  127. package/lib/wallet/estimate-wallet.d.ts +0 -23
  128. package/lib/wallet/index.d.ts +0 -3
  129. package/lib/wallet/local-wallet.d.ts +0 -23
@@ -1,26 +1,15 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.IKeyringBase = void 0;
13
- const bitcoinjs_lib_1 = require("bitcoinjs-lib");
14
- const events_1 = require("events");
15
- const bip371_1 = require("bitcoinjs-lib/src/psbt/bip371");
16
- const utils_1 = require("../../utils");
17
- const message_1 = require("../../message");
18
- class IKeyringBase extends events_1.EventEmitter {
19
- constructor(network = bitcoinjs_lib_1.networks.bitcoin) {
1
+ import { isTaprootInput, networks } from '@btc-vision/bitcoin';
2
+ import { EventEmitter } from 'events';
3
+ import { signMessageOfDeterministicECDSA, verifyMessageOfECDSA } from '../../message';
4
+ import { tweakSigner } from '../../utils';
5
+ export class IKeyringBase extends EventEmitter {
6
+ network;
7
+ static type = '';
8
+ type = '';
9
+ wallets = [];
10
+ constructor(network = networks.bitcoin) {
20
11
  super();
21
12
  this.network = network;
22
- this.type = '';
23
- this.wallets = [];
24
13
  }
25
14
  removeAccount(publicKey) {
26
15
  if (!this.wallets.map((wallet) => wallet.publicKey.toString('hex')).includes(publicKey)) {
@@ -28,10 +17,8 @@ class IKeyringBase extends events_1.EventEmitter {
28
17
  }
29
18
  this.wallets = this.wallets.filter((wallet) => wallet.publicKey.toString('hex') !== publicKey);
30
19
  }
31
- verifyMessage(publicKey, text, sig) {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- return (0, message_1.verifyMessageOfECDSA)(publicKey, text, sig);
34
- });
20
+ async verifyMessage(publicKey, text, sig) {
21
+ return verifyMessageOfECDSA(publicKey, text, sig);
35
22
  }
36
23
  // Sign any content, but note that the content signed by this method is unreadable, so use it with caution.
37
24
  signData(publicKey, data, type = 'ecdsa') {
@@ -48,17 +35,17 @@ class IKeyringBase extends events_1.EventEmitter {
48
35
  }
49
36
  signMessage(publicKey, text) {
50
37
  const keyPair = this._getPrivateKeyFor(publicKey);
51
- return (0, message_1.signMessageOfDeterministicECDSA)(keyPair, text);
38
+ return signMessageOfDeterministicECDSA(keyPair, text);
52
39
  }
53
40
  exportAccount(publicKey) {
54
41
  const wallet = this._getWalletForAccount(publicKey);
55
- return wallet.privateKey.toString('hex');
42
+ return wallet.privateKey?.toString('hex');
56
43
  }
57
44
  signTransaction(psbt, inputs, opts) {
58
45
  inputs.forEach((input) => {
59
46
  const keyPair = this._getPrivateKeyFor(input.publicKey);
60
- if ((0, bip371_1.isTaprootInput)(psbt.data.inputs[input.index]) && !input.disableTweakSigner) {
61
- const signer = (0, utils_1.tweakSigner)(keyPair, opts);
47
+ if (isTaprootInput(psbt.data.inputs[input.index]) && !input.disableTweakSigner) {
48
+ const signer = tweakSigner(keyPair, opts);
62
49
  psbt.signInput(input.index, signer, input.sighashTypes);
63
50
  }
64
51
  else {
@@ -81,5 +68,3 @@ class IKeyringBase extends events_1.EventEmitter {
81
68
  return this._getWalletForAccount(publicKey);
82
69
  }
83
70
  }
84
- exports.IKeyringBase = IKeyringBase;
85
- IKeyringBase.type = '';
@@ -1,76 +1,40 @@
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");
1
+ import { networks, Psbt } from '@btc-vision/bitcoin';
2
+ import KeystoneSDK, { KeystoneBitcoinSDK, UR } from '@keystonehq/keystone-sdk';
3
+ import { uuid } from '@keystonehq/keystone-sdk/dist/utils';
4
+ import bitcore from 'bitcore-lib';
5
+ import { verifyMessageOfECDSA } from '../message';
6
+ import { IKeyringBase } from './interfaces/SimpleKeyringOptions';
45
7
  const type = 'Keystone';
46
- class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
8
+ export class KeystoneKeyring extends IKeyringBase {
9
+ static type = type;
10
+ type = type;
11
+ mfp = '';
12
+ keys = [];
13
+ hdPath;
14
+ activeIndexes = [];
15
+ root = undefined;
16
+ page = 0;
17
+ perPage = 5;
18
+ origin = 'UniSat Wallet';
47
19
  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';
20
+ super(opts?.network || networks.bitcoin);
57
21
  if (opts) {
58
22
  this.deserialize(opts);
59
23
  }
60
24
  }
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
- });
25
+ async initFromUR(type, cbor) {
26
+ const keystoneSDK = new KeystoneSDK({
27
+ origin: this.origin
28
+ });
29
+ const account = keystoneSDK.parseAccount(new UR(Buffer.from(cbor, 'hex'), type));
30
+ this.deserialize({
31
+ mfp: account.masterFingerprint,
32
+ keys: account.keys
33
+ .filter((k) => k.extendedPublicKey !== undefined)
34
+ .map((k) => ({
35
+ path: k.path,
36
+ extendedPublicKey: k.extendedPublicKey
37
+ }))
74
38
  });
75
39
  }
76
40
  getHardenedPath(hdPath) {
@@ -83,27 +47,25 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
83
47
  if (!key) {
84
48
  throw new Error('Invalid path');
85
49
  }
86
- return new bitcore_lib_1.default.HDPublicKey(key.extendedPublicKey);
50
+ return new bitcore.HDPublicKey(key.extendedPublicKey);
87
51
  }
88
52
  getDefaultHdPath() {
89
53
  return "m/44'/0'/0'/0";
90
54
  }
91
55
  initRoot() {
92
- var _a;
93
- this.root = this.getHDPublicKey((_a = this.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath());
56
+ this.root = this.getHDPublicKey(this.hdPath ?? this.getDefaultHdPath());
94
57
  }
95
58
  deserialize(opts) {
96
- var _a;
97
59
  this.mfp = opts.mfp;
98
60
  this.keys = opts.keys;
99
- this.hdPath = (_a = opts.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath();
61
+ this.hdPath = opts.hdPath ?? this.getDefaultHdPath();
100
62
  this.activeIndexes = opts.activeIndexes ? [...opts.activeIndexes] : [];
101
63
  this.initRoot();
102
64
  if (opts.hdPath !== null &&
103
65
  opts.hdPath !== undefined &&
104
66
  opts.hdPath.length >= 13 &&
105
67
  opts.hdPath[opts.hdPath.length - 1] === '1') {
106
- this.root = this.root.derive(`m/1`);
68
+ this.root = this.root?.derive(`m/1`);
107
69
  }
108
70
  }
109
71
  serialize() {
@@ -119,36 +81,34 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
119
81
  let i = 0;
120
82
  const pubkeys = [];
121
83
  while (count) {
122
- if (this.activeIndexes.includes(i)) {
84
+ if (this.activeIndexes?.includes(i)) {
123
85
  i++;
124
86
  }
125
87
  else {
126
88
  const w = this.getWalletByIndex(i);
127
89
  pubkeys.push(w.publicKey);
128
- this.activeIndexes.push(i);
90
+ this.activeIndexes?.push(i);
129
91
  count--;
130
92
  }
131
93
  }
132
94
  return pubkeys;
133
95
  }
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
- }
96
+ async addChangeAddressAccounts(numberOfAccounts = 1) {
97
+ let count = numberOfAccounts;
98
+ let i = 0;
99
+ const pubkeys = [];
100
+ while (count) {
101
+ if (this.activeIndexes?.includes(i)) {
102
+ i++;
149
103
  }
150
- return Promise.resolve(pubkeys);
151
- });
104
+ else {
105
+ const w = this.getChangeAddressWalletByIndex(i);
106
+ pubkeys.push(w.publicKey);
107
+ this.activeIndexes?.push(i);
108
+ count--;
109
+ }
110
+ }
111
+ return Promise.resolve(pubkeys);
152
112
  }
153
113
  getAccounts() {
154
114
  if (this.hdPath !== null &&
@@ -162,27 +122,23 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
162
122
  }
163
123
  return this.activeIndexes.map((i) => this.getWalletByIndex(i).publicKey);
164
124
  }
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
- });
125
+ async getAccounts2() {
126
+ return this.activeIndexes.map((index) => {
127
+ const child = this.root.derive(`m/${index}`);
128
+ return {
129
+ index,
130
+ path: `${this.hdPath}/${index}`,
131
+ publicKey: child.publicKey.toString()
132
+ };
175
133
  });
176
134
  }
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
- });
135
+ async getAccountsWithBrand() {
136
+ return this.activeIndexes.map((i) => {
137
+ const w = this.getWalletByIndex(i);
138
+ return {
139
+ address: w.publicKey,
140
+ index: i
141
+ };
186
142
  });
187
143
  }
188
144
  getWalletByIndex(index) {
@@ -237,24 +193,22 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
237
193
  }
238
194
  return accounts;
239
195
  }
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
- });
196
+ async getPage(increment) {
197
+ this.page += increment;
198
+ if (!this.page || this.page <= 0) {
199
+ this.page = 1;
200
+ }
201
+ const from = (this.page - 1) * this.perPage;
202
+ const to = from + this.perPage;
203
+ const accounts = [];
204
+ for (let i = from; i < to; i++) {
205
+ const w = this.getWalletByIndex(i);
206
+ accounts.push({
207
+ address: w.publicKey,
208
+ index: i + 1
209
+ });
210
+ }
211
+ return accounts;
258
212
  }
259
213
  activeAccounts(indexes) {
260
214
  const accounts = [];
@@ -273,9 +227,8 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
273
227
  this.activeAccounts(this.activeIndexes);
274
228
  }
275
229
  changeChangeAddressHdPath(hdPath) {
276
- var _a;
277
230
  this.hdPath = hdPath;
278
- this.root = this.getHDPublicKey((_a = this.hdPath) !== null && _a !== void 0 ? _a : this.getDefaultHdPath());
231
+ this.root = this.getHDPublicKey(this.hdPath ?? this.getDefaultHdPath());
279
232
  this.root = this.root.derive(`m/1`);
280
233
  this.activeIndexes = [];
281
234
  return [];
@@ -290,88 +243,76 @@ class KeystoneKeyring extends SimpleKeyringOptions_1.IKeyringBase {
290
243
  const child = root.derive(`m/1/${index}`);
291
244
  return child.publicKey.toString();
292
245
  }
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
- };
246
+ async genSignPsbtUr(psbtHex) {
247
+ const psbt = Psbt.fromHex(psbtHex);
248
+ const keystoneSDK = new KeystoneSDK({
249
+ origin: this.origin
304
250
  });
251
+ const ur = keystoneSDK.btc.generatePSBT(psbt.data.toBuffer());
252
+ return {
253
+ type: ur.type,
254
+ cbor: ur.cbor.toString('hex')
255
+ };
305
256
  }
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));
257
+ async parseSignPsbtUr(type, cbor) {
258
+ const keystoneSDK = new KeystoneSDK({
259
+ origin: this.origin
312
260
  });
261
+ return keystoneSDK.btc.parsePSBT(new UR(Buffer.from(cbor, 'hex'), type));
313
262
  }
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
263
+ async genSignMsgUr(publicKey, text) {
264
+ const keystoneSDK = new KeystoneSDK({
265
+ origin: this.origin
266
+ });
267
+ let i = undefined;
268
+ if (this.hdPath !== null &&
269
+ this.hdPath !== undefined &&
270
+ this.hdPath.length >= 13 &&
271
+ this.hdPath[this.hdPath.length - 1] === '1') {
272
+ const root = this.getHDPublicKey(this.hdPath);
273
+ i = this.activeIndexes.find((i) => {
274
+ const child = root.derive(`m/1/${i}`);
275
+ if (child.publicKey.toString() === publicKey) {
276
+ return true;
277
+ }
350
278
  });
351
- return {
352
- requestId,
353
- type: ur.type,
354
- cbor: ur.cbor.toString('hex')
355
- };
279
+ }
280
+ else {
281
+ i = this.activeIndexes.find((i) => this.getWalletByIndex(i).publicKey === publicKey);
282
+ }
283
+ if (i === undefined) {
284
+ throw new Error('publicKey not found');
285
+ }
286
+ const requestId = uuid.v4();
287
+ const ur = keystoneSDK.btc.generateSignRequest({
288
+ requestId,
289
+ signData: Buffer.from(text).toString('hex'),
290
+ dataType: KeystoneBitcoinSDK.DataType.message,
291
+ accounts: [
292
+ {
293
+ path: `${this.hdPath}/${i}`,
294
+ xfp: this.mfp
295
+ }
296
+ ],
297
+ origin: this.origin
356
298
  });
299
+ return {
300
+ requestId,
301
+ type: ur.type,
302
+ cbor: ur.cbor.toString('hex')
303
+ };
357
304
  }
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));
305
+ async parseSignMsgUr(type, cbor) {
306
+ const keystoneSDK = new KeystoneSDK({
307
+ origin: this.origin
364
308
  });
309
+ return keystoneSDK.btc.parseSignature(new UR(Buffer.from(cbor, 'hex'), type));
365
310
  }
366
311
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
367
312
  signMessage(publicKey, text) {
368
313
  return 'Signing Message with Keystone should use genSignMsgUr and parseSignMsgUr';
369
314
  }
370
- verifyMessage(publicKey, text, sig) {
371
- return __awaiter(this, void 0, void 0, function* () {
372
- return (0, message_1.verifyMessageOfECDSA)(publicKey, text, sig);
373
- });
315
+ async verifyMessage(publicKey, text, sig) {
316
+ return verifyMessageOfECDSA(publicKey, text, sig);
374
317
  }
375
318
  }
376
- exports.KeystoneKeyring = KeystoneKeyring;
377
- KeystoneKeyring.type = type;
@@ -1,15 +1,13 @@
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");
1
+ import { networks } from '@btc-vision/bitcoin';
2
+ import * as bs58check from 'bs58check';
3
+ import { ECPair } from '../bitcoin-core';
4
+ import { IKeyringBase } from './interfaces/SimpleKeyringOptions';
8
5
  const type = 'Simple Key Pair';
9
- class SimpleKeyring extends SimpleKeyringOptions_1.IKeyringBase {
6
+ export class SimpleKeyring extends IKeyringBase {
7
+ static type = type;
8
+ type = type;
10
9
  constructor(opts) {
11
- super((opts === null || opts === void 0 ? void 0 : opts.network) || bitcoinjs_lib_1.networks.bitcoin);
12
- this.type = type;
10
+ super(opts?.network || networks.bitcoin);
13
11
  if (opts && opts.privateKeys) {
14
12
  this.deserialize(opts);
15
13
  }
@@ -32,15 +30,15 @@ class SimpleKeyring extends SimpleKeyringOptions_1.IKeyringBase {
32
30
  }
33
31
  else {
34
32
  // base58
35
- buf = Buffer.from((0, bs58check_1.decode)(key).slice(1, 33));
33
+ buf = Buffer.from(bs58check.default.decode(key).slice(1, 33));
36
34
  }
37
- return bitcoin_core_1.ECPair.fromPrivateKey(buf);
35
+ return ECPair.fromPrivateKey(buf);
38
36
  });
39
37
  }
40
38
  addAccounts(n = 1) {
41
39
  const newWallets = [];
42
40
  for (let i = 0; i < n; i++) {
43
- newWallets.push(bitcoin_core_1.ECPair.makeRandom());
41
+ newWallets.push(ECPair.makeRandom());
44
42
  }
45
43
  this.wallets = this.wallets.concat(newWallets);
46
44
  return newWallets.map(({ publicKey }) => publicKey.toString('hex'));
@@ -49,10 +47,8 @@ class SimpleKeyring extends SimpleKeyringOptions_1.IKeyringBase {
49
47
  return this.wallets.map(({ publicKey }) => publicKey.toString('hex'));
50
48
  }
51
49
  }
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'));
50
+ export function verifySignData(publicKey, hash, type, signature) {
51
+ const keyPair = ECPair.fromPublicKey(Buffer.from(publicKey, 'hex'));
56
52
  if (type === 'ecdsa') {
57
53
  return keyPair.verify(Buffer.from(hash, 'hex'), Buffer.from(signature, 'hex'));
58
54
  }
@@ -63,4 +59,3 @@ function verifySignData(publicKey, hash, type, signature) {
63
59
  throw new Error('Not support type');
64
60
  }
65
61
  }
66
- exports.verifySignData = verifySignData;