mainnet-js 2.7.33 → 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.33.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 +8 -8
- 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 +36 -48
- package/src/message/signed.ts +9 -12
- 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/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);
|