mainnet-js 1.1.34 → 2.0.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-1.1.34.js → mainnet-2.0.0.js} +25 -337
- package/dist/module/db/SqlProvider.d.ts +2 -2
- package/dist/module/db/SqlProvider.d.ts.map +1 -1
- package/dist/module/db/SqlProvider.js +6 -24
- package/dist/module/db/SqlProvider.js.map +1 -1
- package/dist/module/index.d.ts +0 -3
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +3 -6
- package/dist/module/index.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +4 -8
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +6 -10
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/util/deriveCashaddr.d.ts +1 -0
- package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
- package/dist/module/util/deriveCashaddr.js +8 -1
- package/dist/module/util/deriveCashaddr.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +1 -1
- package/dist/module/wallet/Base.js +1 -1
- package/dist/module/wallet/Util.d.ts +2 -2
- package/dist/module/wallet/Util.js +2 -2
- package/dist/module/wallet/Wif.d.ts +0 -28
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +2 -109
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +0 -12
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +0 -43
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +0 -2
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/webhook/Webhook.d.ts +1 -6
- package/dist/module/webhook/Webhook.d.ts.map +1 -1
- package/dist/module/webhook/Webhook.js +0 -4
- package/dist/module/webhook/Webhook.js.map +1 -1
- package/dist/module/webhook/index.d.ts +0 -1
- package/dist/module/webhook/index.d.ts.map +1 -1
- package/dist/module/webhook/index.js +0 -1
- package/dist/module/webhook/index.js.map +1 -1
- package/dist/module/webhook/interface.d.ts +0 -1
- package/dist/module/webhook/interface.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -10
- package/src/db/SqlProvider.test.ts +2 -18
- package/src/db/SqlProvider.ts +5 -26
- package/src/index.ts +3 -7
- package/src/transaction/Wif.ts +2 -19
- package/src/util/deriveCashaddr.test.ts +8 -0
- package/src/util/deriveCashaddr.ts +9 -0
- package/src/wallet/Base.ts +1 -1
- package/src/wallet/Util.ts +2 -2
- package/src/wallet/Wif.test.ts +0 -23
- package/src/wallet/Wif.ts +2 -148
- package/src/wallet/createWallet.ts +0 -53
- package/src/wallet/interface.ts +0 -2
- package/src/webhook/Webhook.ts +4 -10
- package/src/webhook/index.ts +0 -1
- package/src/webhook/interface.ts +0 -1
- package/webpack.config.cjs +7 -7
- package/dist/module/slp/GsppProvider.d.ts +0 -55
- package/dist/module/slp/GsppProvider.d.ts.map +0 -1
- package/dist/module/slp/GsppProvider.js +0 -204
- package/dist/module/slp/GsppProvider.js.map +0 -1
- package/dist/module/slp/SlpDbProvider.d.ts +0 -52
- package/dist/module/slp/SlpDbProvider.d.ts.map +0 -1
- package/dist/module/slp/SlpDbProvider.js +0 -175
- package/dist/module/slp/SlpDbProvider.js.map +0 -1
- package/dist/module/slp/SlpDbTemplates.d.ts +0 -313
- package/dist/module/slp/SlpDbTemplates.d.ts.map +0 -1
- package/dist/module/slp/SlpDbTemplates.js +0 -375
- package/dist/module/slp/SlpDbTemplates.js.map +0 -1
- package/dist/module/slp/SlpLibAuth.d.ts +0 -210
- package/dist/module/slp/SlpLibAuth.d.ts.map +0 -1
- package/dist/module/slp/SlpLibAuth.js +0 -451
- package/dist/module/slp/SlpLibAuth.js.map +0 -1
- package/dist/module/slp/SlpProvider.d.ts +0 -24
- package/dist/module/slp/SlpProvider.d.ts.map +0 -1
- package/dist/module/slp/SlpProvider.js +0 -29
- package/dist/module/slp/SlpProvider.js.map +0 -1
- package/dist/module/slp/index.d.ts +0 -6
- package/dist/module/slp/index.d.ts.map +0 -1
- package/dist/module/slp/index.js +0 -5
- package/dist/module/slp/index.js.map +0 -1
- package/dist/module/slp/interface.d.ts +0 -114
- package/dist/module/slp/interface.d.ts.map +0 -1
- package/dist/module/slp/interface.js +0 -7
- package/dist/module/slp/interface.js.map +0 -1
- package/dist/module/util/bchaddr.d.ts +0 -2
- package/dist/module/util/bchaddr.d.ts.map +0 -1
- package/dist/module/util/bchaddr.js +0 -16
- package/dist/module/util/bchaddr.js.map +0 -1
- package/dist/module/wallet/Slp.d.ts +0 -431
- package/dist/module/wallet/Slp.d.ts.map +0 -1
- package/dist/module/wallet/Slp.js +0 -770
- package/dist/module/wallet/Slp.js.map +0 -1
- package/dist/module/webhook/WebhookSlp.d.ts +0 -15
- package/dist/module/webhook/WebhookSlp.d.ts.map +0 -1
- package/dist/module/webhook/WebhookSlp.js +0 -100
- package/dist/module/webhook/WebhookSlp.js.map +0 -1
- package/src/slp/GsppProvider.test.ts +0 -40
- package/src/slp/GsppProvider.ts +0 -329
- package/src/slp/SlpDbProvider.test.ts +0 -27
- package/src/slp/SlpDbProvider.ts +0 -291
- package/src/slp/SlpDbTemplates.ts +0 -400
- package/src/slp/SlpLibAuth.test.ts +0 -162
- package/src/slp/SlpLibAuth.ts +0 -532
- package/src/slp/SlpProvider.ts +0 -100
- package/src/slp/index.ts +0 -6
- package/src/slp/interface.ts +0 -128
- package/src/util/bchaddr.test.ts +0 -27
- package/src/util/bchaddr.ts +0 -27
- package/src/wallet/Slp.test.headless.js +0 -120
- package/src/wallet/Slp.test.ts +0 -1000
- package/src/wallet/Slp.ts +0 -1014
- package/src/webhook/WebhookSlp.test.ts +0 -288
- package/src/webhook/WebhookSlp.ts +0 -123
package/src/db/SqlProvider.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { sslConfigI, WalletI, FaucetQueueItemI } from "./interface.js";
|
|
|
3
3
|
import { TxI } from "../interface.js";
|
|
4
4
|
import { Webhook, WebhookRecurrence, WebhookType } from "../webhook/Webhook.js";
|
|
5
5
|
import { RegisterWebhookParams } from "../webhook/interface.js";
|
|
6
|
-
import { isCashAddress } from "../util/bchaddr.js";
|
|
7
6
|
import { getSslConfig } from "./util.js";
|
|
8
7
|
import parseDbUrl from "parse-database-url";
|
|
9
8
|
import pg from "pg";
|
|
@@ -64,7 +63,6 @@ export default class SqlProvider implements StorageProvider {
|
|
|
64
63
|
"status TEXT," +
|
|
65
64
|
"tx_seen JSON," +
|
|
66
65
|
"last_height INTEGER," +
|
|
67
|
-
"token_id TEXT," +
|
|
68
66
|
"expires_at TIMESTAMPTZ" +
|
|
69
67
|
");",
|
|
70
68
|
this.webhookTable
|
|
@@ -75,7 +73,6 @@ export default class SqlProvider implements StorageProvider {
|
|
|
75
73
|
"CREATE TABLE IF NOT EXISTS %I (" +
|
|
76
74
|
"id SERIAL PRIMARY KEY," +
|
|
77
75
|
"address TEXT," +
|
|
78
|
-
"token TEXT," +
|
|
79
76
|
"value TEXT" +
|
|
80
77
|
");",
|
|
81
78
|
this.faucetQueueTable
|
|
@@ -144,19 +141,8 @@ export default class SqlProvider implements StorageProvider {
|
|
|
144
141
|
}
|
|
145
142
|
|
|
146
143
|
public async webhookFromDb(hook: Webhook) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
delete (hook as any).token_id;
|
|
150
|
-
|
|
151
|
-
if (hook.type.indexOf("slp") === 0) {
|
|
152
|
-
const { WebhookSlp } = await import("../webhook/WebhookSlp.js");
|
|
153
|
-
return new WebhookSlp(hook);
|
|
154
|
-
} else if (isCashAddress(hook.cashaddr)) {
|
|
155
|
-
const { WebhookBch } = await import("../webhook/WebhookBch.js");
|
|
156
|
-
return new WebhookBch(hook);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
throw new Error(`Unsupported or incorrect hook address ${hook.cashaddr}`);
|
|
144
|
+
const { WebhookBch } = await import("../webhook/WebhookBch.js");
|
|
145
|
+
return new WebhookBch(hook);
|
|
160
146
|
}
|
|
161
147
|
|
|
162
148
|
public async addWebhook(params: RegisterWebhookParams): Promise<Webhook> {
|
|
@@ -170,17 +156,12 @@ export default class SqlProvider implements StorageProvider {
|
|
|
170
156
|
params.duration_sec = params.duration_sec || expireTimeout;
|
|
171
157
|
params.duration_sec =
|
|
172
158
|
params.duration_sec > expireTimeout ? expireTimeout : params.duration_sec;
|
|
173
|
-
params.tokenId = params.tokenId || "";
|
|
174
|
-
|
|
175
|
-
if (params.type.indexOf("slp") === 0 && !params.tokenId) {
|
|
176
|
-
throw new Error("'tokenId' parameter is required for SLP webhooks");
|
|
177
|
-
}
|
|
178
159
|
|
|
179
160
|
const expires_at = new Date(
|
|
180
161
|
new Date().getTime() + params.duration_sec * 1000
|
|
181
162
|
);
|
|
182
163
|
let text = this.formatter(
|
|
183
|
-
"INSERT into %I (cashaddr,type,recurrence,url,status,tx_seen,last_height,
|
|
164
|
+
"INSERT into %I (cashaddr,type,recurrence,url,status,tx_seen,last_height,expires_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;",
|
|
184
165
|
this.webhookTable
|
|
185
166
|
);
|
|
186
167
|
|
|
@@ -192,7 +173,6 @@ export default class SqlProvider implements StorageProvider {
|
|
|
192
173
|
"",
|
|
193
174
|
"[]",
|
|
194
175
|
0,
|
|
195
|
-
params.tokenId,
|
|
196
176
|
expires_at.toISOString(),
|
|
197
177
|
]);
|
|
198
178
|
const hook = await this.webhookFromDb(result.rows[0]);
|
|
@@ -266,14 +246,13 @@ export default class SqlProvider implements StorageProvider {
|
|
|
266
246
|
|
|
267
247
|
public async addFaucetQueueItem(
|
|
268
248
|
address: string,
|
|
269
|
-
tokenId: string,
|
|
270
249
|
value: string
|
|
271
250
|
): Promise<boolean> {
|
|
272
251
|
let text = this.formatter(
|
|
273
|
-
"INSERT into %I (address,
|
|
252
|
+
"INSERT into %I (address,value) VALUES ($1, $2);",
|
|
274
253
|
this.faucetQueueTable
|
|
275
254
|
);
|
|
276
|
-
return await this.db.query(text, [address,
|
|
255
|
+
return await this.db.query(text, [address, value]);
|
|
277
256
|
}
|
|
278
257
|
|
|
279
258
|
public async getFaucetQueue(): Promise<Array<FaucetQueueItemI>> {
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from "./db/index.js";
|
|
2
2
|
export * from "./mine/index.js";
|
|
3
|
-
export * from "./slp/index.js";
|
|
4
3
|
export * from "./test/expect.js";
|
|
5
4
|
export * from "./test/axios.js";
|
|
6
5
|
export * from "./webhook/index.js";
|
|
@@ -50,9 +49,6 @@ export {
|
|
|
50
49
|
} from "./message/interface.js";
|
|
51
50
|
export { WalletRequestI, WalletResponseI } from "./wallet/interface.js";
|
|
52
51
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
export function cube(x: number) {
|
|
57
|
-
return x * x * x;
|
|
58
|
-
}
|
|
52
|
+
// export function cube(x: number) {
|
|
53
|
+
// return x * x * x;
|
|
54
|
+
// }
|
package/src/transaction/Wif.ts
CHANGED
|
@@ -39,7 +39,6 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
39
39
|
sourceAddress,
|
|
40
40
|
fee = 0,
|
|
41
41
|
discardChange = false,
|
|
42
|
-
slpOutputs = [],
|
|
43
42
|
feePaidBy = FeePaidByEnum.change,
|
|
44
43
|
changeAddress = "",
|
|
45
44
|
}: {
|
|
@@ -49,7 +48,6 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
49
48
|
sourceAddress: string;
|
|
50
49
|
fee?: number;
|
|
51
50
|
discardChange?: boolean;
|
|
52
|
-
slpOutputs?: Output[];
|
|
53
51
|
feePaidBy?: FeePaidByEnum;
|
|
54
52
|
changeAddress?: string;
|
|
55
53
|
}) {
|
|
@@ -101,7 +99,7 @@ export async function buildP2pkhNonHdTransaction({
|
|
|
101
99
|
const result = generateTransaction({
|
|
102
100
|
inputs: preparedInputs,
|
|
103
101
|
locktime: 0,
|
|
104
|
-
outputs:
|
|
102
|
+
outputs: lockedOutputs,
|
|
105
103
|
version: 2,
|
|
106
104
|
});
|
|
107
105
|
|
|
@@ -429,7 +427,6 @@ export async function getFeeAmountSimple({
|
|
|
429
427
|
utxos,
|
|
430
428
|
sendRequests,
|
|
431
429
|
relayFeePerByteInSatoshi,
|
|
432
|
-
slpOutputs,
|
|
433
430
|
discardChange,
|
|
434
431
|
}: {
|
|
435
432
|
utxos: UtxoI[];
|
|
@@ -437,7 +434,6 @@ export async function getFeeAmountSimple({
|
|
|
437
434
|
privateKey: Uint8Array;
|
|
438
435
|
sourceAddress: string;
|
|
439
436
|
relayFeePerByteInSatoshi: number;
|
|
440
|
-
slpOutputs: Output[];
|
|
441
437
|
feePaidBy: FeePaidByEnum;
|
|
442
438
|
discardChange?: boolean;
|
|
443
439
|
}) {
|
|
@@ -479,15 +475,8 @@ export async function getFeeAmountSimple({
|
|
|
479
475
|
const outputTotalSize =
|
|
480
476
|
sendRequests.reduce((prev, curr) => prev + outputSize(curr), 0) +
|
|
481
477
|
(discardChange ? 0 : outputSizeP2pkh);
|
|
482
|
-
const slpTotalSize = slpOutputs.reduce(
|
|
483
|
-
(prev, curr) => prev + curr.lockingBytecode.length,
|
|
484
|
-
0
|
|
485
|
-
);
|
|
486
478
|
|
|
487
|
-
return (
|
|
488
|
-
(inputTotalSize + outputTotalSize + slpTotalSize + 16) *
|
|
489
|
-
relayFeePerByteInSatoshi
|
|
490
|
-
);
|
|
479
|
+
return (inputTotalSize + outputTotalSize + 16) * relayFeePerByteInSatoshi;
|
|
491
480
|
}
|
|
492
481
|
|
|
493
482
|
// precise fee estimation
|
|
@@ -497,7 +486,6 @@ export async function getFeeAmount({
|
|
|
497
486
|
privateKey,
|
|
498
487
|
sourceAddress,
|
|
499
488
|
relayFeePerByteInSatoshi,
|
|
500
|
-
slpOutputs,
|
|
501
489
|
feePaidBy,
|
|
502
490
|
discardChange,
|
|
503
491
|
}: {
|
|
@@ -506,7 +494,6 @@ export async function getFeeAmount({
|
|
|
506
494
|
privateKey: Uint8Array;
|
|
507
495
|
sourceAddress: string;
|
|
508
496
|
relayFeePerByteInSatoshi: number;
|
|
509
|
-
slpOutputs: Output[];
|
|
510
497
|
feePaidBy: FeePaidByEnum;
|
|
511
498
|
discardChange?: boolean;
|
|
512
499
|
}) {
|
|
@@ -521,7 +508,6 @@ export async function getFeeAmount({
|
|
|
521
508
|
sourceAddress,
|
|
522
509
|
fee: 0, //DUST_UTXO_THRESHOLD
|
|
523
510
|
discardChange: discardChange ?? false,
|
|
524
|
-
slpOutputs,
|
|
525
511
|
feePaidBy,
|
|
526
512
|
changeAddress: "",
|
|
527
513
|
});
|
|
@@ -542,7 +528,6 @@ export async function buildEncodedTransaction({
|
|
|
542
528
|
sourceAddress,
|
|
543
529
|
fee = 0,
|
|
544
530
|
discardChange = false,
|
|
545
|
-
slpOutputs = [],
|
|
546
531
|
feePaidBy = FeePaidByEnum.change,
|
|
547
532
|
changeAddress = "",
|
|
548
533
|
buildUnsigned = false,
|
|
@@ -553,7 +538,6 @@ export async function buildEncodedTransaction({
|
|
|
553
538
|
sourceAddress: string;
|
|
554
539
|
fee?: number;
|
|
555
540
|
discardChange?: boolean;
|
|
556
|
-
slpOutputs?: Output[];
|
|
557
541
|
feePaidBy?: FeePaidByEnum;
|
|
558
542
|
changeAddress?: string;
|
|
559
543
|
buildUnsigned?: boolean;
|
|
@@ -565,7 +549,6 @@ export async function buildEncodedTransaction({
|
|
|
565
549
|
sourceAddress,
|
|
566
550
|
fee,
|
|
567
551
|
discardChange,
|
|
568
|
-
slpOutputs,
|
|
569
552
|
feePaidBy,
|
|
570
553
|
changeAddress,
|
|
571
554
|
});
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
deriveCashaddr,
|
|
4
4
|
deriveTokenaddr,
|
|
5
5
|
isTokenaddr,
|
|
6
|
+
isValidAddress,
|
|
6
7
|
toCashaddr,
|
|
7
8
|
toTokenaddr,
|
|
8
9
|
} from "./deriveCashaddr";
|
|
@@ -41,3 +42,10 @@ test("Test address conversion", async () => {
|
|
|
41
42
|
expect(isTokenaddr(wallet.cashaddr!)).toBe(false);
|
|
42
43
|
expect(isTokenaddr(wallet.tokenaddr!)).toBe(true);
|
|
43
44
|
});
|
|
45
|
+
|
|
46
|
+
test("Test isValidAddress", async () => {
|
|
47
|
+
const wallet = await RegTestWallet.watchOnly(process.env.ADDRESS!);
|
|
48
|
+
expect(isValidAddress(wallet.cashaddr!)).toBe(true);
|
|
49
|
+
expect(isValidAddress(wallet.tokenaddr!)).toBe(true);
|
|
50
|
+
expect(isValidAddress("asdf")).toBe(false);
|
|
51
|
+
});
|
|
@@ -13,6 +13,15 @@ import {
|
|
|
13
13
|
|
|
14
14
|
import { hash160 } from "./hash160.js";
|
|
15
15
|
|
|
16
|
+
export function isValidAddress(cashaddr: string): boolean {
|
|
17
|
+
const result = decodeCashAddress(cashaddr);
|
|
18
|
+
if (typeof result === "string") {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
export function deriveCashaddr(
|
|
17
26
|
privateKey: Uint8Array,
|
|
18
27
|
networkPrefix: CashAddressNetworkPrefix
|
package/src/wallet/Base.ts
CHANGED
|
@@ -153,7 +153,7 @@ export class BaseWallet implements WalletI {
|
|
|
153
153
|
* such kind of wallet does not have a private key and is unable to spend any funds
|
|
154
154
|
* however it still allows to use many utility functions such as getting and watching balance, etc.
|
|
155
155
|
*
|
|
156
|
-
* @param address cashaddress, token aware cashaddress
|
|
156
|
+
* @param address cashaddress, token aware cashaddress of a wallet
|
|
157
157
|
*
|
|
158
158
|
* @returns instantiated wallet
|
|
159
159
|
*/
|
package/src/wallet/Util.ts
CHANGED
|
@@ -166,7 +166,7 @@ export class Util {
|
|
|
166
166
|
|
|
167
167
|
//#region Specific wallet classes
|
|
168
168
|
/**
|
|
169
|
-
* Class to manage
|
|
169
|
+
* Class to manage a testnet wallet.
|
|
170
170
|
*/
|
|
171
171
|
export class TestNetUtil extends Util {
|
|
172
172
|
static get walletType() {
|
|
@@ -175,7 +175,7 @@ export class TestNetUtil extends Util {
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Class to manage
|
|
178
|
+
* Class to manage a regtest wallet.
|
|
179
179
|
*/
|
|
180
180
|
export class RegTestUtil extends Util {
|
|
181
181
|
static get walletType() {
|
package/src/wallet/Wif.test.ts
CHANGED
|
@@ -887,29 +887,6 @@ describe(`Wallet subscriptions`, () => {
|
|
|
887
887
|
delay(3000);
|
|
888
888
|
expect(response.balance!.sat!).toBe(0);
|
|
889
889
|
});
|
|
890
|
-
|
|
891
|
-
test.skip("Should get testnet slp tokens and send them back", async () => {
|
|
892
|
-
let aliceWif = `${process.env.ALICE_TESTNET_WALLET_ID!}`;
|
|
893
|
-
let aliceWallet = await TestNetWallet.fromId(aliceWif);
|
|
894
|
-
|
|
895
|
-
const wallet = (await TestNetWallet.newRandom()) as TestNetWallet;
|
|
896
|
-
|
|
897
|
-
// send bob some bch gas to enable him to send slp
|
|
898
|
-
await aliceWallet
|
|
899
|
-
.slpAware()
|
|
900
|
-
.send([{ cashaddr: wallet.cashaddr!, value: 5000, unit: "sat" }]);
|
|
901
|
-
|
|
902
|
-
const txid = await wallet.getTestnetSlp("MNC");
|
|
903
|
-
expect(txid.length).toBe(64);
|
|
904
|
-
let balance = await wallet.slp.getBalance("MNC");
|
|
905
|
-
expect(balance[0].value.toNumber()).toBe(10);
|
|
906
|
-
|
|
907
|
-
const tokenId = balance[0].tokenId;
|
|
908
|
-
const response = await wallet.returnTestnetSlp(tokenId);
|
|
909
|
-
expect(response.balance).toBe(0);
|
|
910
|
-
|
|
911
|
-
await wallet.slpAware(false).sendMax(aliceWallet.cashaddr!);
|
|
912
|
-
});
|
|
913
890
|
});
|
|
914
891
|
|
|
915
892
|
describe(`Wallet extrema behavior regression testing`, () => {
|
package/src/wallet/Wif.ts
CHANGED
|
@@ -77,20 +77,7 @@ import { sumTokenAmounts, sumUtxoValue } from "../util/sumUtxoValue.js";
|
|
|
77
77
|
import { sumSendRequestAmounts } from "../util/sumSendRequestAmounts.js";
|
|
78
78
|
import { ElectrumRawTransaction } from "../network/interface.js";
|
|
79
79
|
import { getRelayFeeCache } from "../network/getRelayFeeCache.js";
|
|
80
|
-
import {
|
|
81
|
-
RegTestSlp,
|
|
82
|
-
RegTestWatchSlp,
|
|
83
|
-
RegTestWifSlp,
|
|
84
|
-
Slp,
|
|
85
|
-
TestNetSlp,
|
|
86
|
-
TestNetWatchSlp,
|
|
87
|
-
TestNetWifSlp,
|
|
88
|
-
WatchSlp,
|
|
89
|
-
WifSlp,
|
|
90
|
-
} from "./Slp.js";
|
|
91
80
|
import axios from "axios";
|
|
92
|
-
import { SlpSendResponse } from "../slp/interface.js";
|
|
93
|
-
import { toCashAddress } from "../util/bchaddr.js";
|
|
94
81
|
import {
|
|
95
82
|
RegTestUtil,
|
|
96
83
|
RegTestWatchUtil,
|
|
@@ -137,28 +124,11 @@ export class Wallet extends BaseWallet {
|
|
|
137
124
|
publicKey?: Uint8Array;
|
|
138
125
|
publicKeyHash?: Uint8Array;
|
|
139
126
|
networkPrefix: CashAddressNetworkPrefix;
|
|
140
|
-
_slp?: Slp;
|
|
141
|
-
_slpAware: boolean = false; // a flag which activates utxo checking against an external slp indexer
|
|
142
127
|
_slpSemiAware: boolean = false; // a flag which requires an utxo to have more than 546 sats to be spendable and counted in the balance
|
|
143
128
|
_util?: Util;
|
|
144
129
|
static signedMessage: SignedMessageI = new SignedMessage();
|
|
145
130
|
|
|
146
131
|
//#region Accessors
|
|
147
|
-
// interface to slp functions. see Slp.ts
|
|
148
|
-
public get slp() {
|
|
149
|
-
if (!this._slp) {
|
|
150
|
-
this._slp = new Slp(this);
|
|
151
|
-
this._slpAware = true;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return this._slp;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// interface to slp functions. see Slp.ts
|
|
158
|
-
public static get slp() {
|
|
159
|
-
return Slp;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
132
|
// interface to util functions. see Util.ts
|
|
163
133
|
public get util() {
|
|
164
134
|
if (!this._util) {
|
|
@@ -173,11 +143,6 @@ export class Wallet extends BaseWallet {
|
|
|
173
143
|
return Util;
|
|
174
144
|
}
|
|
175
145
|
|
|
176
|
-
public slpAware(value: boolean = true): Wallet {
|
|
177
|
-
this._slpAware = value;
|
|
178
|
-
return this;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
146
|
public slpSemiAware(value: boolean = true): Wallet {
|
|
182
147
|
this._slpSemiAware = value;
|
|
183
148
|
return this;
|
|
@@ -346,23 +311,6 @@ export class Wallet extends BaseWallet {
|
|
|
346
311
|
address
|
|
347
312
|
) as InstanceType<T>;
|
|
348
313
|
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* fromSlpaddr - create an SLP aware watch-only wallet in the network derived from the address
|
|
352
|
-
*
|
|
353
|
-
* such kind of wallet does not have a private key and is unable to spend any funds
|
|
354
|
-
* however it still allows to use many utility functions such as getting and watching balance, etc.
|
|
355
|
-
*
|
|
356
|
-
* @param address slpaddress of a wallet
|
|
357
|
-
*
|
|
358
|
-
* @returns instantiated wallet
|
|
359
|
-
*/
|
|
360
|
-
public static async fromSlpaddr<T extends typeof Wallet>(
|
|
361
|
-
this: T,
|
|
362
|
-
address: string
|
|
363
|
-
): Promise<InstanceType<T>> {
|
|
364
|
-
return this.fromCashaddr(toCashAddress(address)) as InstanceType<T>;
|
|
365
|
-
}
|
|
366
314
|
//#endregion Constructors and Statics
|
|
367
315
|
|
|
368
316
|
//#region Protected implementations
|
|
@@ -639,18 +587,7 @@ export class Wallet extends BaseWallet {
|
|
|
639
587
|
address = this.cashaddr!;
|
|
640
588
|
}
|
|
641
589
|
|
|
642
|
-
if (this.
|
|
643
|
-
const [bchUtxos, slpOutpoints] = await Promise.all([
|
|
644
|
-
this.provider!.getUtxos(address),
|
|
645
|
-
this.slp.getSlpOutpoints(),
|
|
646
|
-
]);
|
|
647
|
-
return bchUtxos.filter(
|
|
648
|
-
(bchutxo) =>
|
|
649
|
-
slpOutpoints.findIndex(
|
|
650
|
-
(slpOutpoint) => `${bchutxo.txid}:${bchutxo.vout}` === slpOutpoint
|
|
651
|
-
) === -1
|
|
652
|
-
);
|
|
653
|
-
} else if (this._slpSemiAware) {
|
|
590
|
+
if (this._slpSemiAware) {
|
|
654
591
|
const bchUtxos: UtxoI[] = await this.provider!.getUtxos(address);
|
|
655
592
|
return bchUtxos.filter(
|
|
656
593
|
(bchutxo) => bchutxo.satoshis > DUST_UTXO_THRESHOLD
|
|
@@ -866,10 +803,6 @@ export class Wallet extends BaseWallet {
|
|
|
866
803
|
throw Error("attempted to send without a cashaddr");
|
|
867
804
|
}
|
|
868
805
|
|
|
869
|
-
if (params.options && params.options.slpAware) {
|
|
870
|
-
this._slpAware = true;
|
|
871
|
-
}
|
|
872
|
-
|
|
873
806
|
if (params.options && params.options.slpSemiAware) {
|
|
874
807
|
this._slpSemiAware = true;
|
|
875
808
|
}
|
|
@@ -920,7 +853,6 @@ export class Wallet extends BaseWallet {
|
|
|
920
853
|
privateKey: this.privateKey ?? Uint8Array.from([]),
|
|
921
854
|
sourceAddress: this.cashaddr!,
|
|
922
855
|
relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
|
|
923
|
-
slpOutputs: [],
|
|
924
856
|
feePaidBy: feePaidBy,
|
|
925
857
|
});
|
|
926
858
|
const spendableAmount = sumUtxoValue(fundingUtxos);
|
|
@@ -1097,10 +1029,6 @@ export class Wallet extends BaseWallet {
|
|
|
1097
1029
|
throw Error("attempted to send without a cashaddr");
|
|
1098
1030
|
}
|
|
1099
1031
|
|
|
1100
|
-
if (options && options.slpAware) {
|
|
1101
|
-
this._slpAware = true;
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
1032
|
if (options && options.slpSemiAware) {
|
|
1105
1033
|
this._slpSemiAware = true;
|
|
1106
1034
|
}
|
|
@@ -1238,7 +1166,6 @@ export class Wallet extends BaseWallet {
|
|
|
1238
1166
|
privateKey: this.privateKey ?? Uint8Array.from([]),
|
|
1239
1167
|
sourceAddress: this.cashaddr!,
|
|
1240
1168
|
relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
|
|
1241
|
-
slpOutputs: [],
|
|
1242
1169
|
feePaidBy: feePaidBy,
|
|
1243
1170
|
});
|
|
1244
1171
|
|
|
@@ -1262,7 +1189,6 @@ export class Wallet extends BaseWallet {
|
|
|
1262
1189
|
privateKey: this.privateKey ?? Uint8Array.from([]),
|
|
1263
1190
|
sourceAddress: this.cashaddr!,
|
|
1264
1191
|
relayFeePerByteInSatoshi: relayFeePerByteInSatoshi,
|
|
1265
|
-
slpOutputs: [],
|
|
1266
1192
|
feePaidBy: feePaidBy,
|
|
1267
1193
|
});
|
|
1268
1194
|
const { encodedTransaction, sourceOutputs } = await buildEncodedTransaction(
|
|
@@ -1273,7 +1199,6 @@ export class Wallet extends BaseWallet {
|
|
|
1273
1199
|
sourceAddress: this.cashaddr!,
|
|
1274
1200
|
fee,
|
|
1275
1201
|
discardChange,
|
|
1276
|
-
slpOutputs: [],
|
|
1277
1202
|
feePaidBy,
|
|
1278
1203
|
changeAddress,
|
|
1279
1204
|
buildUnsigned: options?.buildUnsigned === true,
|
|
@@ -1857,38 +1782,7 @@ export class TestNetWallet extends Wallet {
|
|
|
1857
1782
|
`${TestNetWallet.faucetServer}/faucet/get_addresses`
|
|
1858
1783
|
);
|
|
1859
1784
|
const data = response.data;
|
|
1860
|
-
return await this.
|
|
1861
|
-
} catch (e: any) {
|
|
1862
|
-
console.log(e);
|
|
1863
|
-
console.log(e.response ? e.response.data : "");
|
|
1864
|
-
throw e;
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
|
|
1868
|
-
// will receive 10 testnet tokens, rate limits apply
|
|
1869
|
-
async getTestnetSlp(tokenId: string): Promise<string> {
|
|
1870
|
-
try {
|
|
1871
|
-
const response = await axios.post(
|
|
1872
|
-
`${TestNetWallet.faucetServer}/faucet/get_testnet_slp`,
|
|
1873
|
-
{ slpaddr: this.slp.slpaddr, tokenId: tokenId }
|
|
1874
|
-
);
|
|
1875
|
-
const data = response.data;
|
|
1876
|
-
return data.txId;
|
|
1877
|
-
} catch (e) {
|
|
1878
|
-
//console.log(e);
|
|
1879
|
-
//console.log(e.response ? e.response.data : "");
|
|
1880
|
-
throw e;
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
// be nice and return them back
|
|
1885
|
-
async returnTestnetSlp(tokenId: string): Promise<SlpSendResponse> {
|
|
1886
|
-
try {
|
|
1887
|
-
const response = await axios.post(
|
|
1888
|
-
`${TestNetWallet.faucetServer}/faucet/get_addresses`
|
|
1889
|
-
);
|
|
1890
|
-
const data = response.data;
|
|
1891
|
-
return await this.slp.sendMax(data.slptest, tokenId);
|
|
1785
|
+
return await this.sendMax(data.bchtest);
|
|
1892
1786
|
} catch (e: any) {
|
|
1893
1787
|
console.log(e);
|
|
1894
1788
|
console.log(e.response ? e.response.data : "");
|
|
@@ -1896,11 +1790,6 @@ export class TestNetWallet extends Wallet {
|
|
|
1896
1790
|
}
|
|
1897
1791
|
}
|
|
1898
1792
|
|
|
1899
|
-
// interface to static slp functions. see Slp.ts
|
|
1900
|
-
public static get slp() {
|
|
1901
|
-
return TestNetSlp;
|
|
1902
|
-
}
|
|
1903
|
-
|
|
1904
1793
|
// interface to static util functions. see Util.ts
|
|
1905
1794
|
public static get util() {
|
|
1906
1795
|
return TestNetUtil;
|
|
@@ -1916,11 +1805,6 @@ export class RegTestWallet extends Wallet {
|
|
|
1916
1805
|
super(name, NetworkType.Regtest);
|
|
1917
1806
|
}
|
|
1918
1807
|
|
|
1919
|
-
// interface to static slp functions. see Slp.ts
|
|
1920
|
-
public static get slp() {
|
|
1921
|
-
return RegTestSlp;
|
|
1922
|
-
}
|
|
1923
|
-
|
|
1924
1808
|
// interface to static util functions. see Util.ts
|
|
1925
1809
|
public static get util() {
|
|
1926
1810
|
return RegTestUtil;
|
|
@@ -1937,11 +1821,6 @@ export class WifWallet extends Wallet {
|
|
|
1937
1821
|
super(name, NetworkType.Mainnet, WalletTypeEnum.Wif);
|
|
1938
1822
|
}
|
|
1939
1823
|
|
|
1940
|
-
// interface to static slp functions. see Slp.ts
|
|
1941
|
-
public static get slp() {
|
|
1942
|
-
return WifSlp;
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
1824
|
// interface to static util functions. see Util.ts
|
|
1946
1825
|
public static get util() {
|
|
1947
1826
|
return WifUtil;
|
|
@@ -1958,11 +1837,6 @@ export class TestNetWifWallet extends Wallet {
|
|
|
1958
1837
|
super(name, NetworkType.Testnet, WalletTypeEnum.Wif);
|
|
1959
1838
|
}
|
|
1960
1839
|
|
|
1961
|
-
// interface to static slp functions. see Slp.ts
|
|
1962
|
-
public static get slp() {
|
|
1963
|
-
return TestNetWifSlp;
|
|
1964
|
-
}
|
|
1965
|
-
|
|
1966
1840
|
// interface to static util functions. see Util.ts
|
|
1967
1841
|
public static get util() {
|
|
1968
1842
|
return TestNetWifUtil;
|
|
@@ -1979,11 +1853,6 @@ export class RegTestWifWallet extends Wallet {
|
|
|
1979
1853
|
super(name, NetworkType.Regtest, WalletTypeEnum.Wif);
|
|
1980
1854
|
}
|
|
1981
1855
|
|
|
1982
|
-
// interface to static slp functions. see Slp.ts
|
|
1983
|
-
public static get slp() {
|
|
1984
|
-
return RegTestWifSlp;
|
|
1985
|
-
}
|
|
1986
|
-
|
|
1987
1856
|
// interface to static util functions. see Util.ts
|
|
1988
1857
|
public static get util() {
|
|
1989
1858
|
return RegTestWifUtil;
|
|
@@ -2000,11 +1869,6 @@ export class WatchWallet extends Wallet {
|
|
|
2000
1869
|
super(name, NetworkType.Mainnet, WalletTypeEnum.Watch);
|
|
2001
1870
|
}
|
|
2002
1871
|
|
|
2003
|
-
// interface to static slp functions. see Slp.ts
|
|
2004
|
-
public static get slp() {
|
|
2005
|
-
return WatchSlp;
|
|
2006
|
-
}
|
|
2007
|
-
|
|
2008
1872
|
// interface to static util functions. see Util.ts
|
|
2009
1873
|
public static get util() {
|
|
2010
1874
|
return WatchUtil;
|
|
@@ -2021,11 +1885,6 @@ export class TestNetWatchWallet extends Wallet {
|
|
|
2021
1885
|
super(name, NetworkType.Testnet, WalletTypeEnum.Watch);
|
|
2022
1886
|
}
|
|
2023
1887
|
|
|
2024
|
-
// interface to static slp functions. see Slp.ts
|
|
2025
|
-
public static get slp() {
|
|
2026
|
-
return TestNetWatchSlp;
|
|
2027
|
-
}
|
|
2028
|
-
|
|
2029
1888
|
// interface to static util functions. see Util.ts
|
|
2030
1889
|
public static get util() {
|
|
2031
1890
|
return TestNetWatchUtil;
|
|
@@ -2042,11 +1901,6 @@ export class RegTestWatchWallet extends Wallet {
|
|
|
2042
1901
|
super(name, NetworkType.Regtest, WalletTypeEnum.Watch);
|
|
2043
1902
|
}
|
|
2044
1903
|
|
|
2045
|
-
// interface to static slp functions. see Slp.ts
|
|
2046
|
-
public static get slp() {
|
|
2047
|
-
return RegTestWatchSlp;
|
|
2048
|
-
}
|
|
2049
|
-
|
|
2050
1904
|
// interface to static util functions. see Util.ts
|
|
2051
1905
|
public static get util() {
|
|
2052
1906
|
return RegTestWatchUtil;
|
|
@@ -164,41 +164,6 @@ export async function createWallet(body: WalletRequestI): Promise<Wallet> {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
/**
|
|
168
|
-
* Create a new SLP aware wallet
|
|
169
|
-
* @param body A wallet request object
|
|
170
|
-
* @returns A promise to a new wallet object
|
|
171
|
-
*/
|
|
172
|
-
export async function createSlpWallet(body: WalletRequestI): Promise<Wallet> {
|
|
173
|
-
let wallet;
|
|
174
|
-
let walletType = body.type ? body.type : "seed";
|
|
175
|
-
let networkType = body.network ? body.network : "mainnet";
|
|
176
|
-
|
|
177
|
-
// Named wallets are saved in the database
|
|
178
|
-
if (body.name && body.name.length > 0) {
|
|
179
|
-
wallet = await walletClassMap[walletType][networkType]().slp.named(
|
|
180
|
-
body.name
|
|
181
|
-
);
|
|
182
|
-
if (wallet.network != networkType) {
|
|
183
|
-
throw Error(
|
|
184
|
-
`A wallet already exists with name ${body.name}, but with network ${wallet.network} not ${body.network}, per request`
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
if (wallet.walletType != walletType) {
|
|
188
|
-
throw Error(
|
|
189
|
-
`A wallet already exists with name ${body.name}, but with type ${wallet.walletType} not ${body.type}, per request`
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
return wallet;
|
|
193
|
-
}
|
|
194
|
-
// This handles unsaved/unnamed wallets
|
|
195
|
-
else {
|
|
196
|
-
wallet = await walletClassMap[walletType][networkType]().slp.newRandom();
|
|
197
|
-
wallet.walletType = walletType;
|
|
198
|
-
return wallet;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
167
|
/**
|
|
203
168
|
* Create a new wallet
|
|
204
169
|
* @param walletRequest A wallet request object
|
|
@@ -215,22 +180,6 @@ export async function createWalletResponse(
|
|
|
215
180
|
}
|
|
216
181
|
}
|
|
217
182
|
|
|
218
|
-
/**
|
|
219
|
-
* Create a new SLP aware wallet
|
|
220
|
-
* @param walletRequest A wallet request object
|
|
221
|
-
* @returns A new wallet object
|
|
222
|
-
*/
|
|
223
|
-
export async function createSlpWalletResponse(
|
|
224
|
-
walletRequest: WalletRequestI
|
|
225
|
-
): Promise<WalletResponseI> {
|
|
226
|
-
let wallet = await createSlpWallet(walletRequest);
|
|
227
|
-
if (wallet) {
|
|
228
|
-
return asJsonResponse(wallet);
|
|
229
|
-
} else {
|
|
230
|
-
throw Error("Error creating wallet");
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
183
|
/**
|
|
235
184
|
* asJsonResponse return a wallet as json
|
|
236
185
|
* @param wallet A wallet object
|
|
@@ -241,7 +190,6 @@ function asJsonResponse(wallet: Wallet): WalletResponseI {
|
|
|
241
190
|
return {
|
|
242
191
|
name: wallet.name,
|
|
243
192
|
cashaddr: wallet.cashaddr as string,
|
|
244
|
-
slpaddr: wallet.slp.slpaddr,
|
|
245
193
|
walletId: wallet.toString(),
|
|
246
194
|
...wallet.getSeed(),
|
|
247
195
|
network: wallet.network as any,
|
|
@@ -250,7 +198,6 @@ function asJsonResponse(wallet: Wallet): WalletResponseI {
|
|
|
250
198
|
return {
|
|
251
199
|
name: wallet.name,
|
|
252
200
|
cashaddr: wallet.cashaddr as string,
|
|
253
|
-
slpaddr: wallet.slp.slpaddr,
|
|
254
201
|
walletId: wallet.toString(),
|
|
255
202
|
wif: wallet.privateKeyWif,
|
|
256
203
|
network: wallet.network as any,
|
package/src/wallet/interface.ts
CHANGED
|
@@ -16,7 +16,6 @@ export interface WalletRequestI {
|
|
|
16
16
|
export interface WalletResponseI {
|
|
17
17
|
name: string;
|
|
18
18
|
cashaddr?: string;
|
|
19
|
-
slpaddr?: string;
|
|
20
19
|
address?: string;
|
|
21
20
|
walletId: string;
|
|
22
21
|
network?: NetworkEnum;
|
|
@@ -48,7 +47,6 @@ export interface WalletInfoI {
|
|
|
48
47
|
export interface SendRequestOptionsI {
|
|
49
48
|
utxoIds?: string[] | UtxoI[];
|
|
50
49
|
changeAddress?: string;
|
|
51
|
-
slpAware?: boolean; // a flag which activates utxo checking against an external slp indexer
|
|
52
50
|
slpSemiAware?: boolean; // a flag which requires an utxo to have more than 546 sats to be spendable and counted in the balance
|
|
53
51
|
queryBalance?: boolean;
|
|
54
52
|
awaitTransactionPropagation?: boolean;
|