mainnet-js 3.0.0-next.0 → 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-3.0.0-next.0.js → mainnet-3.0.0-next.1.js} +21 -41
- package/dist/module/cache/walletCache.d.ts +10 -4
- package/dist/module/cache/walletCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.js +12 -13
- package/dist/module/cache/walletCache.js.map +1 -1
- 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/getHistory.d.ts.map +1 -1
- package/dist/module/history/getHistory.js +0 -30
- package/dist/module/history/getHistory.js.map +1 -1
- package/dist/module/interface.d.ts +1 -2
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +1 -1
- package/dist/module/network/ElectrumNetworkProvider.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 +9 -9
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +11 -13
- 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/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/index.d.ts +2 -3
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +2 -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 +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 +10 -12
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +18 -50
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/Util.js +1 -1
- package/dist/module/wallet/Util.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +2 -3
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +12 -16
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +4 -8
- package/dist/module/wallet/model.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cache/walletCache.ts +23 -36
- package/src/chain.ts +1 -1
- package/src/constant.ts +1 -1
- package/src/enum.ts +0 -6
- package/src/history/getHistory.test.ts +20 -34
- package/src/history/getHistory.ts +0 -32
- package/src/interface.ts +1 -2
- package/src/network/Connection.test.ts +3 -3
- package/src/network/ElectrumNetworkProvider.ts +1 -1
- package/src/network/Rpc.test.ts +2 -3
- 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 +24 -23
- 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/convert.ts +18 -0
- package/src/util/index.ts +2 -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 +3 -3
- package/src/wallet/Base.ts +28 -92
- package/src/wallet/Cashtokens.test.headless.js +11 -11
- package/src/wallet/Cashtokens.test.ts +36 -37
- package/src/wallet/HDWallet.test.ts +211 -68
- package/src/wallet/Util.ts +1 -1
- package/src/wallet/Wif.test.ts +108 -133
- package/src/wallet/interface.ts +2 -3
- package/src/wallet/model.test.ts +2 -5
- package/src/wallet/model.ts +15 -30
- 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
package/src/constant.ts
CHANGED
package/src/enum.ts
CHANGED
|
@@ -10,7 +10,6 @@ export enum NetworkEnum {
|
|
|
10
10
|
Mainnet = <any>"mainnet",
|
|
11
11
|
Testnet = <any>"testnet",
|
|
12
12
|
Regtest = <any>"regtest",
|
|
13
|
-
Simtest = <any>"simtest",
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
export const networkPrefixMap = {
|
|
@@ -30,11 +29,6 @@ const literal = <L extends string>(l: L): L => l;
|
|
|
30
29
|
export const UnitEnum = {
|
|
31
30
|
BCH: literal("bch"),
|
|
32
31
|
USD: literal("usd"),
|
|
33
|
-
BIT: literal("bit"),
|
|
34
|
-
BITS: literal("bits"),
|
|
35
32
|
SAT: literal("sat"),
|
|
36
|
-
SATS: literal("sats"),
|
|
37
|
-
SATOSHI: literal("satoshi"),
|
|
38
|
-
SATOSHIS: literal("satoshis"),
|
|
39
33
|
};
|
|
40
34
|
export type UnitEnum = typeof UnitEnum[keyof typeof UnitEnum];
|
|
@@ -3,6 +3,7 @@ import { WalletTypeEnum } from "../wallet/enum";
|
|
|
3
3
|
import { createWallet } from "../wallet/createWallet";
|
|
4
4
|
import { mine } from "../mine";
|
|
5
5
|
import { encodeCashAddress } from "@bitauth/libauth";
|
|
6
|
+
import { toBch } from "../util";
|
|
6
7
|
|
|
7
8
|
test("Should get miner history", async () => {
|
|
8
9
|
const alice = await RegTestWallet.fromWIF(process.env.PRIVATE_WIF!);
|
|
@@ -28,33 +29,29 @@ test("Should get an address history", async () => {
|
|
|
28
29
|
let sendResponse = await alice.send([
|
|
29
30
|
{
|
|
30
31
|
cashaddr: bob.cashaddr!,
|
|
31
|
-
value:
|
|
32
|
-
unit: "satoshis",
|
|
32
|
+
value: 31000n,
|
|
33
33
|
},
|
|
34
34
|
{
|
|
35
35
|
cashaddr: charlie.cashaddr!,
|
|
36
|
-
value:
|
|
37
|
-
unit: "satoshis",
|
|
36
|
+
value: 41000n,
|
|
38
37
|
},
|
|
39
38
|
]);
|
|
40
39
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
41
40
|
await bob.send([
|
|
42
41
|
{
|
|
43
42
|
cashaddr: charlie.cashaddr!,
|
|
44
|
-
value:
|
|
45
|
-
unit: "satoshis",
|
|
43
|
+
value: 2100n,
|
|
46
44
|
},
|
|
47
45
|
]);
|
|
48
46
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
|
|
49
47
|
await bob.send([
|
|
50
48
|
{
|
|
51
49
|
cashaddr: alice.cashaddr!,
|
|
52
|
-
value:
|
|
53
|
-
unit: "satoshis",
|
|
50
|
+
value: 2100n,
|
|
54
51
|
},
|
|
55
52
|
]);
|
|
56
53
|
expect(sendResponse!.txId!.length).toBe(64);
|
|
57
|
-
expect(sendResponse.balance
|
|
54
|
+
expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
|
|
58
55
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
59
56
|
|
|
60
57
|
// Build Bob's wallet from a public address, check his balance.
|
|
@@ -110,28 +107,25 @@ test("Should get a history with multi-party sends", async () => {
|
|
|
110
107
|
let sendResponse = await alice.send([
|
|
111
108
|
{
|
|
112
109
|
cashaddr: bob.cashaddr!,
|
|
113
|
-
value:
|
|
114
|
-
unit: "satoshis",
|
|
110
|
+
value: 31000n,
|
|
115
111
|
},
|
|
116
112
|
]);
|
|
117
113
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
118
114
|
await bob.send([
|
|
119
115
|
{
|
|
120
116
|
cashaddr: charlie.cashaddr!,
|
|
121
|
-
value:
|
|
122
|
-
unit: "satoshis",
|
|
117
|
+
value: 2100n,
|
|
123
118
|
},
|
|
124
119
|
]);
|
|
125
120
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
|
|
126
121
|
await bob.send([
|
|
127
122
|
{
|
|
128
123
|
cashaddr: alice.cashaddr!,
|
|
129
|
-
value:
|
|
130
|
-
unit: "satoshis",
|
|
124
|
+
value: 2100n,
|
|
131
125
|
},
|
|
132
126
|
]);
|
|
133
127
|
expect(sendResponse!.txId!.length).toBe(64);
|
|
134
|
-
expect(sendResponse.balance
|
|
128
|
+
expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
|
|
135
129
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
|
|
136
130
|
|
|
137
131
|
// Build Bob's wallet from a public address, check his balance.
|
|
@@ -190,31 +184,27 @@ test("Should cut results with a longer history to given count", async () => {
|
|
|
190
184
|
let sendResponse = await alice.send([
|
|
191
185
|
{
|
|
192
186
|
cashaddr: bob.cashaddr!,
|
|
193
|
-
value:
|
|
194
|
-
unit: "satoshis",
|
|
187
|
+
value: 31000n,
|
|
195
188
|
},
|
|
196
189
|
]);
|
|
197
190
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
198
191
|
await bob.send([
|
|
199
192
|
{
|
|
200
193
|
cashaddr: charlie.cashaddr!,
|
|
201
|
-
value:
|
|
202
|
-
unit: "satoshis",
|
|
194
|
+
value: 2100n,
|
|
203
195
|
},
|
|
204
196
|
{
|
|
205
197
|
cashaddr: alice.cashaddr!,
|
|
206
|
-
value:
|
|
207
|
-
unit: "satoshis",
|
|
198
|
+
value: 2100n,
|
|
208
199
|
},
|
|
209
200
|
{
|
|
210
201
|
cashaddr: alice.cashaddr!,
|
|
211
|
-
value:
|
|
212
|
-
unit: "satoshis",
|
|
202
|
+
value: 2100n,
|
|
213
203
|
},
|
|
214
204
|
]);
|
|
215
205
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
|
|
216
206
|
expect(sendResponse!.txId!.length).toBe(64);
|
|
217
|
-
expect(sendResponse.balance
|
|
207
|
+
expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
|
|
218
208
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
219
209
|
|
|
220
210
|
// Build Bob's wallet from a public address, check his balance.
|
|
@@ -258,30 +248,26 @@ test("Should handle input and fee from many utxos", async () => {
|
|
|
258
248
|
let sendResponse = await alice.send([
|
|
259
249
|
{
|
|
260
250
|
cashaddr: bob.cashaddr!,
|
|
261
|
-
value:
|
|
262
|
-
unit: "satoshis",
|
|
251
|
+
value: 600n,
|
|
263
252
|
},
|
|
264
253
|
{
|
|
265
254
|
cashaddr: bob.cashaddr!,
|
|
266
|
-
value:
|
|
267
|
-
unit: "satoshis",
|
|
255
|
+
value: 600n,
|
|
268
256
|
},
|
|
269
257
|
{
|
|
270
258
|
cashaddr: bob.cashaddr!,
|
|
271
|
-
value:
|
|
272
|
-
unit: "satoshis",
|
|
259
|
+
value: 600n,
|
|
273
260
|
},
|
|
274
261
|
{
|
|
275
262
|
cashaddr: bob.cashaddr!,
|
|
276
|
-
value:
|
|
277
|
-
unit: "satoshis",
|
|
263
|
+
value: 600n,
|
|
278
264
|
},
|
|
279
265
|
]);
|
|
280
266
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
281
267
|
await bob.sendMax(charlie.cashaddr!);
|
|
282
268
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
|
|
283
269
|
expect(sendResponse!.txId!.length).toBe(64);
|
|
284
|
-
expect(sendResponse.balance
|
|
270
|
+
expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
|
|
285
271
|
await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
|
|
286
272
|
|
|
287
273
|
// Build Bob's wallet from a public address, check his balance.
|
|
@@ -22,38 +22,6 @@ type Transaction = TransactionCommon & {
|
|
|
22
22
|
hash: string;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
// export type AddressTxI = TxI & {
|
|
26
|
-
// address: string;
|
|
27
|
-
// }
|
|
28
|
-
|
|
29
|
-
// export const getAddressHistory = async ({
|
|
30
|
-
// address,
|
|
31
|
-
// provider,
|
|
32
|
-
// unit = "sat",
|
|
33
|
-
// fromHeight = 0,
|
|
34
|
-
// toHeight = -1,
|
|
35
|
-
// start = 0,
|
|
36
|
-
// count = -1,
|
|
37
|
-
// }: {
|
|
38
|
-
// address: string;
|
|
39
|
-
// provider: NetworkProvider;
|
|
40
|
-
// unit?: UnitEnum;
|
|
41
|
-
// fromHeight?: number;
|
|
42
|
-
// toHeight?: number;
|
|
43
|
-
// start?: number;
|
|
44
|
-
// count?: number;
|
|
45
|
-
// }): Promise<TransactionHistoryItem[]> => {
|
|
46
|
-
// return getHistory({
|
|
47
|
-
// addresses: [address],
|
|
48
|
-
// provider,
|
|
49
|
-
// unit,
|
|
50
|
-
// fromHeight,
|
|
51
|
-
// toHeight,
|
|
52
|
-
// start,
|
|
53
|
-
// count,
|
|
54
|
-
// });
|
|
55
|
-
// }
|
|
56
|
-
|
|
57
25
|
export const getHistory = async ({
|
|
58
26
|
addresses,
|
|
59
27
|
provider,
|
package/src/interface.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type Network = typeof Network[keyof typeof Network];
|
|
|
18
18
|
export interface Utxo {
|
|
19
19
|
txid: string;
|
|
20
20
|
vout: number;
|
|
21
|
-
satoshis:
|
|
21
|
+
satoshis: bigint;
|
|
22
22
|
height?: number;
|
|
23
23
|
coinbase?: boolean;
|
|
24
24
|
token?: TokenI;
|
|
@@ -28,7 +28,6 @@ export interface Utxo {
|
|
|
28
28
|
export interface UtxoId {
|
|
29
29
|
txid: string;
|
|
30
30
|
vout: number;
|
|
31
|
-
satoshis: number;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
export interface ElectrumBalanceI {
|
|
@@ -14,7 +14,7 @@ test("Should connect to mainnet", async () => {
|
|
|
14
14
|
|
|
15
15
|
let wallet = await Wallet.newRandom();
|
|
16
16
|
expect(wallet.provider == globalThis.BCH).toBeTruthy();
|
|
17
|
-
expect(await wallet.getBalance(
|
|
17
|
+
expect(await wallet.getBalance()).toBe(0n);
|
|
18
18
|
|
|
19
19
|
let height = await globalThis.BCH.getBlockHeight();
|
|
20
20
|
expect(height).toBeGreaterThan(5000);
|
|
@@ -23,7 +23,7 @@ test("Should connect to mainnet", async () => {
|
|
|
23
23
|
test("Should use global provider when creating testnet wallet", async () => {
|
|
24
24
|
let wallet = await TestNetWallet.newRandom();
|
|
25
25
|
expect(wallet.provider == globalThis.tBCH).toBeTruthy();
|
|
26
|
-
expect(await wallet.getBalance(
|
|
26
|
+
expect(await wallet.getBalance()).toBe(0n);
|
|
27
27
|
|
|
28
28
|
let height = await globalThis.tBCH.getBlockHeight();
|
|
29
29
|
expect(height).toBeGreaterThan(114);
|
|
@@ -34,7 +34,7 @@ test.skip("Should lower overhead in creating wallets", async () => {
|
|
|
34
34
|
for (let i = 0; i < 100; i++) {
|
|
35
35
|
let wallet = await RegTestWallet.newRandom();
|
|
36
36
|
expect(wallet.provider == globalThis.rBCH).toBeTruthy();
|
|
37
|
-
expect(await wallet.getBalance(
|
|
37
|
+
expect(await wallet.getBalance()).toBe(0n);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
let height = await globalThis.rBCH.getBlockHeight();
|
package/src/network/Rpc.test.ts
CHANGED
|
@@ -73,7 +73,7 @@ describe("Rpc tests", () => {
|
|
|
73
73
|
|
|
74
74
|
let result = false;
|
|
75
75
|
aliceWallet.watchBalance((balance) => {
|
|
76
|
-
expect(balance
|
|
76
|
+
expect(balance).toBeGreaterThan(0);
|
|
77
77
|
result = true;
|
|
78
78
|
// stop watching
|
|
79
79
|
return true;
|
|
@@ -105,8 +105,7 @@ describe("Rpc tests", () => {
|
|
|
105
105
|
await aliceWallet.send([
|
|
106
106
|
{
|
|
107
107
|
cashaddr: bobWallet.cashaddr!,
|
|
108
|
-
value:
|
|
109
|
-
unit: "satoshis",
|
|
108
|
+
value: 1000n,
|
|
110
109
|
},
|
|
111
110
|
]);
|
|
112
111
|
|
|
@@ -15,9 +15,9 @@ export async function getRelayFeeCache(provider: NetworkProvider) {
|
|
|
15
15
|
relayFeePerKbInCoins;
|
|
16
16
|
}
|
|
17
17
|
if (typeof relayFeePerKbInCoins === "number") {
|
|
18
|
-
return Math.round(relayFeePerKbInCoins * bchParam.subUnits) / 1000;
|
|
18
|
+
return Math.round(relayFeePerKbInCoins * Number(bchParam.subUnits)) / 1000;
|
|
19
19
|
} else {
|
|
20
20
|
console.warn("Couldn't get min relay fee, using default instead");
|
|
21
|
-
return Math.round(0.00001 * bchParam.subUnits) / 1000;
|
|
21
|
+
return Math.round(0.00001 * Number(bchParam.subUnits)) / 1000;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { RegTestWallet } from "../wallet/Wif";
|
|
4
|
-
import { ExchangeRate, getRateFromExchange } from "./ExchangeRate";
|
|
5
|
-
import { delay } from "../util/delay";
|
|
1
|
+
import { setupFetchMock } from "../test/fetch";
|
|
2
|
+
import { ExchangeRate } from "./ExchangeRate";
|
|
6
3
|
import { initProviders, disconnectProviders } from "../network";
|
|
7
4
|
import { Config } from "../config";
|
|
8
5
|
|
|
@@ -25,45 +22,6 @@ describe("Exchange rate tests", () => {
|
|
|
25
22
|
expect(rate).toBe(1337.42);
|
|
26
23
|
});
|
|
27
24
|
|
|
28
|
-
test("Test watchBalanceUsd", async () => {
|
|
29
|
-
setupFetchMock("https://markets.api.bitcoin.com/live/bitcoin", {
|
|
30
|
-
data: {
|
|
31
|
-
BCH: 1337.42,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
|
|
36
|
-
const bob = await RegTestWallet.newRandom();
|
|
37
|
-
const balance = (await alice.getBalance()) as BalanceResponse;
|
|
38
|
-
let cbCounter = 0;
|
|
39
|
-
const cancelWatchFn = await alice.watchBalanceUsd(async (newBalance) => {
|
|
40
|
-
cbCounter++;
|
|
41
|
-
if (cbCounter === 1) {
|
|
42
|
-
expect(newBalance.usd!).toBeGreaterThan(balance.usd!);
|
|
43
|
-
}
|
|
44
|
-
}, 3000);
|
|
45
|
-
|
|
46
|
-
setupFetchMock("https://markets.api.bitcoin.com/live/bitcoin", {
|
|
47
|
-
data: {
|
|
48
|
-
BCH: 31337.42,
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
await delay(3000);
|
|
53
|
-
|
|
54
|
-
await alice.send({
|
|
55
|
-
cashaddr: bob.getDepositAddress(),
|
|
56
|
-
value: 10000,
|
|
57
|
-
unit: "sat",
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
await delay(3000);
|
|
61
|
-
|
|
62
|
-
removeFetchMock("https://markets.api.bitcoin.com/live/bitcoin");
|
|
63
|
-
expect(cbCounter).toBe(2);
|
|
64
|
-
await cancelWatchFn();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
25
|
test("Test other currencies", async () => {
|
|
68
26
|
const eurRate = await ExchangeRate.get("eur");
|
|
69
27
|
expect(eurRate).toBeGreaterThan(0);
|
package/src/rate/ExchangeRate.ts
CHANGED
|
@@ -42,7 +42,7 @@ export class ExchangeRate {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
static async getRateFromLocalStorage(
|
|
45
|
-
symbol,
|
|
45
|
+
symbol: string,
|
|
46
46
|
useCache = true
|
|
47
47
|
): Promise<number> {
|
|
48
48
|
if (!useCache) {
|
|
@@ -136,4 +136,7 @@ export async function getRateFromExchange(symbol: string): Promise<number> {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// do not await and do not throw in case we are offline
|
|
139
|
-
|
|
139
|
+
// this promise can be used to warm up the cache
|
|
140
|
+
export const ExchageRatePromise = ExchangeRate.get(Config.DefaultCurrency)
|
|
141
|
+
.then((result) => result)
|
|
142
|
+
.catch((error: Error) => error);
|
package/src/transaction/Wif.ts
CHANGED
|
@@ -26,11 +26,10 @@ import {
|
|
|
26
26
|
SourceOutput,
|
|
27
27
|
TokenSendRequest,
|
|
28
28
|
} from "../wallet/model.js";
|
|
29
|
-
import { amountInSatoshi } from "../util/amountInSatoshi.js";
|
|
30
29
|
import { sumSendRequestAmounts } from "../util/sumSendRequestAmounts.js";
|
|
31
30
|
import { sumUtxoValue } from "../util/sumUtxoValue.js";
|
|
32
31
|
import { FeePaidByEnum } from "../wallet/enum.js";
|
|
33
|
-
import {
|
|
32
|
+
import { WalletCache } from "../cache/walletCache.js";
|
|
34
33
|
|
|
35
34
|
export const placeholderPrivateKey =
|
|
36
35
|
"0000000000000000000000000000000000000000000000000000000000000001";
|
|
@@ -41,7 +40,7 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
41
40
|
inputs,
|
|
42
41
|
outputs,
|
|
43
42
|
signingKey,
|
|
44
|
-
fee =
|
|
43
|
+
fee = 0n,
|
|
45
44
|
discardChange = false,
|
|
46
45
|
feePaidBy = FeePaidByEnum.change,
|
|
47
46
|
changeAddress = "",
|
|
@@ -50,11 +49,11 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
50
49
|
inputs: Utxo[];
|
|
51
50
|
outputs: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
52
51
|
signingKey?: Uint8Array;
|
|
53
|
-
fee?:
|
|
52
|
+
fee?: bigint;
|
|
54
53
|
discardChange?: boolean;
|
|
55
54
|
feePaidBy?: FeePaidByEnum;
|
|
56
55
|
changeAddress?: string;
|
|
57
|
-
walletCache?:
|
|
56
|
+
walletCache?: WalletCache;
|
|
58
57
|
}) {
|
|
59
58
|
if (!signingKey) {
|
|
60
59
|
throw new Error("Missing signing key when building transaction");
|
|
@@ -135,7 +134,7 @@ export function prepareInputs({
|
|
|
135
134
|
AuthenticationProgramStateCommon
|
|
136
135
|
>;
|
|
137
136
|
signingKey: Uint8Array;
|
|
138
|
-
walletCache?:
|
|
137
|
+
walletCache?: WalletCache;
|
|
139
138
|
}) {
|
|
140
139
|
const preparedInputs: any[] = [];
|
|
141
140
|
const sourceOutputs: any[] = [];
|
|
@@ -166,7 +165,7 @@ export function prepareInputs({
|
|
|
166
165
|
: undefined,
|
|
167
166
|
};
|
|
168
167
|
const key =
|
|
169
|
-
walletCache?.
|
|
168
|
+
walletCache?.get(i.address)?.privateKey ??
|
|
170
169
|
(signingKey?.length ? signingKey : Uint8Array.from(Array(32)));
|
|
171
170
|
const newInput = {
|
|
172
171
|
outpointIndex: utxoIndex,
|
|
@@ -231,7 +230,7 @@ export async function prepareOutputs(
|
|
|
231
230
|
if (typeof outputLockingBytecode === "string")
|
|
232
231
|
throw new Error(outputLockingBytecode);
|
|
233
232
|
|
|
234
|
-
const sendAmount =
|
|
233
|
+
const sendAmount = Number(output.value);
|
|
235
234
|
if (sendAmount % 1 !== 0) {
|
|
236
235
|
throw Error(
|
|
237
236
|
`Cannot send ${sendAmount} satoshis, (fractional sats do not exist, yet), please use an integer number.`
|
|
@@ -451,7 +450,7 @@ export async function getFeeAmountSimple({
|
|
|
451
450
|
relayFeePerByteInSatoshi: number;
|
|
452
451
|
feePaidBy: FeePaidByEnum;
|
|
453
452
|
discardChange?: boolean;
|
|
454
|
-
}) {
|
|
453
|
+
}): Promise<bigint> {
|
|
455
454
|
const inputSizeP2pkh = 148;
|
|
456
455
|
const outputSizeP2pkh = 34;
|
|
457
456
|
|
|
@@ -468,10 +467,8 @@ export async function getFeeAmountSimple({
|
|
|
468
467
|
0
|
|
469
468
|
);
|
|
470
469
|
|
|
471
|
-
const outputSize = (sendRequest) => {
|
|
472
|
-
if (sendRequest.hasOwnProperty("
|
|
473
|
-
return outputSizeP2pkh;
|
|
474
|
-
} else if (sendRequest.hasOwnProperty("tokenId")) {
|
|
470
|
+
const outputSize = (sendRequest: SendRequestType) => {
|
|
471
|
+
if (sendRequest.hasOwnProperty("tokenId")) {
|
|
475
472
|
const tokenRequest = sendRequest as TokenSendRequest;
|
|
476
473
|
return (
|
|
477
474
|
outputSizeP2pkh +
|
|
@@ -482,17 +479,19 @@ export async function getFeeAmountSimple({
|
|
|
482
479
|
);
|
|
483
480
|
} else if (sendRequest.hasOwnProperty("buffer")) {
|
|
484
481
|
return 9 + (sendRequest as OpReturnData).buffer.length;
|
|
482
|
+
} else {
|
|
483
|
+
return outputSizeP2pkh;
|
|
485
484
|
}
|
|
486
|
-
|
|
487
|
-
return 0;
|
|
488
485
|
};
|
|
489
486
|
|
|
490
487
|
const outputTotalSize =
|
|
491
488
|
sendRequests.reduce((prev, curr) => prev + outputSize(curr), 0) +
|
|
492
489
|
(discardChange ? 0 : outputSizeP2pkh);
|
|
493
490
|
|
|
494
|
-
return
|
|
495
|
-
(
|
|
491
|
+
return BigInt(
|
|
492
|
+
Math.ceil(
|
|
493
|
+
(inputTotalSize + outputTotalSize + 16) * relayFeePerByteInSatoshi
|
|
494
|
+
)
|
|
496
495
|
);
|
|
497
496
|
}
|
|
498
497
|
|
|
@@ -512,7 +511,7 @@ export async function getFeeAmount({
|
|
|
512
511
|
relayFeePerByteInSatoshi: number;
|
|
513
512
|
feePaidBy: FeePaidByEnum;
|
|
514
513
|
discardChange?: boolean;
|
|
515
|
-
walletCache?:
|
|
514
|
+
walletCache?: WalletCache;
|
|
516
515
|
}) {
|
|
517
516
|
// build transaction
|
|
518
517
|
if (utxos) {
|
|
@@ -522,14 +521,16 @@ export async function getFeeAmount({
|
|
|
522
521
|
inputs: utxos,
|
|
523
522
|
outputs: sendRequests,
|
|
524
523
|
signingKey: placeholderPrivateKeyBin,
|
|
525
|
-
fee:
|
|
524
|
+
fee: 0n, //DUST_UTXO_THRESHOLD
|
|
526
525
|
discardChange: discardChange ?? false,
|
|
527
526
|
feePaidBy,
|
|
528
527
|
changeAddress: "",
|
|
529
528
|
walletCache,
|
|
530
529
|
});
|
|
531
530
|
|
|
532
|
-
return
|
|
531
|
+
return BigInt(
|
|
532
|
+
Math.ceil(draftTransaction.length * relayFeePerByteInSatoshi + 1)
|
|
533
|
+
);
|
|
533
534
|
} else {
|
|
534
535
|
throw Error(
|
|
535
536
|
"The available inputs in the wallet cannot satisfy this send request"
|
|
@@ -542,7 +543,7 @@ export async function buildEncodedTransaction({
|
|
|
542
543
|
inputs,
|
|
543
544
|
outputs,
|
|
544
545
|
signingKey,
|
|
545
|
-
fee =
|
|
546
|
+
fee = 0n,
|
|
546
547
|
discardChange = false,
|
|
547
548
|
feePaidBy = FeePaidByEnum.change,
|
|
548
549
|
changeAddress = "",
|
|
@@ -552,12 +553,12 @@ export async function buildEncodedTransaction({
|
|
|
552
553
|
inputs: Utxo[];
|
|
553
554
|
outputs: Array<SendRequest | TokenSendRequest | OpReturnData>;
|
|
554
555
|
signingKey: Uint8Array;
|
|
555
|
-
fee?:
|
|
556
|
+
fee?: bigint;
|
|
556
557
|
discardChange?: boolean;
|
|
557
558
|
feePaidBy?: FeePaidByEnum;
|
|
558
559
|
changeAddress?: string;
|
|
559
560
|
buildUnsigned?: boolean;
|
|
560
|
-
walletCache?:
|
|
561
|
+
walletCache?: WalletCache;
|
|
561
562
|
}) {
|
|
562
563
|
const { transaction, sourceOutputs } = await buildP2pkhNonHdTransaction({
|
|
563
564
|
inputs,
|