mainnet-js 2.7.34 → 3.0.0-next.1
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.34.js → mainnet-3.0.0-next.1.js} +728 -708
- 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 +45 -0
- package/dist/module/cache/walletCache.d.ts.map +1 -0
- package/dist/module/cache/walletCache.js +140 -0
- package/dist/module/cache/walletCache.js.map +1 -0
- package/dist/module/chain.d.ts +1 -1
- package/dist/module/chain.js +1 -1
- package/dist/module/chain.js.map +1 -1
- package/dist/module/constant.d.ts +1 -1
- package/dist/module/constant.d.ts.map +1 -1
- package/dist/module/constant.js +1 -1
- package/dist/module/constant.js.map +1 -1
- package/dist/module/enum.d.ts +1 -7
- package/dist/module/enum.d.ts.map +1 -1
- package/dist/module/enum.js +0 -6
- package/dist/module/enum.js.map +1 -1
- 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} +17 -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 -2
- 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 +3 -2
- 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/network/getRelayFeeCache.js +2 -2
- package/dist/module/network/getRelayFeeCache.js.map +1 -1
- package/dist/module/rate/ExchangeRate.d.ts +2 -1
- package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.js +4 -1
- package/dist/module/rate/ExchangeRate.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +25 -23
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +26 -25
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/transaction/allocateFee.d.ts +3 -3
- package/dist/module/transaction/allocateFee.d.ts.map +1 -1
- package/dist/module/transaction/allocateFee.js +5 -6
- package/dist/module/transaction/allocateFee.js.map +1 -1
- package/dist/module/util/amountInSatoshi.d.ts +1 -1
- package/dist/module/util/amountInSatoshi.d.ts.map +1 -1
- package/dist/module/util/amountInSatoshi.js +3 -9
- package/dist/module/util/amountInSatoshi.js.map +1 -1
- package/dist/module/util/asSendRequestObject.d.ts.map +1 -1
- package/dist/module/util/asSendRequestObject.js +10 -7
- package/dist/module/util/asSendRequestObject.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/convert.d.ts +3 -0
- package/dist/module/util/convert.d.ts.map +1 -1
- package/dist/module/util/convert.js +12 -0
- package/dist/module/util/convert.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 +3 -3
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +3 -3
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/satoshiToAmount.d.ts +1 -1
- package/dist/module/util/satoshiToAmount.d.ts.map +1 -1
- package/dist/module/util/satoshiToAmount.js +3 -9
- package/dist/module/util/satoshiToAmount.js.map +1 -1
- package/dist/module/util/sumSendRequestAmounts.d.ts.map +1 -1
- package/dist/module/util/sumSendRequestAmounts.js +3 -4
- package/dist/module/util/sumSendRequestAmounts.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 +2 -2
- package/dist/module/util/sumUtxoValue.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +45 -103
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +99 -298
- 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/Util.js +1 -1
- package/dist/module/wallet/Util.js.map +1 -1
- 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 +5 -6
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +15 -19
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +5 -25
- 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 +239 -0
- package/src/chain.ts +1 -1
- package/src/constant.ts +1 -1
- package/src/enum.ts +0 -6
- package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +26 -53
- package/src/history/{electrumTransformer.ts → getHistory.ts} +31 -15
- package/src/index.ts +3 -1
- package/src/interface.ts +8 -2
- 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/Connection.test.ts +3 -3
- package/src/network/ElectrumNetworkProvider.ts +5 -5
- package/src/network/NetworkProvider.ts +2 -2
- package/src/network/Rpc.test.ts +3 -4
- package/src/network/constant.ts +4 -4
- package/src/network/getRelayFeeCache.ts +2 -2
- package/src/rate/ExchangeRate.test.ts +2 -44
- package/src/rate/ExchangeRate.ts +5 -2
- package/src/transaction/Wif.ts +59 -52
- package/src/transaction/allocateFee.test.ts +110 -131
- package/src/transaction/allocateFee.ts +14 -15
- package/src/util/amountInSatoshi.test.ts +1 -9
- package/src/util/amountInSatoshi.ts +6 -10
- package/src/util/asSendRequestObject.ts +12 -7
- package/src/util/checkUtxos.ts +21 -26
- package/src/util/convert.ts +18 -0
- 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 +3 -7
- package/src/util/satoshiToAmount.test.ts +1 -1
- package/src/util/satoshiToAmount.ts +4 -10
- package/src/util/sumSendRequestAmounts.ts +3 -4
- package/src/util/sumUtxoValue.ts +7 -7
- package/src/wallet/Base.ts +147 -420
- package/src/wallet/Cashtokens.test.headless.js +11 -11
- package/src/wallet/Cashtokens.test.ts +36 -37
- package/src/wallet/HDWallet.test.ts +515 -0
- package/src/wallet/HDWallet.ts +764 -0
- package/src/wallet/Util.ts +1 -1
- package/src/wallet/Watch.ts +447 -0
- package/src/wallet/Wif.bip39.test.ts +1 -1
- package/src/wallet/Wif.test.ts +108 -133
- package/src/wallet/Wif.ts +258 -283
- package/src/wallet/createWallet.ts +28 -18
- package/src/wallet/interface.ts +5 -6
- package/src/wallet/model.test.ts +4 -7
- package/src/wallet/model.ts +19 -51
- package/dist/module/history/electrumTransformer.d.ts.map +0 -1
- package/dist/module/history/electrumTransformer.js.map +0 -1
- package/dist/module/util/balanceObjectFromSatoshi.d.ts +0 -8
- package/dist/module/util/balanceObjectFromSatoshi.d.ts.map +0 -1
- package/dist/module/util/balanceObjectFromSatoshi.js +0 -35
- package/dist/module/util/balanceObjectFromSatoshi.js.map +0 -1
- package/src/util/balanceObjectFromSatoshi.test.ts +0 -58
- package/src/util/balanceObjectFromSatoshi.ts +0 -52
|
@@ -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
|
@@ -8,7 +8,7 @@ export {
|
|
|
8
8
|
binToBase64,
|
|
9
9
|
sha256,
|
|
10
10
|
} from "@bitauth/libauth";
|
|
11
|
-
export
|
|
11
|
+
export * from "./convert.js";
|
|
12
12
|
export { delay } from "./delay.js";
|
|
13
13
|
export { derivedNetwork } from "./deriveNetwork.js";
|
|
14
14
|
export { derivePublicKeyHash } from "./derivePublicKeyHash.js";
|
|
@@ -22,16 +22,12 @@ export {
|
|
|
22
22
|
export { getRuntimePlatform, RuntimePlatform } from "./getRuntimePlatform.js";
|
|
23
23
|
export { getUsdRate } from "./getUsdRate.js";
|
|
24
24
|
export { hash160 } from "./hash160.js";
|
|
25
|
-
export { ExchangeRate } from "../rate/ExchangeRate.js";
|
|
25
|
+
export { ExchangeRate, ExchageRatePromise } from "../rate/ExchangeRate.js";
|
|
26
26
|
export { sanitizeAddress } from "./sanitizeAddress.js";
|
|
27
27
|
export { sanitizeUnit } from "./sanitizeUnit.js";
|
|
28
28
|
export { getWeakRandomInt } from "./randomInt.js";
|
|
29
29
|
export { getXPubKey } from "../util/getXPubKey.js";
|
|
30
30
|
export { sumUtxoValue } from "./sumUtxoValue.js";
|
|
31
|
-
export {
|
|
32
|
-
BalanceResponse,
|
|
33
|
-
balanceFromSatoshi,
|
|
34
|
-
balanceResponseFromSatoshi,
|
|
35
|
-
} from "./balanceObjectFromSatoshi.js";
|
|
36
31
|
export * from "./sumUtxoValue.js";
|
|
37
32
|
export { decodeHeader } from "./header.js";
|
|
33
|
+
export * from "./checkUtxos.js";
|
|
@@ -12,27 +12,21 @@ import { sanitizeUnit } from "../util/sanitizeUnit.js";
|
|
|
12
12
|
* @returns a promise to the value in the unit of account given by rawUnit
|
|
13
13
|
*/
|
|
14
14
|
export async function satoshiToAmount(
|
|
15
|
-
value:
|
|
15
|
+
value: bigint,
|
|
16
16
|
rawUnit: any
|
|
17
17
|
): Promise<number> {
|
|
18
18
|
const unit = sanitizeUnit(rawUnit);
|
|
19
19
|
switch (unit) {
|
|
20
20
|
case UnitEnum.BCH:
|
|
21
|
-
return value / bchParam.subUnits;
|
|
22
|
-
case UnitEnum.SATOSHI:
|
|
23
|
-
return value;
|
|
21
|
+
return Number(value) / Number(bchParam.subUnits);
|
|
24
22
|
case UnitEnum.SAT:
|
|
25
|
-
return value;
|
|
26
|
-
case UnitEnum.SATS:
|
|
27
|
-
return value;
|
|
28
|
-
case UnitEnum.SATOSHIS:
|
|
29
|
-
return value;
|
|
23
|
+
return Number(value);
|
|
30
24
|
default:
|
|
31
25
|
const Currency_over_BCH = await ExchangeRate.get(rawUnit);
|
|
32
26
|
// truncate currency amounts to fixed precision (2),
|
|
33
27
|
// then return the fixed value string as a float.
|
|
34
28
|
const currencyValue = Number(
|
|
35
|
-
value * (Currency_over_BCH / bchParam.subUnits)
|
|
29
|
+
Number(value) * (Currency_over_BCH / Number(bchParam.subUnits))
|
|
36
30
|
).toFixed(2);
|
|
37
31
|
return Number.parseFloat(currencyValue);
|
|
38
32
|
}
|
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
SendRequest,
|
|
4
4
|
TokenSendRequest,
|
|
5
5
|
} from "../wallet/model.js";
|
|
6
|
-
import { amountInSatoshi } from "./amountInSatoshi.js";
|
|
7
6
|
|
|
8
7
|
// This function sums a list of send request objects
|
|
9
8
|
export async function sumSendRequestAmounts(
|
|
@@ -13,10 +12,10 @@ export async function sumSendRequestAmounts(
|
|
|
13
12
|
const balanceArray: (BigInt | Error)[] = await Promise.all(
|
|
14
13
|
requests.map(async (r: SendRequest | TokenSendRequest | OpReturnData) => {
|
|
15
14
|
if (r instanceof SendRequest) {
|
|
16
|
-
return
|
|
15
|
+
return r.value;
|
|
17
16
|
} else if (r instanceof TokenSendRequest) {
|
|
18
|
-
return
|
|
19
|
-
} else return
|
|
17
|
+
return r.value || 1000n;
|
|
18
|
+
} else return 0n;
|
|
20
19
|
})
|
|
21
20
|
);
|
|
22
21
|
const balance = balanceArray.reduce(sumBalance, BigInt(0));
|
package/src/util/sumUtxoValue.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
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:
|
|
5
|
+
const balanceArray: bigint[] = utxos.map((o: Utxo) => {
|
|
6
6
|
return o.satoshis;
|
|
7
7
|
});
|
|
8
|
-
const balance = balanceArray.reduce((a:
|
|
8
|
+
const balance = balanceArray.reduce((a: bigint, b: bigint) => a + b, 0n);
|
|
9
9
|
return balance;
|
|
10
10
|
} else {
|
|
11
|
-
return
|
|
11
|
+
return 0n;
|
|
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);
|