mainnet-js 2.7.31 → 3.0.0-next.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/dist/index.html +1 -1
- package/dist/{mainnet-2.7.31.js → mainnet-3.0.0-next.0.js} +718 -678
- package/dist/module/cache/MemoryCache.d.ts +0 -1
- package/dist/module/cache/MemoryCache.d.ts.map +1 -1
- package/dist/module/cache/MemoryCache.js +5 -7
- package/dist/module/cache/MemoryCache.js.map +1 -1
- package/dist/module/cache/index.d.ts +1 -0
- package/dist/module/cache/index.d.ts.map +1 -1
- package/dist/module/cache/index.js +1 -0
- package/dist/module/cache/index.js.map +1 -1
- package/dist/module/cache/walletCache.d.ts +39 -0
- package/dist/module/cache/walletCache.d.ts.map +1 -0
- package/dist/module/cache/walletCache.js +141 -0
- package/dist/module/cache/walletCache.js.map +1 -0
- package/dist/module/history/{electrumTransformer.d.ts → getHistory.d.ts} +3 -3
- package/dist/module/history/getHistory.d.ts.map +1 -0
- package/dist/module/history/{electrumTransformer.js → getHistory.js} +47 -14
- package/dist/module/history/getHistory.js.map +1 -0
- package/dist/module/index.d.ts +3 -1
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +3 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/interface.d.ts +7 -1
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/message/interface.d.ts +2 -2
- package/dist/module/message/interface.d.ts.map +1 -1
- package/dist/module/message/interface.js +0 -3
- package/dist/module/message/interface.js.map +1 -1
- package/dist/module/message/signed.d.ts +5 -5
- package/dist/module/message/signed.d.ts.map +1 -1
- package/dist/module/message/signed.js +14 -14
- package/dist/module/message/signed.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +2 -1
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/NetworkProvider.d.ts +2 -2
- package/dist/module/network/NetworkProvider.d.ts.map +1 -1
- package/dist/module/network/constant.js +4 -4
- package/dist/module/network/constant.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +21 -19
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +18 -15
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/util/checkUtxos.d.ts +2 -2
- package/dist/module/util/checkUtxos.d.ts.map +1 -1
- package/dist/module/util/checkUtxos.js +11 -12
- package/dist/module/util/checkUtxos.js.map +1 -1
- package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
- package/dist/module/util/deriveCashaddr.js +6 -0
- package/dist/module/util/deriveCashaddr.js.map +1 -1
- package/dist/module/util/deriveNetwork.js +1 -1
- package/dist/module/util/deriveNetwork.js.map +1 -1
- package/dist/module/util/hd.d.ts +3 -0
- package/dist/module/util/hd.d.ts.map +1 -0
- package/dist/module/util/hd.js +11 -0
- package/dist/module/util/hd.js.map +1 -0
- package/dist/module/util/index.d.ts +1 -0
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +1 -0
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/sumUtxoValue.d.ts +3 -3
- package/dist/module/util/sumUtxoValue.d.ts.map +1 -1
- package/dist/module/util/sumUtxoValue.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +37 -93
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +83 -250
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts +164 -0
- package/dist/module/wallet/HDWallet.d.ts.map +1 -0
- package/dist/module/wallet/HDWallet.js +486 -0
- package/dist/module/wallet/HDWallet.js.map +1 -0
- package/dist/module/wallet/Watch.d.ts +151 -0
- package/dist/module/wallet/Watch.d.ts.map +1 -0
- package/dist/module/wallet/Watch.js +307 -0
- package/dist/module/wallet/Watch.js.map +1 -0
- package/dist/module/wallet/Wif.d.ts +23 -29
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +204 -267
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +7 -1
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +26 -17
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +3 -3
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +3 -3
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +2 -18
- package/dist/module/wallet/model.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cache/MemoryCache.ts +5 -5
- package/src/cache/index.ts +1 -0
- package/src/cache/walletCache.ts +252 -0
- package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +6 -19
- package/src/history/{electrumTransformer.ts → getHistory.ts} +63 -15
- package/src/index.ts +3 -1
- package/src/interface.ts +8 -1
- package/src/message/interface.ts +2 -28
- package/src/message/signed.test.ts +56 -48
- package/src/message/signed.ts +15 -18
- package/src/network/ElectrumNetworkProvider.ts +4 -4
- package/src/network/NetworkProvider.ts +2 -2
- package/src/network/Rpc.test.ts +1 -1
- package/src/network/constant.ts +4 -4
- package/src/transaction/Wif.ts +41 -35
- package/src/util/checkUtxos.ts +21 -26
- package/src/util/deriveCashaddr.ts +8 -0
- package/src/util/deriveNetwork.ts +1 -1
- package/src/util/derivePublicKeyHash.test.ts +0 -13
- package/src/util/hd.ts +16 -0
- package/src/util/index.ts +1 -0
- package/src/util/sumUtxoValue.ts +5 -5
- package/src/wallet/Base.ts +123 -332
- package/src/wallet/HDWallet.test.ts +372 -0
- package/src/wallet/HDWallet.ts +764 -0
- package/src/wallet/Watch.ts +447 -0
- package/src/wallet/Wif.ts +258 -283
- package/src/wallet/createWallet.ts +28 -18
- package/src/wallet/interface.ts +3 -3
- package/src/wallet/model.test.ts +2 -2
- package/src/wallet/model.ts +6 -23
- package/dist/module/history/electrumTransformer.d.ts.map +0 -1
- package/dist/module/history/electrumTransformer.js.map +0 -1
package/src/transaction/Wif.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
2
|
walletTemplateP2pkhNonHd,
|
|
3
|
-
walletTemplateToCompilerBCH,
|
|
4
3
|
cashAddressToLockingBytecode,
|
|
5
4
|
Compiler,
|
|
6
5
|
encodeTransaction,
|
|
@@ -8,14 +7,15 @@ import {
|
|
|
8
7
|
importWalletTemplate,
|
|
9
8
|
AnyCompilerConfiguration,
|
|
10
9
|
AuthenticationProgramStateCommon,
|
|
11
|
-
CompilationContextBCH,
|
|
12
10
|
Output,
|
|
13
11
|
hexToBin,
|
|
14
12
|
verifyTransactionTokens,
|
|
15
13
|
decodeTransaction,
|
|
16
14
|
TransactionTemplateFixed,
|
|
15
|
+
CompilationContextBch,
|
|
16
|
+
walletTemplateToCompilerBch,
|
|
17
17
|
} from "@bitauth/libauth";
|
|
18
|
-
import { NFTCapability, TokenI,
|
|
18
|
+
import { NFTCapability, TokenI, Utxo } from "../interface.js";
|
|
19
19
|
import { allocateFee } from "./allocateFee.js";
|
|
20
20
|
|
|
21
21
|
import { DUST_UTXO_THRESHOLD } from "../constant.js";
|
|
@@ -30,26 +30,31 @@ import { amountInSatoshi } from "../util/amountInSatoshi.js";
|
|
|
30
30
|
import { sumSendRequestAmounts } from "../util/sumSendRequestAmounts.js";
|
|
31
31
|
import { sumUtxoValue } from "../util/sumUtxoValue.js";
|
|
32
32
|
import { FeePaidByEnum } from "../wallet/enum.js";
|
|
33
|
+
import { WalletCacheI } from "../cache/walletCache.js";
|
|
34
|
+
|
|
35
|
+
export const placeholderPrivateKey =
|
|
36
|
+
"0000000000000000000000000000000000000000000000000000000000000001";
|
|
37
|
+
export const placeholderPrivateKeyBin = hexToBin(placeholderPrivateKey);
|
|
33
38
|
|
|
34
39
|
// Build a transaction for a p2pkh transaction for a non HD wallet
|
|
35
40
|
export async function buildP2pkhNonHdTransaction({
|
|
36
41
|
inputs,
|
|
37
42
|
outputs,
|
|
38
43
|
signingKey,
|
|
39
|
-
sourceAddress,
|
|
40
44
|
fee = 0,
|
|
41
45
|
discardChange = false,
|
|
42
46
|
feePaidBy = FeePaidByEnum.change,
|
|
43
47
|
changeAddress = "",
|
|
48
|
+
walletCache,
|
|
44
49
|
}: {
|
|
45
|
-
inputs:
|
|
50
|
+
inputs: Utxo[];
|
|
46
51
|
outputs: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
47
|
-
signingKey
|
|
48
|
-
sourceAddress: string;
|
|
52
|
+
signingKey?: Uint8Array;
|
|
49
53
|
fee?: number;
|
|
50
54
|
discardChange?: boolean;
|
|
51
55
|
feePaidBy?: FeePaidByEnum;
|
|
52
56
|
changeAddress?: string;
|
|
57
|
+
walletCache?: WalletCacheI;
|
|
53
58
|
}) {
|
|
54
59
|
if (!signingKey) {
|
|
55
60
|
throw new Error("Missing signing key when building transaction");
|
|
@@ -60,8 +65,8 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
60
65
|
throw new Error("Transaction template error");
|
|
61
66
|
}
|
|
62
67
|
|
|
63
|
-
const compiler =
|
|
64
|
-
const inputAmount =
|
|
68
|
+
const compiler = walletTemplateToCompilerBch(template);
|
|
69
|
+
const inputAmount = sumUtxoValue(inputs);
|
|
65
70
|
|
|
66
71
|
const sendAmount = await sumSendRequestAmounts(outputs);
|
|
67
72
|
|
|
@@ -72,7 +77,7 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
72
77
|
const lockedOutputs = await prepareOutputs(outputs);
|
|
73
78
|
|
|
74
79
|
if (!changeAddress) {
|
|
75
|
-
changeAddress =
|
|
80
|
+
changeAddress = inputs[0].address;
|
|
76
81
|
}
|
|
77
82
|
|
|
78
83
|
if (discardChange !== true) {
|
|
@@ -92,7 +97,7 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
92
97
|
inputs,
|
|
93
98
|
compiler,
|
|
94
99
|
signingKey,
|
|
95
|
-
|
|
100
|
+
walletCache,
|
|
96
101
|
});
|
|
97
102
|
const result = generateTransaction({
|
|
98
103
|
inputs: preparedInputs,
|
|
@@ -121,16 +126,16 @@ export function prepareInputs({
|
|
|
121
126
|
inputs,
|
|
122
127
|
compiler,
|
|
123
128
|
signingKey,
|
|
124
|
-
|
|
129
|
+
walletCache,
|
|
125
130
|
}: {
|
|
126
|
-
inputs:
|
|
131
|
+
inputs: Utxo[];
|
|
127
132
|
compiler: Compiler<
|
|
128
|
-
|
|
129
|
-
AnyCompilerConfiguration<
|
|
133
|
+
CompilationContextBch,
|
|
134
|
+
AnyCompilerConfiguration<CompilationContextBch>,
|
|
130
135
|
AuthenticationProgramStateCommon
|
|
131
136
|
>;
|
|
132
137
|
signingKey: Uint8Array;
|
|
133
|
-
|
|
138
|
+
walletCache?: WalletCacheI;
|
|
134
139
|
}) {
|
|
135
140
|
const preparedInputs: any[] = [];
|
|
136
141
|
const sourceOutputs: any[] = [];
|
|
@@ -160,7 +165,9 @@ export function prepareInputs({
|
|
|
160
165
|
}
|
|
161
166
|
: undefined,
|
|
162
167
|
};
|
|
163
|
-
const key =
|
|
168
|
+
const key =
|
|
169
|
+
walletCache?.getByAddress(i.address)?.privateKey ??
|
|
170
|
+
(signingKey?.length ? signingKey : Uint8Array.from(Array(32)));
|
|
164
171
|
const newInput = {
|
|
165
172
|
outpointIndex: utxoIndex,
|
|
166
173
|
outpointTransactionHash: utxoOutpointTransactionHash,
|
|
@@ -178,7 +185,7 @@ export function prepareInputs({
|
|
|
178
185
|
|
|
179
186
|
preparedInputs.push(newInput);
|
|
180
187
|
|
|
181
|
-
const lockingBytecode = cashAddressToLockingBytecode(
|
|
188
|
+
const lockingBytecode = cashAddressToLockingBytecode(i.address);
|
|
182
189
|
if (typeof lockingBytecode === "string") {
|
|
183
190
|
throw lockingBytecode;
|
|
184
191
|
}
|
|
@@ -292,22 +299,22 @@ export function prepareTokenOutputs(request: TokenSendRequest): Output {
|
|
|
292
299
|
* @returns A promise to a list of unspent outputs
|
|
293
300
|
*/
|
|
294
301
|
export async function getSuitableUtxos(
|
|
295
|
-
inputs:
|
|
302
|
+
inputs: Utxo[],
|
|
296
303
|
amountRequired: bigint | undefined,
|
|
297
304
|
bestHeight: number,
|
|
298
305
|
feePaidBy: FeePaidByEnum,
|
|
299
306
|
requests: SendRequestType[],
|
|
300
|
-
ensureUtxos:
|
|
307
|
+
ensureUtxos: Utxo[] = [],
|
|
301
308
|
tokenOperation: "send" | "genesis" | "mint" | "burn" = "send"
|
|
302
|
-
): Promise<
|
|
303
|
-
const suitableUtxos:
|
|
309
|
+
): Promise<Utxo[]> {
|
|
310
|
+
const suitableUtxos: Utxo[] = [...ensureUtxos];
|
|
304
311
|
let amountAvailable = BigInt(0);
|
|
305
312
|
const tokenRequests = requests.filter(
|
|
306
313
|
(val) => val instanceof TokenSendRequest
|
|
307
314
|
) as TokenSendRequest[];
|
|
308
315
|
|
|
309
316
|
const availableInputs = inputs.slice();
|
|
310
|
-
const selectedInputs:
|
|
317
|
+
const selectedInputs: Utxo[] = [];
|
|
311
318
|
|
|
312
319
|
// find matching utxos for token transfers
|
|
313
320
|
if (tokenOperation === "send") {
|
|
@@ -438,9 +445,8 @@ export async function getFeeAmountSimple({
|
|
|
438
445
|
relayFeePerByteInSatoshi,
|
|
439
446
|
discardChange,
|
|
440
447
|
}: {
|
|
441
|
-
utxos:
|
|
448
|
+
utxos: Utxo[];
|
|
442
449
|
sendRequests: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
443
|
-
privateKey: Uint8Array;
|
|
444
450
|
sourceAddress: string;
|
|
445
451
|
relayFeePerByteInSatoshi: number;
|
|
446
452
|
feePaidBy: FeePaidByEnum;
|
|
@@ -494,19 +500,19 @@ export async function getFeeAmountSimple({
|
|
|
494
500
|
export async function getFeeAmount({
|
|
495
501
|
utxos,
|
|
496
502
|
sendRequests,
|
|
497
|
-
privateKey,
|
|
498
503
|
sourceAddress,
|
|
499
504
|
relayFeePerByteInSatoshi,
|
|
500
505
|
feePaidBy,
|
|
501
506
|
discardChange,
|
|
507
|
+
walletCache,
|
|
502
508
|
}: {
|
|
503
|
-
utxos:
|
|
509
|
+
utxos: Utxo[];
|
|
504
510
|
sendRequests: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
505
|
-
privateKey: Uint8Array;
|
|
506
511
|
sourceAddress: string;
|
|
507
512
|
relayFeePerByteInSatoshi: number;
|
|
508
513
|
feePaidBy: FeePaidByEnum;
|
|
509
514
|
discardChange?: boolean;
|
|
515
|
+
walletCache?: WalletCacheI;
|
|
510
516
|
}) {
|
|
511
517
|
// build transaction
|
|
512
518
|
if (utxos) {
|
|
@@ -515,12 +521,12 @@ export async function getFeeAmount({
|
|
|
515
521
|
await buildEncodedTransaction({
|
|
516
522
|
inputs: utxos,
|
|
517
523
|
outputs: sendRequests,
|
|
518
|
-
signingKey:
|
|
519
|
-
sourceAddress,
|
|
524
|
+
signingKey: placeholderPrivateKeyBin,
|
|
520
525
|
fee: 0, //DUST_UTXO_THRESHOLD
|
|
521
526
|
discardChange: discardChange ?? false,
|
|
522
527
|
feePaidBy,
|
|
523
528
|
changeAddress: "",
|
|
529
|
+
walletCache,
|
|
524
530
|
});
|
|
525
531
|
|
|
526
532
|
return Math.ceil(draftTransaction.length * relayFeePerByteInSatoshi + 1);
|
|
@@ -536,32 +542,32 @@ export async function buildEncodedTransaction({
|
|
|
536
542
|
inputs,
|
|
537
543
|
outputs,
|
|
538
544
|
signingKey,
|
|
539
|
-
sourceAddress,
|
|
540
545
|
fee = 0,
|
|
541
546
|
discardChange = false,
|
|
542
547
|
feePaidBy = FeePaidByEnum.change,
|
|
543
548
|
changeAddress = "",
|
|
544
549
|
buildUnsigned = false,
|
|
550
|
+
walletCache,
|
|
545
551
|
}: {
|
|
546
|
-
inputs:
|
|
552
|
+
inputs: Utxo[];
|
|
547
553
|
outputs: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
548
554
|
signingKey: Uint8Array;
|
|
549
|
-
sourceAddress: string;
|
|
550
555
|
fee?: number;
|
|
551
556
|
discardChange?: boolean;
|
|
552
557
|
feePaidBy?: FeePaidByEnum;
|
|
553
558
|
changeAddress?: string;
|
|
554
559
|
buildUnsigned?: boolean;
|
|
560
|
+
walletCache?: WalletCacheI;
|
|
555
561
|
}) {
|
|
556
562
|
const { transaction, sourceOutputs } = await buildP2pkhNonHdTransaction({
|
|
557
563
|
inputs,
|
|
558
564
|
outputs,
|
|
559
565
|
signingKey,
|
|
560
|
-
sourceAddress,
|
|
561
566
|
fee,
|
|
562
567
|
discardChange,
|
|
563
568
|
feePaidBy,
|
|
564
569
|
changeAddress,
|
|
570
|
+
walletCache,
|
|
565
571
|
});
|
|
566
572
|
|
|
567
573
|
if (buildUnsigned === true) {
|
|
@@ -592,7 +598,7 @@ export async function signUnsignedTransaction(
|
|
|
592
598
|
throw new Error("Transaction template error");
|
|
593
599
|
}
|
|
594
600
|
|
|
595
|
-
const compiler =
|
|
601
|
+
const compiler = walletTemplateToCompilerBch(template);
|
|
596
602
|
const transactionTemplate: Readonly<
|
|
597
603
|
TransactionTemplateFixed<typeof compiler>
|
|
598
604
|
> = { ...decoded };
|
package/src/util/checkUtxos.ts
CHANGED
|
@@ -1,31 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Utxo, UtxoId } from "../interface.js";
|
|
2
2
|
|
|
3
3
|
export async function checkUtxos(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
): Promise<
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
utxos = utxos.map(
|
|
23
|
-
(val) =>
|
|
24
|
-
addressUtxos.find(
|
|
25
|
-
(utxo) => val.txid === utxo.txid && val.vout === utxo.vout
|
|
26
|
-
)!
|
|
27
|
-
);
|
|
4
|
+
utxoIds: UtxoId[],
|
|
5
|
+
walletOrUtxos: Utxo[] | import("../wallet/Wif.js").Wallet
|
|
6
|
+
): Promise<Utxo[]> {
|
|
7
|
+
const addressUtxos = Array.isArray(walletOrUtxos)
|
|
8
|
+
? walletOrUtxos
|
|
9
|
+
: await walletOrUtxos.getUtxos();
|
|
10
|
+
const absent = utxoIds.filter(
|
|
11
|
+
(val) =>
|
|
12
|
+
!addressUtxos.find(
|
|
13
|
+
(utxo) => val.txid === utxo.txid && val.vout === utxo.vout
|
|
14
|
+
)
|
|
15
|
+
);
|
|
16
|
+
if (absent.length) {
|
|
17
|
+
const absentString = absent
|
|
18
|
+
.map((val) => `${val.txid}:${val.vout}`)
|
|
19
|
+
.join(", ");
|
|
20
|
+
throw Error(`Utxos [${absentString}] not found in wallet`);
|
|
28
21
|
}
|
|
29
22
|
|
|
30
|
-
return
|
|
23
|
+
return addressUtxos.filter((val) =>
|
|
24
|
+
utxoIds.find((utxo) => val.txid === utxo.txid && val.vout === utxo.vout)
|
|
25
|
+
);
|
|
31
26
|
}
|
|
@@ -80,6 +80,10 @@ function decodeAddress(address: string): DecodedCashAddress {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
export function toCashaddr(address: string): string {
|
|
83
|
+
if (address.indexOf(":q") > 0 || address.indexOf(":p") > 0) {
|
|
84
|
+
return address;
|
|
85
|
+
}
|
|
86
|
+
|
|
83
87
|
const result = decodeAddress(address);
|
|
84
88
|
|
|
85
89
|
return encodeCashAddress({
|
|
@@ -90,6 +94,10 @@ export function toCashaddr(address: string): string {
|
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
export function toTokenaddr(address: string): string {
|
|
97
|
+
if (address.indexOf(":z") > 0 || address.indexOf(":r") > 0) {
|
|
98
|
+
return address;
|
|
99
|
+
}
|
|
100
|
+
|
|
93
101
|
const result = decodeAddress(address);
|
|
94
102
|
|
|
95
103
|
return encodeCashAddress({
|
|
@@ -10,7 +10,7 @@ export function derivedNetwork(addresses: string[]) {
|
|
|
10
10
|
// Get a unique set of networks requested
|
|
11
11
|
let networkSet = new Set(networks);
|
|
12
12
|
if (networkSet.size > 1) {
|
|
13
|
-
throw Error("
|
|
13
|
+
throw Error("Addresses are not on the same network");
|
|
14
14
|
} else if (networkSet.size === 0) {
|
|
15
15
|
throw Error("No address network was derived");
|
|
16
16
|
} else {
|
|
@@ -53,16 +53,3 @@ test("Should calculate public key hash consistent with hash160", async () => {
|
|
|
53
53
|
expect(binToHex(pkh2)).toEqual("d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa");
|
|
54
54
|
expect(binToHex(pkh3)).toEqual("d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa");
|
|
55
55
|
});
|
|
56
|
-
|
|
57
|
-
test("Should return the a public key hash of manually set wallet", async () => {
|
|
58
|
-
let w = new Wallet();
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
w.privateKey = hexToBin(
|
|
61
|
-
"e284129cc0922579a535bbf4d1a3b25773090d28c909bc0fed73b5e0222cc372"
|
|
62
|
-
);
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
await w.deriveInfo();
|
|
65
|
-
let pkh = derivePublicKeyHash(w.getDepositAddress());
|
|
66
|
-
let expectedPKH = "d986ed01b7a22225a70edbf2ba7cfb63a15cb3aa";
|
|
67
|
-
expect(binToHex(pkh)).toBe(expectedPKH);
|
|
68
|
-
});
|
package/src/util/hd.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const arrayRange = (start: number, stop: number) =>
|
|
2
|
+
Array.from({ length: stop - start }, (_, index) => start + index);
|
|
3
|
+
|
|
4
|
+
export const getNextUnusedIndex = (
|
|
5
|
+
index: number,
|
|
6
|
+
statuses: Array<string | null>
|
|
7
|
+
): number => {
|
|
8
|
+
if (index === -1) {
|
|
9
|
+
index = statuses.findIndex((status) => status === null);
|
|
10
|
+
if (index === -1) {
|
|
11
|
+
index = statuses.length;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return index;
|
|
16
|
+
};
|
package/src/util/index.ts
CHANGED
package/src/util/sumUtxoValue.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Utxo } from "../interface.js";
|
|
2
2
|
|
|
3
|
-
export function sumUtxoValue(utxos:
|
|
3
|
+
export function sumUtxoValue(utxos: Utxo[]) {
|
|
4
4
|
if (utxos.length > 0) {
|
|
5
|
-
const balanceArray: number[] = utxos.map((o:
|
|
5
|
+
const balanceArray: number[] = utxos.map((o: Utxo) => {
|
|
6
6
|
return o.satoshis;
|
|
7
7
|
});
|
|
8
8
|
const balance = balanceArray.reduce((a: number, b: number) => a + b, 0);
|
|
@@ -12,11 +12,11 @@ export function sumUtxoValue(utxos: UtxoI[]) {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function sumTokenAmounts(utxos:
|
|
15
|
+
export function sumTokenAmounts(utxos: Utxo[], tokenId: string): bigint {
|
|
16
16
|
if (utxos.length > 0) {
|
|
17
17
|
const tokenArray: bigint[] = utxos
|
|
18
18
|
.filter((utxo) => utxo.token?.tokenId === tokenId)
|
|
19
|
-
.map((o:
|
|
19
|
+
.map((o: Utxo) => {
|
|
20
20
|
return o.token?.amount || 0n;
|
|
21
21
|
});
|
|
22
22
|
const balance = tokenArray.reduce((a: bigint, b: bigint) => a + b, 0n);
|