mainnet-js 3.1.7 → 4.0.0-next.10
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/module/cache/IndexedDbCache.d.ts +1 -1
- package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
- package/dist/module/cache/MemoryCache.d.ts +1 -1
- package/dist/module/cache/MemoryCache.d.ts.map +1 -1
- package/dist/module/cache/WebStorageCache.d.ts +1 -1
- package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.d.ts +16 -6
- package/dist/module/cache/walletCache.d.ts.map +1 -1
- package/dist/module/cache/walletCache.js +92 -34
- package/dist/module/cache/walletCache.js.map +1 -1
- package/dist/module/cli.js +1 -2
- package/dist/module/cli.js.map +1 -1
- package/dist/module/db/index.d.ts +1 -1
- package/dist/module/db/index.d.ts.map +1 -1
- package/dist/module/db/index.js +1 -1
- package/dist/module/db/index.js.map +1 -1
- package/dist/module/enum.d.ts +1 -1
- package/dist/module/enum.d.ts.map +1 -1
- package/dist/module/history/getHistory.d.ts +1 -1
- package/dist/module/history/getHistory.d.ts.map +1 -1
- package/dist/module/history/getHistory.js +3 -3
- package/dist/module/history/getHistory.js.map +1 -1
- package/dist/module/index.d.ts +25 -22
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +33 -26
- package/dist/module/index.js.map +1 -1
- package/dist/module/interface.d.ts +12 -2
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/libauth.d.ts +1 -1
- package/dist/module/libauth.d.ts.map +1 -1
- package/dist/module/libauth.js +1 -1
- package/dist/module/libauth.js.map +1 -1
- package/dist/module/mine/mine.d.ts +2 -7
- package/dist/module/mine/mine.d.ts.map +1 -1
- package/dist/module/mine/mine.js +6 -27
- package/dist/module/mine/mine.js.map +1 -1
- package/dist/module/network/Connection.d.ts +1 -12
- package/dist/module/network/Connection.d.ts.map +1 -1
- package/dist/module/network/Connection.js +24 -33
- package/dist/module/network/Connection.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +91 -93
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/MockNetworkProvider.d.ts +28 -0
- package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
- package/dist/module/network/MockNetworkProvider.js +74 -0
- package/dist/module/network/MockNetworkProvider.js.map +1 -0
- package/dist/module/network/NetworkProvider.d.ts +9 -2
- package/dist/module/network/NetworkProvider.d.ts.map +1 -1
- package/dist/module/network/configuration.d.ts +2 -4
- package/dist/module/network/configuration.d.ts.map +1 -1
- package/dist/module/network/configuration.js +25 -50
- package/dist/module/network/configuration.js.map +1 -1
- package/dist/module/network/constant.d.ts +7 -7
- package/dist/module/network/constant.d.ts.map +1 -1
- package/dist/module/network/constant.js +21 -24
- package/dist/module/network/constant.js.map +1 -1
- package/dist/module/network/default.d.ts +5 -3
- package/dist/module/network/default.d.ts.map +1 -1
- package/dist/module/network/default.js +29 -51
- package/dist/module/network/default.js.map +1 -1
- package/dist/module/network/index.d.ts +4 -4
- package/dist/module/network/index.d.ts.map +1 -1
- package/dist/module/network/index.js +2 -2
- package/dist/module/network/index.js.map +1 -1
- package/dist/module/network/interface.d.ts +0 -6
- package/dist/module/network/interface.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.d.ts +1 -1
- package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
- package/dist/module/rate/ExchangeRate.js +13 -7
- package/dist/module/rate/ExchangeRate.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +3 -3
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +4 -4
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/transaction/allocateFee.d.ts +1 -1
- package/dist/module/transaction/allocateFee.d.ts.map +1 -1
- package/dist/module/transaction/allocateFee.js +2 -2
- package/dist/module/transaction/allocateFee.js.map +1 -1
- 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/getAddrsByXpubKey.js +1 -1
- package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
- package/dist/module/util/getXPubKey.js +1 -1
- package/dist/module/util/getXPubKey.js.map +1 -1
- package/dist/module/util/hd.d.ts +2 -0
- package/dist/module/util/hd.d.ts.map +1 -1
- package/dist/module/util/hd.js +41 -0
- package/dist/module/util/hd.js.map +1 -1
- package/dist/module/util/index.d.ts +7 -9
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +7 -9
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/transaction.js +1 -1
- package/dist/module/util/transaction.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +13 -9
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +54 -58
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts.map +1 -1
- package/dist/module/wallet/HDWallet.js +19 -38
- package/dist/module/wallet/HDWallet.js.map +1 -1
- package/dist/module/wallet/Util.d.ts +4 -22
- package/dist/module/wallet/Util.d.ts.map +1 -1
- package/dist/module/wallet/Util.js +75 -102
- package/dist/module/wallet/Util.js.map +1 -1
- package/dist/module/wallet/Watch.d.ts +24 -5
- package/dist/module/wallet/Watch.d.ts.map +1 -1
- package/dist/module/wallet/Watch.js +141 -17
- package/dist/module/wallet/Watch.js.map +1 -1
- package/dist/module/wallet/Wif.d.ts +2 -6
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +3 -69
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +1 -1
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +2 -1
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +3 -2
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +2 -1
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +1 -1
- package/dist/module/wallet/model.js.map +1 -1
- package/package.json +17 -29
- package/dist/index.html +0 -9
- package/dist/mainnet-3.1.7.js +0 -2066
- package/dist/module/network/util.d.ts +0 -3
- package/dist/module/network/util.d.ts.map +0 -1
- package/dist/module/network/util.js +0 -27
- package/dist/module/network/util.js.map +0 -1
- package/dist/module/test/expect.d.ts +0 -12
- package/dist/module/test/expect.d.ts.map +0 -1
- package/dist/module/test/expect.js +0 -47
- package/dist/module/test/expect.js.map +0 -1
- package/dist/module/test/fetch.d.ts +0 -3
- package/dist/module/test/fetch.d.ts.map +0 -1
- package/dist/module/test/fetch.js +0 -32
- package/dist/module/test/fetch.js.map +0 -1
- package/dist/module/util/randomBytes.d.ts +0 -2
- package/dist/module/util/randomBytes.d.ts.map +0 -1
- package/dist/module/util/randomBytes.js +0 -13
- package/dist/module/util/randomBytes.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/cache/IndexedDbCache.test.ts +0 -15
- package/src/cache/IndexedDbCache.ts +0 -172
- package/src/cache/MemoryCache.test.ts +0 -15
- package/src/cache/MemoryCache.ts +0 -32
- package/src/cache/WebStorageCache.test.ts +0 -15
- package/src/cache/WebStorageCache.ts +0 -38
- package/src/cache/index.ts +0 -2
- package/src/cache/interface.ts +0 -9
- package/src/cache/walletCache.ts +0 -254
- package/src/chain.ts +0 -3
- package/src/cli.ts +0 -32
- package/src/config.ts +0 -23
- package/src/constant.ts +0 -27
- package/src/db/ExchangeRateProvider.ts +0 -28
- package/src/db/StorageProvider.ts +0 -64
- package/src/db/index.ts +0 -2
- package/src/db/interface.ts +0 -11
- package/src/enum.ts +0 -34
- package/src/history/getHistory.test.ts +0 -290
- package/src/history/getHistory.ts +0 -411
- package/src/history/interface.ts +0 -24
- package/src/index.ts +0 -48
- package/src/interface.ts +0 -72
- package/src/libauth.ts +0 -11
- package/src/message/index.ts +0 -2
- package/src/message/interface.ts +0 -40
- package/src/message/signed.test.ts +0 -309
- package/src/message/signed.ts +0 -201
- package/src/mine/index.ts +0 -1
- package/src/mine/mine.test.ts +0 -10
- package/src/mine/mine.ts +0 -42
- package/src/network/Connection.test.ts +0 -51
- package/src/network/Connection.ts +0 -73
- package/src/network/ElectrumNetworkProvider.ts +0 -657
- package/src/network/NetworkProvider.ts +0 -180
- package/src/network/Rpc.test.ts +0 -130
- package/src/network/configuration.test.ts +0 -59
- package/src/network/configuration.ts +0 -72
- package/src/network/constant.ts +0 -43
- package/src/network/default.ts +0 -120
- package/src/network/electrum.test.ts +0 -28
- package/src/network/getRelayFeeCache.test.ts +0 -15
- package/src/network/getRelayFeeCache.ts +0 -23
- package/src/network/index.ts +0 -14
- package/src/network/interface.ts +0 -80
- package/src/network/util.test.ts +0 -24
- package/src/network/util.ts +0 -30
- package/src/rate/ExchangeRate.test.headless.js +0 -35
- package/src/rate/ExchangeRate.test.ts +0 -51
- package/src/rate/ExchangeRate.ts +0 -142
- package/src/test/expect.ts +0 -59
- package/src/test/fetch.ts +0 -39
- package/src/test/json.test.ts +0 -13
- package/src/transaction/Wif.ts +0 -680
- package/src/transaction/allocateFee.test.ts +0 -298
- package/src/transaction/allocateFee.ts +0 -149
- package/src/util/amountInSatoshi.test.ts +0 -27
- package/src/util/amountInSatoshi.ts +0 -33
- package/src/util/asSendRequestObject.ts +0 -81
- package/src/util/base64.test.ts +0 -39
- package/src/util/base64.ts +0 -12
- package/src/util/browserNotSupported.ts +0 -7
- package/src/util/checkForEmptySeed.ts +0 -9
- package/src/util/checkUtxos.ts +0 -29
- package/src/util/checkWifNetwork.ts +0 -24
- package/src/util/convert.test.ts +0 -46
- package/src/util/convert.ts +0 -50
- package/src/util/delay.ts +0 -3
- package/src/util/deriveCashaddr.test.ts +0 -164
- package/src/util/deriveCashaddr.ts +0 -143
- package/src/util/deriveLockscript.ts +0 -16
- package/src/util/deriveNetwork.ts +0 -19
- package/src/util/derivePublicKeyHash.test.ts +0 -55
- package/src/util/derivePublicKeyHash.ts +0 -64
- package/src/util/floor.test.ts +0 -21
- package/src/util/floor.ts +0 -4
- package/src/util/getAddrsByXpubKey.test.ts +0 -115
- package/src/util/getAddrsByXpubKey.ts +0 -86
- package/src/util/getRuntimePlatform.test.headless.js +0 -40
- package/src/util/getRuntimePlatform.test.ts +0 -5
- package/src/util/getRuntimePlatform.ts +0 -31
- package/src/util/getUsdRate.ts +0 -5
- package/src/util/getXPubKey.ts +0 -39
- package/src/util/hash160.test.ts +0 -18
- package/src/util/hash160.ts +0 -12
- package/src/util/hd.ts +0 -16
- package/src/util/header.test.ts +0 -34
- package/src/util/header.ts +0 -26
- package/src/util/index.ts +0 -33
- package/src/util/randomBytes.ts +0 -13
- package/src/util/randomInt.test.ts +0 -15
- package/src/util/randomInt.ts +0 -4
- package/src/util/sanitizeAddress.ts +0 -10
- package/src/util/sanitizeUnit.ts +0 -11
- package/src/util/satoshiToAmount.test.ts +0 -6
- package/src/util/satoshiToAmount.ts +0 -33
- package/src/util/sumSendRequestAmounts.ts +0 -34
- package/src/util/sumUtxoValue.ts +0 -27
- package/src/util/transaction.ts +0 -10
- package/src/wallet/Base.ts +0 -1563
- package/src/wallet/Cashtokens.test.headless.js +0 -730
- package/src/wallet/Cashtokens.test.ts +0 -1411
- package/src/wallet/HDWallet.test.ts +0 -1086
- package/src/wallet/HDWallet.ts +0 -992
- package/src/wallet/Util.test.ts +0 -134
- package/src/wallet/Util.ts +0 -191
- package/src/wallet/WalletCache.test.ts +0 -45
- package/src/wallet/Watch.ts +0 -441
- package/src/wallet/Wif.bip39.test.ts +0 -48
- package/src/wallet/Wif.test.ts +0 -1189
- package/src/wallet/Wif.ts +0 -687
- package/src/wallet/Wif.watchOnly.test.ts +0 -58
- package/src/wallet/createWallet.ts +0 -238
- package/src/wallet/enum.ts +0 -18
- package/src/wallet/interface.ts +0 -102
- package/src/wallet/model.test.ts +0 -24
- package/src/wallet/model.ts +0 -352
- package/tsconfig.browser.json +0 -11
- package/tsconfig.json +0 -33
- package/webpack.config.cjs +0 -132
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import { allocateFee, sortSendRequests } from "./allocateFee";
|
|
2
|
-
import { FeePaidByEnum } from "../wallet/enum";
|
|
3
|
-
import { asSendRequestObject } from "../util/asSendRequestObject";
|
|
4
|
-
import { SendRequest, disconnectProviders, initProviders } from "..";
|
|
5
|
-
import { RegTestWallet } from "../wallet/Wif";
|
|
6
|
-
|
|
7
|
-
beforeAll(async () => {
|
|
8
|
-
await initProviders();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
afterAll(async () => {
|
|
12
|
-
await disconnectProviders();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe("Fee tests", () => {
|
|
16
|
-
test("Should get the regtest wallet balance", async () => {
|
|
17
|
-
// Build Alice's wallet from Wallet Import Format string, send some sats
|
|
18
|
-
if (!process.env.ADDRESS) {
|
|
19
|
-
throw Error("Attempted to pass an empty address");
|
|
20
|
-
} else {
|
|
21
|
-
const funder = await RegTestWallet.fromId(
|
|
22
|
-
`wif:regtest:${process.env.PRIVATE_WIF!}`
|
|
23
|
-
);
|
|
24
|
-
const alice = await RegTestWallet.newRandom();
|
|
25
|
-
const bob = await RegTestWallet.newRandom();
|
|
26
|
-
const charlie = await RegTestWallet.newRandom();
|
|
27
|
-
const dave = await RegTestWallet.newRandom();
|
|
28
|
-
const edward = await RegTestWallet.newRandom();
|
|
29
|
-
await funder.send([
|
|
30
|
-
{
|
|
31
|
-
cashaddr: alice.cashaddr!,
|
|
32
|
-
value: 4500n,
|
|
33
|
-
},
|
|
34
|
-
]);
|
|
35
|
-
await alice.send(
|
|
36
|
-
[
|
|
37
|
-
{
|
|
38
|
-
cashaddr: bob.cashaddr!,
|
|
39
|
-
value: 549n,
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
cashaddr: charlie.cashaddr!,
|
|
43
|
-
value: 550n,
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
cashaddr: dave.cashaddr!,
|
|
47
|
-
value: 551n,
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
cashaddr: edward.cashaddr!,
|
|
51
|
-
value: 2552n,
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
{ feePaidBy: FeePaidByEnum.changeThenAny }
|
|
55
|
-
);
|
|
56
|
-
expect(await alice.getBalance()).toBe(0n);
|
|
57
|
-
expect(await bob.getBalance()).toBe(0n);
|
|
58
|
-
expect(await charlie.getBalance()).toBe(550n);
|
|
59
|
-
expect(await dave.getBalance()).toBe(551n);
|
|
60
|
-
expect(await edward.getBalance()).toBe(2552n);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("Expect first in to subtract fee from first", async () => {
|
|
65
|
-
let to = [
|
|
66
|
-
["alice", 2000n],
|
|
67
|
-
["bob", 2000n],
|
|
68
|
-
];
|
|
69
|
-
let fee = 1n;
|
|
70
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
71
|
-
let allocatedInputs = allocateFee(
|
|
72
|
-
requests,
|
|
73
|
-
fee,
|
|
74
|
-
FeePaidByEnum.first,
|
|
75
|
-
BigInt(0)
|
|
76
|
-
);
|
|
77
|
-
expect(allocatedInputs[0].value).toBeLessThan(2000n);
|
|
78
|
-
expect(allocatedInputs[1].value).toBe(2000n);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test("Expect last to subtract fee from last", async () => {
|
|
82
|
-
let to = [
|
|
83
|
-
["alice", 2000n],
|
|
84
|
-
["bob", 2000n],
|
|
85
|
-
];
|
|
86
|
-
let fee = 1n;
|
|
87
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
88
|
-
let allocatedInputs = allocateFee(
|
|
89
|
-
requests,
|
|
90
|
-
fee,
|
|
91
|
-
FeePaidByEnum.last,
|
|
92
|
-
BigInt(0)
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
expect(allocatedInputs[0].value).toBe(2000n);
|
|
96
|
-
expect(allocatedInputs[1].value).toBeLessThan(2000n);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("Expect all to allocate fees equally", async () => {
|
|
100
|
-
let to = [
|
|
101
|
-
["alice", 2000n],
|
|
102
|
-
["bob", 2000n],
|
|
103
|
-
["charlie", 2000n],
|
|
104
|
-
];
|
|
105
|
-
let fee = 3n;
|
|
106
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
107
|
-
|
|
108
|
-
let allocatedInputs = allocateFee(
|
|
109
|
-
requests,
|
|
110
|
-
fee,
|
|
111
|
-
FeePaidByEnum.any,
|
|
112
|
-
BigInt(0)
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
expect(allocatedInputs[0].value).toBe(1999n);
|
|
116
|
-
expect(allocatedInputs[1].value).toBe(1999n);
|
|
117
|
-
expect(allocatedInputs[2].value).toBe(1999n);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test("Expect all to allocate fees equally, taking dust result", async () => {
|
|
121
|
-
let to = [
|
|
122
|
-
["alice", 2000n],
|
|
123
|
-
["bob", 547n],
|
|
124
|
-
["charlie", 2000n],
|
|
125
|
-
];
|
|
126
|
-
let fee = 300n;
|
|
127
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
128
|
-
|
|
129
|
-
let allocatedInputs = allocateFee(
|
|
130
|
-
requests,
|
|
131
|
-
fee,
|
|
132
|
-
FeePaidByEnum.any,
|
|
133
|
-
BigInt(0)
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
expect(allocatedInputs[0].value).toBe(2000n);
|
|
137
|
-
expect(allocatedInputs[1].value).toBe(2000n);
|
|
138
|
-
expect(allocatedInputs.length).toBe(2);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test("Expect all to allocate fees equally, taking dust result output, dividing remainder", async () => {
|
|
142
|
-
let to = [
|
|
143
|
-
["alice", 2000n],
|
|
144
|
-
["bob", 547n],
|
|
145
|
-
["charlie", 2000n],
|
|
146
|
-
];
|
|
147
|
-
let fee = 647n;
|
|
148
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
149
|
-
|
|
150
|
-
let allocatedInputs = allocateFee(
|
|
151
|
-
requests,
|
|
152
|
-
fee,
|
|
153
|
-
FeePaidByEnum.any,
|
|
154
|
-
BigInt(0)
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
expect(allocatedInputs[0].value).toBe(1950n);
|
|
158
|
-
expect(allocatedInputs[1].value).toBe(1950n);
|
|
159
|
-
expect(allocatedInputs.length).toBe(2);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
test("Expect an odd fee to be applied to have remainder applied to first receipt", async () => {
|
|
163
|
-
let to = [
|
|
164
|
-
["alice", 2000n],
|
|
165
|
-
["bob", 2000n],
|
|
166
|
-
["charlie", 2000n],
|
|
167
|
-
];
|
|
168
|
-
let fee = 301n;
|
|
169
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
170
|
-
|
|
171
|
-
let allocatedInputs = allocateFee(
|
|
172
|
-
requests,
|
|
173
|
-
fee,
|
|
174
|
-
FeePaidByEnum.any,
|
|
175
|
-
BigInt(0)
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
expect(allocatedInputs[0].value).toBe(1899n);
|
|
179
|
-
expect(allocatedInputs[1].value).toBe(1900n);
|
|
180
|
-
expect(allocatedInputs[2].value).toBe(1900n);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test("Expect insufficient funds to error", async () => {
|
|
184
|
-
let to = [
|
|
185
|
-
["alice", 2000n],
|
|
186
|
-
["bob", 2000n],
|
|
187
|
-
["charlie", 2000n],
|
|
188
|
-
];
|
|
189
|
-
let fee = 7000n;
|
|
190
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
191
|
-
expect(() =>
|
|
192
|
-
allocateFee(requests, fee, FeePaidByEnum.changeThenAny, BigInt(999))
|
|
193
|
-
).toThrow("Insufficient funds for transaction given fee");
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
test("Expect dust amounts to error", async () => {
|
|
197
|
-
let to = [
|
|
198
|
-
["alice", 2000n],
|
|
199
|
-
["bob", 2000n],
|
|
200
|
-
["charlie", 2000n],
|
|
201
|
-
];
|
|
202
|
-
let fee = 1500n;
|
|
203
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
204
|
-
expect(() =>
|
|
205
|
-
allocateFee(requests, fee, FeePaidByEnum.first, BigInt(0))
|
|
206
|
-
).toThrow("Fee strategy would result in dust output");
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
test("Expect near-dust amounts not to error", async () => {
|
|
210
|
-
let to = [
|
|
211
|
-
["alice", 1000n],
|
|
212
|
-
["bob", 1000n],
|
|
213
|
-
["charlie", 1000n],
|
|
214
|
-
];
|
|
215
|
-
let fee = 1362n;
|
|
216
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
217
|
-
let result = allocateFee(requests, fee, FeePaidByEnum.any, BigInt(0));
|
|
218
|
-
expect(result[0].value).toBe(546n);
|
|
219
|
-
expect(result[1].value).toBe(546n);
|
|
220
|
-
expect(result[2].value).toBe(546n);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
test("Expect `any` to not consume change", async () => {
|
|
224
|
-
let to = [
|
|
225
|
-
["alice", 1000n],
|
|
226
|
-
["bob", 1000n],
|
|
227
|
-
["charlie", 1000n],
|
|
228
|
-
];
|
|
229
|
-
let fee = 1362n;
|
|
230
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
231
|
-
let result = allocateFee(requests, fee, FeePaidByEnum.any, BigInt(1362));
|
|
232
|
-
expect(result[0].value).toBe(546n);
|
|
233
|
-
expect(result[1].value).toBe(546n);
|
|
234
|
-
expect(result[2].value).toBe(546n);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test("Expect `change,any` to consume only change", async () => {
|
|
238
|
-
let to = [
|
|
239
|
-
["alice", 1000n],
|
|
240
|
-
["bob", 1000n],
|
|
241
|
-
["charlie", 1000n],
|
|
242
|
-
];
|
|
243
|
-
let fee = 1362n;
|
|
244
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
245
|
-
let result = allocateFee(
|
|
246
|
-
requests,
|
|
247
|
-
fee,
|
|
248
|
-
FeePaidByEnum.changeThenAny,
|
|
249
|
-
BigInt(1362)
|
|
250
|
-
);
|
|
251
|
-
expect(result[0].value).toBe(1000n);
|
|
252
|
-
expect(result[1].value).toBe(1000n);
|
|
253
|
-
expect(result[2].value).toBe(1000n);
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
test("Expect `change,any` to use both", async () => {
|
|
257
|
-
let to = [
|
|
258
|
-
["alice", 1000n],
|
|
259
|
-
["bob", 1000n],
|
|
260
|
-
["charlie", 1000n],
|
|
261
|
-
];
|
|
262
|
-
let fee = 1362n * 2n;
|
|
263
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
264
|
-
let result = allocateFee(
|
|
265
|
-
requests,
|
|
266
|
-
fee,
|
|
267
|
-
FeePaidByEnum.changeThenAny,
|
|
268
|
-
BigInt(1362)
|
|
269
|
-
);
|
|
270
|
-
expect(result[0].value).toBe(546n);
|
|
271
|
-
expect(result[1].value).toBe(546n);
|
|
272
|
-
expect(result[2].value).toBe(546n);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
test("Expect sortSendRequests to sort by lowest value first", async () => {
|
|
276
|
-
let to = [
|
|
277
|
-
["alice", 2000n],
|
|
278
|
-
["bob", 547n],
|
|
279
|
-
["charlie", 1n],
|
|
280
|
-
["dave", 4n],
|
|
281
|
-
["edward", 6n],
|
|
282
|
-
["fred", 2000n],
|
|
283
|
-
["greg", 2000n],
|
|
284
|
-
["harry", 2000n],
|
|
285
|
-
];
|
|
286
|
-
let fee = 1n;
|
|
287
|
-
let requests = asSendRequestObject(to) as SendRequest[];
|
|
288
|
-
|
|
289
|
-
let result = sortSendRequests(requests);
|
|
290
|
-
expect(result[0].value).toBe(1n);
|
|
291
|
-
expect(result[1].value).toBe(4n);
|
|
292
|
-
expect(result[2].value).toBe(6n);
|
|
293
|
-
expect(result[3].value).toBe(547n);
|
|
294
|
-
expect(result[4].value).toBe(2000n);
|
|
295
|
-
expect(result[5].value).toBe(2000n);
|
|
296
|
-
expect(result.length).toBe(8);
|
|
297
|
-
});
|
|
298
|
-
});
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
OpReturnData,
|
|
3
|
-
SendRequest,
|
|
4
|
-
TokenSendRequest,
|
|
5
|
-
} from "../wallet/model.js";
|
|
6
|
-
import { FeePaidByEnum } from "../wallet/enum.js";
|
|
7
|
-
import { DUST_UTXO_THRESHOLD } from "../constant.js";
|
|
8
|
-
|
|
9
|
-
export function checkFeeForDust(value: bigint) {
|
|
10
|
-
if (value < DUST_UTXO_THRESHOLD) {
|
|
11
|
-
throw Error("Fee strategy would result in dust output");
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function checkSatsAvailable(
|
|
16
|
-
sendRequestArray: Array<SendRequest>,
|
|
17
|
-
fee: bigint
|
|
18
|
-
) {
|
|
19
|
-
let amountAvailable = sendRequestArray.reduce(function (sum, r) {
|
|
20
|
-
return sum + (r.value - DUST_UTXO_THRESHOLD);
|
|
21
|
-
}, 0n);
|
|
22
|
-
if (amountAvailable < fee) {
|
|
23
|
-
throw Error("Insufficient funds for transaction given fee");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function checkForNonStandardSendRequest(
|
|
28
|
-
output: SendRequest | TokenSendRequest | OpReturnData
|
|
29
|
-
): SendRequest {
|
|
30
|
-
if (output instanceof TokenSendRequest) {
|
|
31
|
-
throw Error("Cannot specify fee to be paid by TokenSendRequest");
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (output instanceof OpReturnData) {
|
|
35
|
-
throw Error("Cannot specify fee to be paid by OpReturnData");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return output;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function sortSendRequests(sendRequestArray: Array<SendRequest>) {
|
|
42
|
-
return sendRequestArray.sort((a: SendRequest, b: SendRequest) =>
|
|
43
|
-
Number(a.value - b.value)
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function distributeFees(requests: Array<SendRequest>, fee: bigint) {
|
|
48
|
-
checkSatsAvailable(requests, fee);
|
|
49
|
-
for (let r = 0; r < requests.length; r++) {
|
|
50
|
-
if (fee > 0) {
|
|
51
|
-
checkForNonStandardSendRequest(requests[r]);
|
|
52
|
-
let perRequestFee = fee / BigInt(requests.length - r);
|
|
53
|
-
perRequestFee += fee % BigInt(requests.length - r);
|
|
54
|
-
if (requests[r].value - perRequestFee < DUST_UTXO_THRESHOLD) {
|
|
55
|
-
fee -= requests[r].value;
|
|
56
|
-
requests[r].value = 0n;
|
|
57
|
-
} else {
|
|
58
|
-
fee -= perRequestFee;
|
|
59
|
-
requests[r].value -= perRequestFee;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return requests.filter((r) => r.value >= DUST_UTXO_THRESHOLD);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function firstPays(
|
|
67
|
-
requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
|
|
68
|
-
fee: bigint
|
|
69
|
-
) {
|
|
70
|
-
let payer = requests.shift()!;
|
|
71
|
-
payer = checkForNonStandardSendRequest(payer);
|
|
72
|
-
payer.value = payer.value! - fee;
|
|
73
|
-
checkFeeForDust(payer.value);
|
|
74
|
-
requests.unshift(payer);
|
|
75
|
-
return requests;
|
|
76
|
-
}
|
|
77
|
-
function lastPays(
|
|
78
|
-
requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
|
|
79
|
-
fee: bigint
|
|
80
|
-
) {
|
|
81
|
-
let payer = requests.pop()!;
|
|
82
|
-
payer = checkForNonStandardSendRequest(payer);
|
|
83
|
-
payer.value = payer.value! - fee;
|
|
84
|
-
checkFeeForDust(payer.value);
|
|
85
|
-
requests.push(payer);
|
|
86
|
-
return requests;
|
|
87
|
-
}
|
|
88
|
-
function anyPays(
|
|
89
|
-
requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
|
|
90
|
-
fee: bigint
|
|
91
|
-
) {
|
|
92
|
-
for (let r of requests) {
|
|
93
|
-
checkForNonStandardSendRequest(r);
|
|
94
|
-
}
|
|
95
|
-
requests = sortSendRequests(requests as Array<SendRequest>);
|
|
96
|
-
requests = distributeFees(requests as Array<SendRequest>, fee);
|
|
97
|
-
return requests;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function changeThenFallback(
|
|
101
|
-
requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
|
|
102
|
-
fee: bigint,
|
|
103
|
-
change: bigint,
|
|
104
|
-
fallbackFn: Function
|
|
105
|
-
) {
|
|
106
|
-
if (BigInt(fee) > change) {
|
|
107
|
-
let outstandingFee = BigInt(fee) - change;
|
|
108
|
-
requests = fallbackFn(requests, outstandingFee);
|
|
109
|
-
}
|
|
110
|
-
return requests;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function allocateFee(
|
|
114
|
-
requests: Array<SendRequest | TokenSendRequest | OpReturnData>,
|
|
115
|
-
fee: bigint,
|
|
116
|
-
feePaidBy: FeePaidByEnum,
|
|
117
|
-
change: bigint
|
|
118
|
-
): Array<SendRequest> {
|
|
119
|
-
if (requests.length > 0) {
|
|
120
|
-
switch (feePaidBy) {
|
|
121
|
-
case FeePaidByEnum.change:
|
|
122
|
-
// handled by default
|
|
123
|
-
break;
|
|
124
|
-
case FeePaidByEnum.changeThenFirst:
|
|
125
|
-
requests = changeThenFallback(requests, fee, change, firstPays);
|
|
126
|
-
break;
|
|
127
|
-
case FeePaidByEnum.changeThenLast:
|
|
128
|
-
requests = changeThenFallback(requests, fee, change, lastPays);
|
|
129
|
-
break;
|
|
130
|
-
case FeePaidByEnum.changeThenAny:
|
|
131
|
-
requests = changeThenFallback(requests, fee, change, anyPays);
|
|
132
|
-
break;
|
|
133
|
-
case FeePaidByEnum.first:
|
|
134
|
-
requests = firstPays(requests, fee);
|
|
135
|
-
break;
|
|
136
|
-
case FeePaidByEnum.last:
|
|
137
|
-
requests = lastPays(requests, fee);
|
|
138
|
-
break;
|
|
139
|
-
case FeePaidByEnum.any:
|
|
140
|
-
requests = anyPays(requests, fee);
|
|
141
|
-
break;
|
|
142
|
-
default:
|
|
143
|
-
throw Error("FeePaidBy option not recognized");
|
|
144
|
-
}
|
|
145
|
-
return requests as Array<SendRequest>;
|
|
146
|
-
} else {
|
|
147
|
-
throw Error("Attempted to specify feePaidBy on zero length SendRequest");
|
|
148
|
-
}
|
|
149
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { amountInSatoshi } from "./amountInSatoshi";
|
|
2
|
-
import { bchParam } from "../chain";
|
|
3
|
-
import { ExchangeRate } from "../rate/ExchangeRate";
|
|
4
|
-
|
|
5
|
-
test("Get price of Bch, BCH, bch in sat", async () => {
|
|
6
|
-
let rate = await amountInSatoshi(1, "Bch");
|
|
7
|
-
expect(rate).toBe(bchParam.subUnits);
|
|
8
|
-
rate = await amountInSatoshi(1, "BCH");
|
|
9
|
-
expect(rate).toBe(bchParam.subUnits);
|
|
10
|
-
rate = await amountInSatoshi(1, "bch");
|
|
11
|
-
expect(rate).toBe(bchParam.subUnits);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test("Get price of sat(s)", async () => {
|
|
15
|
-
let rate = await amountInSatoshi(1, "sat");
|
|
16
|
-
expect(rate).toBe(1n);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("Get price of USD, Usd, usd", async () => {
|
|
20
|
-
let usdRate = await ExchangeRate.get("usd");
|
|
21
|
-
let rate = await amountInSatoshi(usdRate - 10, "USD");
|
|
22
|
-
expect(rate).toBeLessThan(bchParam.subUnits);
|
|
23
|
-
rate = await amountInSatoshi(usdRate - 10, "Usd");
|
|
24
|
-
expect(rate).toBeLessThan(bchParam.subUnits);
|
|
25
|
-
rate = await amountInSatoshi(usdRate - 10, "usd");
|
|
26
|
-
expect(rate).toBeLessThan(bchParam.subUnits);
|
|
27
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { bchParam } from "../chain.js";
|
|
2
|
-
import { UnitEnum } from "../enum.js";
|
|
3
|
-
import { ExchangeRate } from "../rate/ExchangeRate.js";
|
|
4
|
-
import { sanitizeUnit } from "../util/sanitizeUnit.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* converts given value and unit into satoshi
|
|
8
|
-
*
|
|
9
|
-
* @param {value} number some value
|
|
10
|
-
* @param {rawUnit} any the unit of value
|
|
11
|
-
*
|
|
12
|
-
* @returns a promise to the value in satoshi
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
export async function amountInSatoshi(
|
|
16
|
-
value: number,
|
|
17
|
-
rawUnit: any
|
|
18
|
-
): Promise<bigint> {
|
|
19
|
-
const unit = sanitizeUnit(rawUnit);
|
|
20
|
-
switch (unit) {
|
|
21
|
-
case UnitEnum.BCH:
|
|
22
|
-
return BigInt(Math.round(value * Number(bchParam.subUnits)));
|
|
23
|
-
case UnitEnum.SAT:
|
|
24
|
-
return BigInt(value);
|
|
25
|
-
default:
|
|
26
|
-
const Currency_over_BCH = await ExchangeRate.get(rawUnit);
|
|
27
|
-
const SAT_over_BCH = bchParam.subUnits;
|
|
28
|
-
|
|
29
|
-
return BigInt(
|
|
30
|
-
Math.round(Number(value * (Number(SAT_over_BCH) / Currency_over_BCH)))
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { base64ToBin } from "@bitauth/libauth";
|
|
2
|
-
import {
|
|
3
|
-
OpReturnData,
|
|
4
|
-
SendRequest,
|
|
5
|
-
SendRequestArray,
|
|
6
|
-
TokenSendRequest,
|
|
7
|
-
} from "../wallet/model.js";
|
|
8
|
-
|
|
9
|
-
export function asSendRequestObject(
|
|
10
|
-
requests:
|
|
11
|
-
| SendRequest
|
|
12
|
-
| TokenSendRequest
|
|
13
|
-
| OpReturnData
|
|
14
|
-
| Array<SendRequest | TokenSendRequest | OpReturnData>
|
|
15
|
-
| Array<SendRequestArray>
|
|
16
|
-
): Array<SendRequest | TokenSendRequest | OpReturnData> {
|
|
17
|
-
let resp: Array<SendRequest | TokenSendRequest | OpReturnData> = [];
|
|
18
|
-
if (Array.isArray(requests)) {
|
|
19
|
-
requests.forEach(
|
|
20
|
-
(r: SendRequest | TokenSendRequest | OpReturnData | SendRequestArray) => {
|
|
21
|
-
// the SendRequestArray[] case
|
|
22
|
-
if (Array.isArray(r)) {
|
|
23
|
-
if (r[0] === "OP_RETURN") {
|
|
24
|
-
// ['OP_RETURN', utf8ToBin('MEMO\x10')],
|
|
25
|
-
resp.push(OpReturnData.from(r[1] as string | Uint8Array));
|
|
26
|
-
} else if (r[0] === "OP_RETURNB64") {
|
|
27
|
-
resp.push(
|
|
28
|
-
OpReturnData.fromUint8Array(
|
|
29
|
-
Uint8Array.from(base64ToBin(r[1] as string))
|
|
30
|
-
)
|
|
31
|
-
);
|
|
32
|
-
} else {
|
|
33
|
-
// ['cashaddr', 120] or ['cashaddr', 120n],
|
|
34
|
-
resp.push(
|
|
35
|
-
new SendRequest({
|
|
36
|
-
cashaddr: r[0] as string,
|
|
37
|
-
value:
|
|
38
|
-
typeof r[1] === "number"
|
|
39
|
-
? BigInt(Math.floor[1])
|
|
40
|
-
: (r[1] as bigint),
|
|
41
|
-
})
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
// SendRequest | TokenSendRequest | OpReturnRequest case
|
|
46
|
-
resp.push(convertToClass(r));
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
} else {
|
|
51
|
-
// the SendRequest | OpReturnData object case
|
|
52
|
-
resp.push(
|
|
53
|
-
convertToClass(requests as SendRequest | TokenSendRequest | OpReturnData)
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
return resp;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function convertToClass(object: SendRequest | TokenSendRequest | OpReturnData) {
|
|
60
|
-
if (object.hasOwnProperty("category")) {
|
|
61
|
-
return new TokenSendRequest(object as TokenSendRequest);
|
|
62
|
-
} else if (object.hasOwnProperty("buffer")) {
|
|
63
|
-
return OpReturnData.fromUint8Array((object as OpReturnData).buffer);
|
|
64
|
-
}
|
|
65
|
-
// endcoding in REST
|
|
66
|
-
else if (object.hasOwnProperty("dataString")) {
|
|
67
|
-
return OpReturnData.fromString((object as any).dataString);
|
|
68
|
-
} else if (object.hasOwnProperty("dataBuffer")) {
|
|
69
|
-
return OpReturnData.fromUint8Array(
|
|
70
|
-
Uint8Array.from(base64ToBin((object as any).dataBuffer))
|
|
71
|
-
);
|
|
72
|
-
} else if (
|
|
73
|
-
object.hasOwnProperty("cashaddr") &&
|
|
74
|
-
object.hasOwnProperty("value") &&
|
|
75
|
-
object.hasOwnProperty("category") === false
|
|
76
|
-
) {
|
|
77
|
-
return new SendRequest(object as SendRequest);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
throw new Error("Unsupported send object");
|
|
81
|
-
}
|
package/src/util/base64.test.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { btoa, atob } from "./base64";
|
|
2
|
-
test("atob should decode a browser encoded string in node", async () => {
|
|
3
|
-
let encodedScript =
|
|
4
|
-
"Y29udHJhY3QgVHJhbnNmZXJXaXRoVGltZW91dChieXRlczIwIHNlbmRlclBraCwgYnl0ZXMyMCByZWNpcGllbnRQa2gsIGludCB0aW1lb3V0KSB7CiAgICAgICAgZnVuY3Rpb24gdHJhbnNmZXIocHVia2V5IHNpZ25pbmdQaywgc2lnIHMpIHsKICAgICAgICAgIHJlcXVpcmUoaGFzaDE2MChzaWduaW5nUGspID09IHJlY2lwaWVudFBraCk7CiAgICAgICAgICByZXF1aXJlKGNoZWNrU2lnKHMsIHNpZ25pbmdQaykpOwogICAgICAgIH0KICAKICAgICAgICBmdW5jdGlvbiB0aW1lb3V0KHB1YmtleSBzaWduaW5nUGssIHNpZyBzKSB7CiAgICAgICAgICAgIHJlcXVpcmUoaGFzaDE2MChzaWduaW5nUGspID09IHNlbmRlclBraCk7CiAgICAgICAgICAgIHJlcXVpcmUoY2hlY2tTaWcocywgc2lnbmluZ1BrKSk7CiAgICAgICAgICAgIHJlcXVpcmUodHgudGltZSA+PSB0aW1lb3V0KTsKICAgICAgICB9CiAgICB9";
|
|
5
|
-
let decodedScript = atob(encodedScript);
|
|
6
|
-
let script = `contract TransferWithTimeout(bytes20 senderPkh, bytes20 recipientPkh, int timeout) {
|
|
7
|
-
function transfer(pubkey signingPk, sig s) {
|
|
8
|
-
require(hash160(signingPk) == recipientPkh);
|
|
9
|
-
require(checkSig(s, signingPk));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function timeout(pubkey signingPk, sig s) {
|
|
13
|
-
require(hash160(signingPk) == senderPkh);
|
|
14
|
-
require(checkSig(s, signingPk));
|
|
15
|
-
require(tx.time >= timeout);
|
|
16
|
-
}
|
|
17
|
-
}`;
|
|
18
|
-
expect(decodedScript).toBe(script);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("btoa should encode a string in node identical to browser", async () => {
|
|
22
|
-
let browserEncoded =
|
|
23
|
-
"Y29udHJhY3QgVHJhbnNmZXJXaXRoVGltZW91dChieXRlczIwIHNlbmRlclBraCwgYnl0ZXMyMCByZWNpcGllbnRQa2gsIGludCB0aW1lb3V0KSB7CiAgICAgICAgZnVuY3Rpb24gdHJhbnNmZXIocHVia2V5IHNpZ25pbmdQaywgc2lnIHMpIHsKICAgICAgICAgIHJlcXVpcmUoaGFzaDE2MChzaWduaW5nUGspID09IHJlY2lwaWVudFBraCk7CiAgICAgICAgICByZXF1aXJlKGNoZWNrU2lnKHMsIHNpZ25pbmdQaykpOwogICAgICAgIH0KICAKICAgICAgICBmdW5jdGlvbiB0aW1lb3V0KHB1YmtleSBzaWduaW5nUGssIHNpZyBzKSB7CiAgICAgICAgICAgIHJlcXVpcmUoaGFzaDE2MChzaWduaW5nUGspID09IHNlbmRlclBraCk7CiAgICAgICAgICAgIHJlcXVpcmUoY2hlY2tTaWcocywgc2lnbmluZ1BrKSk7CiAgICAgICAgICAgIHJlcXVpcmUodHgudGltZSA+PSB0aW1lb3V0KTsKICAgICAgICB9CiAgICB9";
|
|
24
|
-
|
|
25
|
-
let script = `contract TransferWithTimeout(bytes20 senderPkh, bytes20 recipientPkh, int timeout) {
|
|
26
|
-
function transfer(pubkey signingPk, sig s) {
|
|
27
|
-
require(hash160(signingPk) == recipientPkh);
|
|
28
|
-
require(checkSig(s, signingPk));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function timeout(pubkey signingPk, sig s) {
|
|
32
|
-
require(hash160(signingPk) == senderPkh);
|
|
33
|
-
require(checkSig(s, signingPk));
|
|
34
|
-
require(tx.time >= timeout);
|
|
35
|
-
}
|
|
36
|
-
}`;
|
|
37
|
-
let encodedScript = btoa(script);
|
|
38
|
-
expect(encodedScript).toBe(browserEncoded);
|
|
39
|
-
});
|
package/src/util/base64.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { base64ToBin, binToBase64, isBase64 } from "@bitauth/libauth";
|
|
2
|
-
|
|
3
|
-
export function btoa(data: string) {
|
|
4
|
-
return binToBase64(new TextEncoder().encode(data));
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function atob(data: string) {
|
|
8
|
-
if (!isBase64(data)) {
|
|
9
|
-
throw new Error("Provided data is not a valid base64");
|
|
10
|
-
}
|
|
11
|
-
return String.fromCharCode(...base64ToBin(data));
|
|
12
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { binToHex } from "@bitauth/libauth";
|
|
2
|
-
|
|
3
|
-
export function checkForEmptySeed(seed: Uint8Array) {
|
|
4
|
-
let blankSeed =
|
|
5
|
-
"4ed8d4b17698ddeaa1f1559f152f87b5d472f725ca86d341bd0276f1b61197e21dd5a391f9f5ed7340ff4d4513aab9cce44f9497a5e7ed85fd818876b6eb402e";
|
|
6
|
-
let seedBin = new Uint8Array(seed);
|
|
7
|
-
if (blankSeed == binToHex(seedBin))
|
|
8
|
-
throw Error("Seed was generated using empty mnemonic");
|
|
9
|
-
}
|
package/src/util/checkUtxos.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Utxo, UtxoId } from "../interface.js";
|
|
2
|
-
|
|
3
|
-
export function checkUtxos(utxoIds: UtxoId[], utxos: Utxo[]): Utxo[] {
|
|
4
|
-
const walletUtxoMap = new Map<string, Utxo>();
|
|
5
|
-
for (const utxo of utxos) {
|
|
6
|
-
walletUtxoMap.set(`${utxo.txid}:${utxo.vout}`, utxo);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const result: Utxo[] = [];
|
|
10
|
-
const absent: UtxoId[] = [];
|
|
11
|
-
for (const id of utxoIds) {
|
|
12
|
-
const key = `${id.txid}:${id.vout}`;
|
|
13
|
-
const utxo = walletUtxoMap.get(key);
|
|
14
|
-
if (utxo) {
|
|
15
|
-
result.push(utxo);
|
|
16
|
-
} else {
|
|
17
|
-
absent.push(id);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (absent.length) {
|
|
22
|
-
const absentString = absent
|
|
23
|
-
.map((val) => `${val.txid}:${val.vout}`)
|
|
24
|
-
.join(", ");
|
|
25
|
-
throw Error(`Utxos [${absentString}] not found in wallet`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return result;
|
|
29
|
-
}
|