@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,174 @@
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.EstimateWallet = void 0;
13
+ const address_1 = require("../address");
14
+ const bitcoin_core_1 = require("../bitcoin-core");
15
+ const keyring_1 = require("../keyring");
16
+ const message_1 = require("../message");
17
+ const network_1 = require("../network");
18
+ const types_1 = require("../types");
19
+ const utils_1 = require("../utils");
20
+ /**
21
+ * EstimateWallet is a wallet that can be used to estimate the size of a transaction.
22
+ */
23
+ class EstimateWallet {
24
+ constructor(wif, networkType = network_1.NetworkType.MAINNET, addressType = types_1.AddressType.P2WPKH) {
25
+ const network = (0, network_1.toPsbtNetwork)(networkType);
26
+ const keyPair = bitcoin_core_1.ECPair.fromWIF(wif, network);
27
+ this.keyring = new keyring_1.SimpleKeyring({
28
+ privateKeys: [keyPair.privateKey.toString('hex')],
29
+ network: network
30
+ });
31
+ this.keyring.addAccounts(1);
32
+ this.pubkey = keyPair.publicKey.toString('hex');
33
+ this.address = (0, address_1.publicKeyToAddress)(this.pubkey, addressType, networkType);
34
+ this.network = network;
35
+ this.networkType = networkType;
36
+ this.addressType = addressType;
37
+ }
38
+ static fromRandom(addressType = types_1.AddressType.P2WPKH, networkType = network_1.NetworkType.MAINNET) {
39
+ const network = (0, network_1.toPsbtNetwork)(networkType);
40
+ const ecpair = bitcoin_core_1.ECPair.makeRandom({ network });
41
+ const wallet = new EstimateWallet(ecpair.toWIF(), networkType, addressType);
42
+ return wallet;
43
+ }
44
+ getNetworkType() {
45
+ return this.networkType;
46
+ }
47
+ signPsbt(psbt, opts) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const _opts = opts || {
50
+ autoFinalized: true,
51
+ toSignInputs: []
52
+ };
53
+ let _inputs = yield this.formatOptionsToSignInputs(psbt, opts);
54
+ if (_inputs.length == 0) {
55
+ throw new Error('no input to sign');
56
+ }
57
+ psbt.data.inputs.forEach((v) => {
58
+ var _a;
59
+ const isNotSigned = !(v.finalScriptSig || v.finalScriptWitness);
60
+ const isP2TR = this.addressType === types_1.AddressType.P2TR || this.addressType === types_1.AddressType.M44_P2TR;
61
+ const lostInternalPubkey = !v.tapInternalKey;
62
+ // Special measures taken for compatibility with certain applications.
63
+ if (isNotSigned && isP2TR && lostInternalPubkey) {
64
+ const tapInternalKey = (0, utils_1.toXOnly)(Buffer.from(this.pubkey, 'hex'));
65
+ const { output } = bitcoin_core_1.bitcoin.payments.p2tr({
66
+ internalPubkey: tapInternalKey,
67
+ network: (0, network_1.toPsbtNetwork)(this.networkType)
68
+ });
69
+ if (((_a = v.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script.toString('hex')) == (output === null || output === void 0 ? void 0 : output.toString('hex'))) {
70
+ v.tapInternalKey = tapInternalKey;
71
+ }
72
+ }
73
+ });
74
+ psbt = yield this.keyring.signTransaction(psbt, _inputs);
75
+ if (_opts.autoFinalized) {
76
+ _inputs.forEach((v) => {
77
+ // psbt.validateSignaturesOfInput(v.index, validator);
78
+ psbt.finalizeInput(v.index);
79
+ });
80
+ }
81
+ return psbt;
82
+ });
83
+ }
84
+ getPublicKey() {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ const pubkeys = yield this.keyring.getAccounts();
87
+ return pubkeys[0];
88
+ });
89
+ }
90
+ signMessage(text, type) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ if (type === 'bip322-simple') {
93
+ return yield (0, message_1.signMessageOfBIP322Simple)({
94
+ message: text,
95
+ address: this.address,
96
+ networkType: this.networkType,
97
+ wallet: this
98
+ });
99
+ }
100
+ else {
101
+ const pubkey = yield this.getPublicKey();
102
+ return yield this.keyring.signMessage(pubkey, text);
103
+ }
104
+ });
105
+ }
106
+ formatOptionsToSignInputs(_psbt, options) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const accountAddress = this.address;
109
+ const accountPubkey = yield this.getPublicKey();
110
+ let toSignInputs = [];
111
+ if (options && options.toSignInputs) {
112
+ // We expect userToSignInputs objects to be similar to ToSignInput interface,
113
+ // but we allow address to be specified in addition to publicKey for convenience.
114
+ toSignInputs = options.toSignInputs.map((input) => {
115
+ var _a;
116
+ const index = Number(input.index);
117
+ if (isNaN(index))
118
+ throw new Error('invalid index in toSignInput');
119
+ if (!input.address && !input.publicKey) {
120
+ throw new Error('no address or public key in toSignInput');
121
+ }
122
+ if (input.address &&
123
+ input.address != accountAddress) {
124
+ throw new Error('invalid address in toSignInput');
125
+ }
126
+ if (input.publicKey &&
127
+ input.publicKey != accountPubkey) {
128
+ throw new Error('invalid public key in toSignInput');
129
+ }
130
+ const sighashTypes = (_a = input.sighashTypes) === null || _a === void 0 ? void 0 : _a.map(Number);
131
+ if (sighashTypes === null || sighashTypes === void 0 ? void 0 : sighashTypes.some(isNaN))
132
+ throw new Error('invalid sighash type in toSignInput');
133
+ return {
134
+ index,
135
+ publicKey: accountPubkey,
136
+ sighashTypes,
137
+ disableTweakSigner: input.disableTweakSigner
138
+ };
139
+ });
140
+ }
141
+ else {
142
+ const networkType = this.getNetworkType();
143
+ const psbtNetwork = (0, network_1.toPsbtNetwork)(networkType);
144
+ const psbt = typeof _psbt === 'string'
145
+ ? bitcoin_core_1.bitcoin.Psbt.fromHex(_psbt, { network: psbtNetwork })
146
+ : _psbt;
147
+ psbt.data.inputs.forEach((v, index) => {
148
+ let script = null;
149
+ if (v.witnessUtxo) {
150
+ script = v.witnessUtxo.script;
151
+ }
152
+ else if (v.nonWitnessUtxo) {
153
+ const tx = bitcoin_core_1.bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo);
154
+ const output = tx.outs[psbt.txInputs[index].index];
155
+ script = output.script;
156
+ }
157
+ const isSigned = v.finalScriptSig || v.finalScriptWitness;
158
+ if (script && !isSigned) {
159
+ const address = (0, address_1.scriptPkToAddress)(script, this.networkType);
160
+ if (accountAddress === address) {
161
+ toSignInputs.push({
162
+ index,
163
+ publicKey: accountPubkey,
164
+ sighashTypes: v.sighashType ? [v.sighashType] : undefined
165
+ });
166
+ }
167
+ }
168
+ });
169
+ }
170
+ return toSignInputs;
171
+ });
172
+ }
173
+ }
174
+ exports.EstimateWallet = EstimateWallet;
@@ -0,0 +1,3 @@
1
+ export * from './abstract-wallet';
2
+ export * from './estimate-wallet';
3
+ export * from './local-wallet';
@@ -0,0 +1,19 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./abstract-wallet"), exports);
18
+ __exportStar(require("./estimate-wallet"), exports);
19
+ __exportStar(require("./local-wallet"), exports);
@@ -0,0 +1,23 @@
1
+ import { bitcoin } from '../bitcoin-core';
2
+ import { SimpleKeyring } from '../keyring';
3
+ import { NetworkType } from '../network';
4
+ import { AddressType, SignPsbtOptions } from '../types';
5
+ import { AbstractWallet } from './abstract-wallet';
6
+ export declare class LocalWallet implements AbstractWallet {
7
+ keyring: SimpleKeyring;
8
+ address: string;
9
+ pubkey: string;
10
+ network: bitcoin.Network;
11
+ addressType: AddressType;
12
+ networkType: NetworkType;
13
+ scriptPk: string;
14
+ constructor(wif: string, addressType?: AddressType, networkType?: NetworkType);
15
+ static fromMnemonic(addressType: AddressType, networkType: NetworkType, mnemonic: string, passPhrase?: string, hdPath?: string): LocalWallet;
16
+ static fromRandom(addressType?: AddressType, networkType?: NetworkType): LocalWallet;
17
+ getNetworkType(): NetworkType;
18
+ signPsbt(psbt: bitcoin.Psbt, opts?: SignPsbtOptions): Promise<bitcoin.Psbt>;
19
+ getPublicKey(): string;
20
+ signMessage(text: string, type: 'bip322-simple' | 'ecdsa'): Promise<string>;
21
+ signData(data: string, type?: 'ecdsa' | 'schnorr'): Promise<string>;
22
+ private formatOptionsToSignInputs;
23
+ }
@@ -0,0 +1,183 @@
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.LocalWallet = void 0;
13
+ const address_1 = require("../address");
14
+ const bitcoin_core_1 = require("../bitcoin-core");
15
+ const keyring_1 = require("../keyring");
16
+ const message_1 = require("../message");
17
+ const network_1 = require("../network");
18
+ const types_1 = require("../types");
19
+ const utils_1 = require("../utils");
20
+ class LocalWallet {
21
+ constructor(wif, addressType = types_1.AddressType.P2WPKH, networkType = network_1.NetworkType.MAINNET) {
22
+ const network = (0, network_1.toPsbtNetwork)(networkType);
23
+ const keyPair = bitcoin_core_1.ECPair.fromWIF(wif, network);
24
+ this.keyring = new keyring_1.SimpleKeyring({ privateKeys: [keyPair.privateKey.toString('hex')], network: network });
25
+ this.keyring.addAccounts(1);
26
+ this.pubkey = keyPair.publicKey.toString('hex');
27
+ this.address = (0, address_1.publicKeyToAddress)(this.pubkey, addressType, networkType);
28
+ this.network = network;
29
+ this.networkType = networkType;
30
+ this.addressType = addressType;
31
+ this.scriptPk = (0, address_1.publicKeyToScriptPk)(this.pubkey, addressType, networkType);
32
+ }
33
+ static fromMnemonic(addressType, networkType, mnemonic, passPhrase, hdPath) {
34
+ const keyring = new keyring_1.HdKeyring({
35
+ mnemonic,
36
+ hdPath,
37
+ passphrase: passPhrase,
38
+ activeIndexes: [0]
39
+ });
40
+ const _wallet = keyring.wallets[0];
41
+ _wallet.network = (0, network_1.toPsbtNetwork)(networkType);
42
+ return new LocalWallet(keyring.wallets[0].toWIF(), addressType, networkType);
43
+ }
44
+ static fromRandom(addressType = types_1.AddressType.P2WPKH, networkType = network_1.NetworkType.MAINNET) {
45
+ const network = (0, network_1.toPsbtNetwork)(networkType);
46
+ const ecpair = bitcoin_core_1.ECPair.makeRandom({ network });
47
+ return new LocalWallet(ecpair.toWIF(), addressType, networkType);
48
+ }
49
+ getNetworkType() {
50
+ return this.networkType;
51
+ }
52
+ signPsbt(psbt, opts) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const _opts = opts || {
55
+ autoFinalized: true,
56
+ toSignInputs: []
57
+ };
58
+ let _inputs = yield this.formatOptionsToSignInputs(psbt, opts);
59
+ if (_inputs.length == 0) {
60
+ throw new Error('no input to sign');
61
+ }
62
+ psbt.data.inputs.forEach((v) => {
63
+ var _a;
64
+ const isNotSigned = !(v.finalScriptSig || v.finalScriptWitness);
65
+ const isP2TR = this.addressType === types_1.AddressType.P2TR || this.addressType === types_1.AddressType.M44_P2TR;
66
+ const lostInternalPubkey = !v.tapInternalKey;
67
+ // Special measures taken for compatibility with certain applications.
68
+ if (isNotSigned && isP2TR && lostInternalPubkey) {
69
+ const tapInternalKey = (0, utils_1.toXOnly)(Buffer.from(this.pubkey, 'hex'));
70
+ const { output } = bitcoin_core_1.bitcoin.payments.p2tr({
71
+ internalPubkey: tapInternalKey,
72
+ network: (0, network_1.toPsbtNetwork)(this.networkType)
73
+ });
74
+ if (((_a = v.witnessUtxo) === null || _a === void 0 ? void 0 : _a.script.toString('hex')) == (output === null || output === void 0 ? void 0 : output.toString('hex'))) {
75
+ v.tapInternalKey = tapInternalKey;
76
+ }
77
+ }
78
+ });
79
+ psbt = this.keyring.signTransaction(psbt, _inputs);
80
+ if (_opts.autoFinalized) {
81
+ _inputs.forEach((v) => {
82
+ // psbt.validateSignaturesOfInput(v.index, validator);
83
+ psbt.finalizeInput(v.index);
84
+ });
85
+ }
86
+ return psbt;
87
+ });
88
+ }
89
+ getPublicKey() {
90
+ const pubkeys = this.keyring.getAccounts();
91
+ return pubkeys[0];
92
+ }
93
+ signMessage(text, type) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ if (type === 'bip322-simple') {
96
+ return yield (0, message_1.signMessageOfBIP322Simple)({
97
+ message: text,
98
+ address: this.address,
99
+ networkType: this.networkType,
100
+ wallet: this
101
+ });
102
+ }
103
+ else {
104
+ const pubkey = this.getPublicKey();
105
+ return this.keyring.signMessage(pubkey, text);
106
+ }
107
+ });
108
+ }
109
+ signData(data, type = 'ecdsa') {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ const pubkey = this.getPublicKey();
112
+ return this.keyring.signData(pubkey, data, type);
113
+ });
114
+ }
115
+ formatOptionsToSignInputs(_psbt, options) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const accountAddress = this.address;
118
+ const accountPubkey = this.getPublicKey();
119
+ let toSignInputs = [];
120
+ if (options && options.toSignInputs) {
121
+ // We expect userToSignInputs objects to be similar to ToSignInput interface,
122
+ // but we allow address to be specified in addition to publicKey for convenience.
123
+ toSignInputs = options.toSignInputs.map((input) => {
124
+ var _a;
125
+ const index = Number(input.index);
126
+ if (isNaN(index))
127
+ throw new Error('invalid index in toSignInput');
128
+ if (!input.address && !input.publicKey) {
129
+ throw new Error('no address or public key in toSignInput');
130
+ }
131
+ if (input.address &&
132
+ input.address != accountAddress) {
133
+ throw new Error('invalid address in toSignInput');
134
+ }
135
+ if (input.publicKey &&
136
+ input.publicKey != accountPubkey) {
137
+ throw new Error('invalid public key in toSignInput');
138
+ }
139
+ const sighashTypes = (_a = input.sighashTypes) === null || _a === void 0 ? void 0 : _a.map(Number);
140
+ if (sighashTypes === null || sighashTypes === void 0 ? void 0 : sighashTypes.some(isNaN))
141
+ throw new Error('invalid sighash type in toSignInput');
142
+ return {
143
+ index,
144
+ publicKey: accountPubkey,
145
+ sighashTypes,
146
+ disableTweakSigner: input.disableTweakSigner
147
+ };
148
+ });
149
+ }
150
+ else {
151
+ const networkType = this.getNetworkType();
152
+ const psbtNetwork = (0, network_1.toPsbtNetwork)(networkType);
153
+ const psbt = typeof _psbt === 'string'
154
+ ? bitcoin_core_1.bitcoin.Psbt.fromHex(_psbt, { network: psbtNetwork })
155
+ : _psbt;
156
+ psbt.data.inputs.forEach((v, index) => {
157
+ let script = null;
158
+ if (v.witnessUtxo) {
159
+ script = v.witnessUtxo.script;
160
+ }
161
+ else if (v.nonWitnessUtxo) {
162
+ const tx = bitcoin_core_1.bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo);
163
+ const output = tx.outs[psbt.txInputs[index].index];
164
+ script = output.script;
165
+ }
166
+ const isSigned = v.finalScriptSig || v.finalScriptWitness;
167
+ if (script && !isSigned) {
168
+ const address = (0, address_1.scriptPkToAddress)(script, this.networkType);
169
+ if (accountAddress === address) {
170
+ toSignInputs.push({
171
+ index,
172
+ publicKey: accountPubkey,
173
+ sighashTypes: v.sighashType ? [v.sighashType] : undefined
174
+ });
175
+ }
176
+ }
177
+ });
178
+ }
179
+ return toSignInputs;
180
+ });
181
+ }
182
+ }
183
+ exports.LocalWallet = LocalWallet;
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "@btc-vision/wallet-sdk",
3
+ "version": "1.0.0",
4
+ "description": "UniSat Wallet SDK",
5
+ "keywords": [
6
+ "bitcoin",
7
+ "keyring",
8
+ "unisat"
9
+ ],
10
+ "homepage": "https://github.com/btc-vision/wallet-sdk#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/btc-vision/wallet-sdk/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/btc-vision/wallet-sdk.git"
17
+ },
18
+ "author": "cybersinsloth",
19
+ "license": "ISC",
20
+ "main": "lib/index.js",
21
+ "module": "es/index.js",
22
+ "types": "lib/index.d.ts",
23
+ "files": [
24
+ "/es",
25
+ "/lib"
26
+ ],
27
+ "scripts": {
28
+ "build": "npm run build:cjs && npm run build:esm",
29
+ "build:cjs": "rimraf lib && cross-env BABEL_ENV=cjs gulp build",
30
+ "build:esm": "rimraf es && cross-env BABEL_ENV=esm gulp build",
31
+ "build:typed": "tsc --declaration --emitDeclarationOnly --noEmit false",
32
+ "test": "mocha -r ts-node/register test/**/*.ts --timeout 300000",
33
+ "prepublishOnly": "npm run build",
34
+ "lint": "eslint ./src ./test"
35
+ },
36
+ "dependencies": {
37
+ "@bitcoinerlab/secp256k1": "^1.1.1",
38
+ "@keystonehq/keystone-sdk": "^0.3.0",
39
+ "big-integer": "^1.6.52",
40
+ "bignumber.js": "^9.1.2",
41
+ "bip-schnorr": "^0.6.7",
42
+ "bip39": "^3.1.0",
43
+ "bitcoinjs-lib": "^6.1.6",
44
+ "bitcore-lib": "^10.2.1",
45
+ "ecpair": "^2.1.0",
46
+ "hdkey": "^2.1.0"
47
+ },
48
+ "devDependencies": {
49
+ "@babel/core": "^7.16.7",
50
+ "@babel/plugin-transform-runtime": "^7.16.5",
51
+ "@babel/preset-env": "^7.16.5",
52
+ "@babel/preset-typescript": "^7.16.5",
53
+ "@types/bitcore-lib": "^0.15.6",
54
+ "@types/chai": "^4.2.20",
55
+ "@types/hdkey": "^2.0.3",
56
+ "@types/mocha": "^8.2.2",
57
+ "@types/node": "^16.11.10",
58
+ "@typescript-eslint/eslint-plugin": "^5.59.1",
59
+ "@typescript-eslint/parser": "^5.59.1",
60
+ "babel-loader": "^8.2.3",
61
+ "browserify-zlib": "^0.2.0",
62
+ "chai": "^4.3.4",
63
+ "clean-webpack-plugin": "^4.0.0",
64
+ "cross-env": "^7.0.3",
65
+ "crypto-browserify": "^3.12.0",
66
+ "eslint": "8.0.0",
67
+ "eslint-config-prettier": "^9.1.0",
68
+ "eslint-plugin-prettier": "^5.1.3",
69
+ "eslint-webpack-plugin": "^4.1.0",
70
+ "glob": "^7.1.7",
71
+ "gulp": "^4.0.2",
72
+ "gulp-babel": "^8.0.0",
73
+ "gulp-typescript": "*",
74
+ "https-browserify": "^1.0.0",
75
+ "mocha": "10.1.0",
76
+ "prettier": "^3.2.5",
77
+ "stream-browserify": "^3.0.0",
78
+ "stream-http": "^3.2.0",
79
+ "ts-loader": "^9.5.1",
80
+ "ts-node": "^10.4.0",
81
+ "tsconfig-paths-webpack-plugin": "^4.1.0",
82
+ "typescript": "^4.5.3",
83
+ "watch": "^1.0.2",
84
+ "watchify": "^4.0.0",
85
+ "webpack": "^5.91.0",
86
+ "webpack-cli": "^4.9.1",
87
+ "webpack-stream": "^7.0.0",
88
+ "vm-browserify": "^1.1.2"
89
+ },
90
+ "engines": {
91
+ "node": ">=14.0.0"
92
+ },
93
+ "publishConfig": {
94
+ "access": "public",
95
+ "registry": "https://registry.npmjs.org/"
96
+ }
97
+ }