mainnet-js 3.0.0-next.1 → 3.0.0-next.2
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.1.js → mainnet-3.0.0-next.2.js} +7 -7
- package/dist/module/history/getHistory.js +17 -17
- package/dist/module/history/getHistory.js.map +1 -1
- package/dist/module/history/interface.d.ts +1 -1
- package/dist/module/history/interface.d.ts.map +1 -1
- package/dist/module/interface.d.ts +5 -3
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +1 -3
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/interface.d.ts +2 -2
- package/dist/module/network/interface.d.ts.map +1 -1
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +25 -23
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/util/asSendRequestObject.js +2 -2
- package/dist/module/util/asSendRequestObject.js.map +1 -1
- package/dist/module/util/sumUtxoValue.js +1 -1
- package/dist/module/util/sumUtxoValue.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +18 -18
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +64 -69
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts +1 -1
- package/dist/module/wallet/Wif.d.ts +1 -1
- package/dist/module/wallet/model.d.ts +41 -25
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +10 -14
- package/dist/module/wallet/model.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/history/getHistory.ts +17 -17
- package/src/history/interface.ts +1 -1
- package/src/interface.ts +5 -3
- package/src/network/ElectrumNetworkProvider.ts +1 -3
- package/src/network/interface.ts +2 -2
- package/src/transaction/Wif.ts +27 -23
- package/src/util/asSendRequestObject.ts +2 -2
- package/src/util/sumUtxoValue.ts +1 -1
- package/src/wallet/Base.ts +67 -72
- package/src/wallet/Cashtokens.test.headless.js +217 -162
- package/src/wallet/Cashtokens.test.ts +470 -369
- package/src/wallet/HDWallet.test.ts +36 -24
- package/src/wallet/model.ts +49 -41
package/src/transaction/Wif.ts
CHANGED
|
@@ -153,14 +153,15 @@ export function prepareInputs({
|
|
|
153
153
|
|
|
154
154
|
const libAuthToken = i.token && {
|
|
155
155
|
amount: BigInt(i.token.amount),
|
|
156
|
-
category: hexToBin(i.token.
|
|
156
|
+
category: hexToBin(i.token.category),
|
|
157
157
|
nft:
|
|
158
|
-
i.token.capability !== undefined ||
|
|
158
|
+
i.token.nft?.capability !== undefined ||
|
|
159
|
+
i.token.nft?.commitment !== undefined
|
|
159
160
|
? {
|
|
160
|
-
capability: i.token.capability,
|
|
161
|
+
capability: i.token.nft?.capability,
|
|
161
162
|
commitment:
|
|
162
|
-
i.token.commitment !== undefined &&
|
|
163
|
-
hexToBin(i.token.commitment!),
|
|
163
|
+
i.token.nft?.commitment !== undefined &&
|
|
164
|
+
hexToBin(i.token.nft?.commitment!),
|
|
164
165
|
}
|
|
165
166
|
: undefined,
|
|
166
167
|
};
|
|
@@ -270,13 +271,14 @@ export function prepareTokenOutputs(request: TokenSendRequest): Output {
|
|
|
270
271
|
|
|
271
272
|
const libAuthToken = {
|
|
272
273
|
amount: BigInt(token.amount),
|
|
273
|
-
category: hexToBin(token.
|
|
274
|
+
category: hexToBin(token.category),
|
|
274
275
|
nft:
|
|
275
|
-
token.capability !== undefined || token.commitment !== undefined
|
|
276
|
+
token.nft?.capability !== undefined || token.nft?.commitment !== undefined
|
|
276
277
|
? {
|
|
277
|
-
capability: token.capability,
|
|
278
|
+
capability: token.nft?.capability,
|
|
278
279
|
commitment:
|
|
279
|
-
token.commitment !== undefined &&
|
|
280
|
+
token.nft?.commitment !== undefined &&
|
|
281
|
+
hexToBin(token.nft?.commitment!),
|
|
280
282
|
}
|
|
281
283
|
: undefined,
|
|
282
284
|
};
|
|
@@ -319,13 +321,13 @@ export async function getSuitableUtxos(
|
|
|
319
321
|
if (tokenOperation === "send") {
|
|
320
322
|
for (const request of tokenRequests) {
|
|
321
323
|
const tokenInputs = availableInputs.filter(
|
|
322
|
-
(val) => val.token?.
|
|
324
|
+
(val) => val.token?.category === request.category
|
|
323
325
|
);
|
|
324
326
|
const sameCommitmentTokens = [...suitableUtxos, ...tokenInputs]
|
|
325
327
|
.filter(
|
|
326
328
|
(val) =>
|
|
327
|
-
val.token?.capability === request.capability &&
|
|
328
|
-
val.token?.commitment === request.commitment
|
|
329
|
+
val.token?.nft?.capability === request.nft?.capability &&
|
|
330
|
+
val.token?.nft?.commitment === request.nft?.commitment
|
|
329
331
|
)
|
|
330
332
|
.filter(
|
|
331
333
|
(val) =>
|
|
@@ -348,13 +350,15 @@ export async function getSuitableUtxos(
|
|
|
348
350
|
}
|
|
349
351
|
|
|
350
352
|
if (
|
|
351
|
-
request.capability === NFTCapability.minting ||
|
|
352
|
-
request.capability === NFTCapability.mutable
|
|
353
|
+
request.nft?.capability === NFTCapability.minting ||
|
|
354
|
+
request.nft?.capability === NFTCapability.mutable
|
|
353
355
|
) {
|
|
354
356
|
const changeCommitmentTokens = [
|
|
355
357
|
...suitableUtxos,
|
|
356
358
|
...tokenInputs,
|
|
357
|
-
].filter(
|
|
359
|
+
].filter(
|
|
360
|
+
(val) => val.token?.nft?.capability === request.nft?.capability
|
|
361
|
+
);
|
|
358
362
|
if (changeCommitmentTokens.length) {
|
|
359
363
|
const input = changeCommitmentTokens[0];
|
|
360
364
|
const index = availableInputs.indexOf(input);
|
|
@@ -369,17 +373,17 @@ export async function getSuitableUtxos(
|
|
|
369
373
|
|
|
370
374
|
// handle splitting the hybrid (FT+NFT) token into its parts
|
|
371
375
|
if (
|
|
372
|
-
request.capability === undefined &&
|
|
373
|
-
request.commitment === undefined &&
|
|
376
|
+
request.nft?.capability === undefined &&
|
|
377
|
+
request.nft?.commitment === undefined &&
|
|
374
378
|
[...suitableUtxos, ...tokenInputs]
|
|
375
|
-
.map((val) => val.token?.
|
|
376
|
-
.includes(request.
|
|
379
|
+
.map((val) => val.token?.category)
|
|
380
|
+
.includes(request.category)
|
|
377
381
|
) {
|
|
378
382
|
continue;
|
|
379
383
|
}
|
|
380
384
|
|
|
381
385
|
throw Error(
|
|
382
|
-
`No suitable token utxos available to send token with id "${request.
|
|
386
|
+
`No suitable token utxos available to send token with id "${request.category}", capability "${request.nft?.capability}", commitment "${request.nft?.commitment}"`
|
|
383
387
|
);
|
|
384
388
|
}
|
|
385
389
|
}
|
|
@@ -461,20 +465,20 @@ export async function getFeeAmountSimple({
|
|
|
461
465
|
? inputSizeP2pkh +
|
|
462
466
|
1 +
|
|
463
467
|
34 +
|
|
464
|
-
Math.round(1 + (curr.token.commitment?.length ?? 0) / 2) +
|
|
468
|
+
Math.round(1 + (curr.token.nft?.commitment?.length ?? 0) / 2) +
|
|
465
469
|
(curr.token.amount ? 9 : 0)
|
|
466
470
|
: inputSizeP2pkh),
|
|
467
471
|
0
|
|
468
472
|
);
|
|
469
473
|
|
|
470
474
|
const outputSize = (sendRequest: SendRequestType) => {
|
|
471
|
-
if (sendRequest.hasOwnProperty("
|
|
475
|
+
if (sendRequest.hasOwnProperty("category")) {
|
|
472
476
|
const tokenRequest = sendRequest as TokenSendRequest;
|
|
473
477
|
return (
|
|
474
478
|
outputSizeP2pkh +
|
|
475
479
|
1 +
|
|
476
480
|
34 +
|
|
477
|
-
Math.round(1 + (tokenRequest.commitment?.length ?? 0) / 2) +
|
|
481
|
+
Math.round(1 + (tokenRequest.nft?.commitment?.length ?? 0) / 2) +
|
|
478
482
|
(tokenRequest.amount ? 9 : 0)
|
|
479
483
|
);
|
|
480
484
|
} else if (sendRequest.hasOwnProperty("buffer")) {
|
|
@@ -57,7 +57,7 @@ export function asSendRequestObject(
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
function convertToClass(object: SendRequest | TokenSendRequest | OpReturnData) {
|
|
60
|
-
if (object.hasOwnProperty("
|
|
60
|
+
if (object.hasOwnProperty("category")) {
|
|
61
61
|
return new TokenSendRequest(object as TokenSendRequest);
|
|
62
62
|
} else if (object.hasOwnProperty("buffer")) {
|
|
63
63
|
return OpReturnData.fromUint8Array((object as OpReturnData).buffer);
|
|
@@ -72,7 +72,7 @@ function convertToClass(object: SendRequest | TokenSendRequest | OpReturnData) {
|
|
|
72
72
|
} else if (
|
|
73
73
|
object.hasOwnProperty("cashaddr") &&
|
|
74
74
|
object.hasOwnProperty("value") &&
|
|
75
|
-
object.hasOwnProperty("
|
|
75
|
+
object.hasOwnProperty("category") === false
|
|
76
76
|
) {
|
|
77
77
|
return new SendRequest(object as SendRequest);
|
|
78
78
|
}
|
package/src/util/sumUtxoValue.ts
CHANGED
|
@@ -15,7 +15,7 @@ export function sumUtxoValue(utxos: Utxo[]) {
|
|
|
15
15
|
export function sumTokenAmounts(utxos: Utxo[], tokenId: string): bigint {
|
|
16
16
|
if (utxos.length > 0) {
|
|
17
17
|
const tokenArray: bigint[] = utxos
|
|
18
|
-
.filter((utxo) => utxo.token?.
|
|
18
|
+
.filter((utxo) => utxo.token?.category === tokenId)
|
|
19
19
|
.map((o: Utxo) => {
|
|
20
20
|
return o.token?.amount || 0n;
|
|
21
21
|
});
|
package/src/wallet/Base.ts
CHANGED
|
@@ -494,14 +494,14 @@ export class BaseWallet implements WalletI {
|
|
|
494
494
|
// sets up a callback to be called upon wallet's token balance change
|
|
495
495
|
// can be cancelled by calling the function returned from this one
|
|
496
496
|
public async watchTokenBalance(
|
|
497
|
-
|
|
497
|
+
category: string,
|
|
498
498
|
callback: (balance: bigint) => void
|
|
499
499
|
): Promise<CancelFn> {
|
|
500
500
|
let previous: bigint | undefined = undefined;
|
|
501
501
|
return await this.provider.watchAddressStatus(
|
|
502
502
|
this.getDepositAddress(),
|
|
503
503
|
async (_status: string) => {
|
|
504
|
-
const balance = await this.getTokenBalance(
|
|
504
|
+
const balance = await this.getTokenBalance(category);
|
|
505
505
|
if (previous != balance) {
|
|
506
506
|
callback(balance);
|
|
507
507
|
}
|
|
@@ -513,13 +513,13 @@ export class BaseWallet implements WalletI {
|
|
|
513
513
|
// waits for address token balance to be greater than or equal to the target amount
|
|
514
514
|
// this call halts the execution
|
|
515
515
|
public async waitForTokenBalance(
|
|
516
|
-
|
|
516
|
+
category: string,
|
|
517
517
|
amount: bigint
|
|
518
518
|
): Promise<bigint> {
|
|
519
519
|
return new Promise(async (resolve) => {
|
|
520
520
|
let watchCancel: CancelFn;
|
|
521
521
|
watchCancel = await this.watchTokenBalance(
|
|
522
|
-
|
|
522
|
+
category,
|
|
523
523
|
async (balance: bigint) => {
|
|
524
524
|
if (balance >= amount) {
|
|
525
525
|
await watchCancel?.();
|
|
@@ -632,11 +632,11 @@ export class BaseWallet implements WalletI {
|
|
|
632
632
|
| SendRequestArray[],
|
|
633
633
|
options?: SendRequestOptionsI
|
|
634
634
|
): Promise<SendResponse> {
|
|
635
|
-
const { encodedTransaction,
|
|
635
|
+
const { encodedTransaction, categories, sourceOutputs } =
|
|
636
636
|
await this.encodeTransaction(requests, undefined, options);
|
|
637
637
|
|
|
638
638
|
const resp = new SendResponse({});
|
|
639
|
-
resp.
|
|
639
|
+
resp.categories = categories;
|
|
640
640
|
|
|
641
641
|
if (options?.buildUnsigned !== true) {
|
|
642
642
|
const txId = await this.submitTransaction(
|
|
@@ -709,11 +709,11 @@ export class BaseWallet implements WalletI {
|
|
|
709
709
|
value: maxSpendableAmount,
|
|
710
710
|
});
|
|
711
711
|
|
|
712
|
-
const { encodedTransaction,
|
|
712
|
+
const { encodedTransaction, categories, sourceOutputs } =
|
|
713
713
|
await this.encodeTransaction([sendRequest], true, options, privateKey);
|
|
714
714
|
|
|
715
715
|
const resp = new SendResponse({});
|
|
716
|
-
resp.
|
|
716
|
+
resp.categories = categories;
|
|
717
717
|
|
|
718
718
|
if (options?.buildUnsigned !== true) {
|
|
719
719
|
const txId = await this.submitTransaction(
|
|
@@ -813,19 +813,19 @@ export class BaseWallet implements WalletI {
|
|
|
813
813
|
const allTokenOutputs = outputs.filter(
|
|
814
814
|
(val) => val instanceof TokenSendRequest
|
|
815
815
|
) as TokenSendRequest[];
|
|
816
|
-
const
|
|
817
|
-
.map((val) => val.
|
|
816
|
+
const categories = allTokenOutputs
|
|
817
|
+
.map((val) => val.category)
|
|
818
818
|
.filter((val, idx, arr) => arr.indexOf(val) === idx);
|
|
819
|
-
for (let
|
|
819
|
+
for (let category of categories) {
|
|
820
820
|
const tokenInputs = allTokenInputs.filter(
|
|
821
|
-
(val) => val.token?.
|
|
821
|
+
(val) => val.token?.category === category
|
|
822
822
|
);
|
|
823
823
|
const inputAmountSum = tokenInputs.reduce(
|
|
824
824
|
(prev, cur) => prev + cur.token!.amount,
|
|
825
825
|
0n
|
|
826
826
|
);
|
|
827
827
|
const tokenOutputs = allTokenOutputs.filter(
|
|
828
|
-
(val) => val.
|
|
828
|
+
(val) => val.category === category
|
|
829
829
|
);
|
|
830
830
|
const outputAmountSum = tokenOutputs.reduce(
|
|
831
831
|
(prev, cur) => prev + cur.amount,
|
|
@@ -867,9 +867,8 @@ export class BaseWallet implements WalletI {
|
|
|
867
867
|
new TokenSendRequest({
|
|
868
868
|
cashaddr: toTokenaddr(this.getChangeAddress()),
|
|
869
869
|
amount: change,
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
capability: tokenOutputs[0].capability,
|
|
870
|
+
category: category,
|
|
871
|
+
nft: tokenOutputs[0].nft,
|
|
873
872
|
value: tokenOutputs[0].value,
|
|
874
873
|
})
|
|
875
874
|
);
|
|
@@ -934,16 +933,16 @@ export class BaseWallet implements WalletI {
|
|
|
934
933
|
}
|
|
935
934
|
);
|
|
936
935
|
|
|
937
|
-
const
|
|
936
|
+
const categories = [
|
|
938
937
|
...fundingUtxos
|
|
939
|
-
.filter((val) => val.token?.
|
|
940
|
-
.map((val) => val.token!.
|
|
938
|
+
.filter((val) => val.token?.category)
|
|
939
|
+
.map((val) => val.token!.category),
|
|
941
940
|
...sendRequests
|
|
942
941
|
.filter((val) => val instanceof TokenSendRequest)
|
|
943
|
-
.map((val) => (val as TokenSendRequest).
|
|
942
|
+
.map((val) => (val as TokenSendRequest).category),
|
|
944
943
|
].filter((value, index, array) => array.indexOf(value) === index);
|
|
945
944
|
|
|
946
|
-
return { encodedTransaction,
|
|
945
|
+
return { encodedTransaction, categories, sourceOutputs };
|
|
947
946
|
}
|
|
948
947
|
|
|
949
948
|
// Submit a raw transaction
|
|
@@ -1131,9 +1130,8 @@ export class BaseWallet implements WalletI {
|
|
|
1131
1130
|
cashaddr: genesisRequest.cashaddr || this.getTokenDepositAddress(),
|
|
1132
1131
|
amount: genesisRequest.amount,
|
|
1133
1132
|
value: genesisRequest.value || 1000n,
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
tokenId: genesisInputs[0].txid,
|
|
1133
|
+
nft: genesisRequest.nft,
|
|
1134
|
+
category: genesisInputs[0].txid,
|
|
1137
1135
|
});
|
|
1138
1136
|
|
|
1139
1137
|
return this.send([genesisSendRequest, ...(sendRequests as any)], {
|
|
@@ -1149,7 +1147,7 @@ export class BaseWallet implements WalletI {
|
|
|
1149
1147
|
/**
|
|
1150
1148
|
* Mint new NFT cashtokens using an existing minting token
|
|
1151
1149
|
* Refer to spec https://github.com/bitjson/cashtokens
|
|
1152
|
-
* @param {string}
|
|
1150
|
+
* @param {string} category category of an NFT to mint
|
|
1153
1151
|
* @param {TokenMintRequest | TokenMintRequest[]} mintRequests mint requests with new token properties and recipients
|
|
1154
1152
|
* @param {NFTCapability?} mintRequest.capability capability of new NFT
|
|
1155
1153
|
* @param {string?} mintRequest.commitment NFT commitment message
|
|
@@ -1159,13 +1157,13 @@ export class BaseWallet implements WalletI {
|
|
|
1159
1157
|
* @param {SendRequestOptionsI} options Options of the send requests
|
|
1160
1158
|
*/
|
|
1161
1159
|
public async tokenMint(
|
|
1162
|
-
|
|
1160
|
+
category: string,
|
|
1163
1161
|
mintRequests: TokenMintRequest | Array<TokenMintRequest>,
|
|
1164
1162
|
deductTokenAmount: boolean = false,
|
|
1165
1163
|
options?: SendRequestOptionsI
|
|
1166
1164
|
): Promise<SendResponse> {
|
|
1167
|
-
if (
|
|
1168
|
-
throw Error(`Invalid
|
|
1165
|
+
if (category?.length !== 64) {
|
|
1166
|
+
throw Error(`Invalid category supplied: ${category}`);
|
|
1169
1167
|
}
|
|
1170
1168
|
|
|
1171
1169
|
if (!Array.isArray(mintRequests)) {
|
|
@@ -1175,12 +1173,12 @@ export class BaseWallet implements WalletI {
|
|
|
1175
1173
|
const utxos = await this.getUtxos();
|
|
1176
1174
|
const nftUtxos = utxos.filter(
|
|
1177
1175
|
(val) =>
|
|
1178
|
-
val.token?.
|
|
1179
|
-
val.token?.capability === NFTCapability.minting
|
|
1176
|
+
val.token?.category === category &&
|
|
1177
|
+
val.token?.nft?.capability === NFTCapability.minting
|
|
1180
1178
|
);
|
|
1181
1179
|
if (!nftUtxos.length) {
|
|
1182
1180
|
throw new Error(
|
|
1183
|
-
"You do not have any token UTXOs with minting capability for specified
|
|
1181
|
+
"You do not have any token UTXOs with minting capability for specified category"
|
|
1184
1182
|
);
|
|
1185
1183
|
}
|
|
1186
1184
|
const newAmount =
|
|
@@ -1190,9 +1188,8 @@ export class BaseWallet implements WalletI {
|
|
|
1190
1188
|
const safeNewAmount = newAmount < 0n ? 0n : newAmount;
|
|
1191
1189
|
const mintingInput = new TokenSendRequest({
|
|
1192
1190
|
cashaddr: toTokenaddr(nftUtxos[0].address),
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
commitment: nftUtxos[0].token!.commitment,
|
|
1191
|
+
category: category,
|
|
1192
|
+
nft: nftUtxos[0].token?.nft,
|
|
1196
1193
|
amount: safeNewAmount,
|
|
1197
1194
|
value: nftUtxos[0].satoshis,
|
|
1198
1195
|
});
|
|
@@ -1204,10 +1201,9 @@ export class BaseWallet implements WalletI {
|
|
|
1204
1201
|
new TokenSendRequest({
|
|
1205
1202
|
cashaddr: val.cashaddr || this.getTokenDepositAddress(),
|
|
1206
1203
|
amount: 0n,
|
|
1207
|
-
|
|
1204
|
+
category: category,
|
|
1208
1205
|
value: val.value,
|
|
1209
|
-
|
|
1210
|
-
commitment: val.commitment,
|
|
1206
|
+
nft: val.nft,
|
|
1211
1207
|
})
|
|
1212
1208
|
),
|
|
1213
1209
|
],
|
|
@@ -1229,7 +1225,7 @@ export class BaseWallet implements WalletI {
|
|
|
1229
1225
|
* * FTs' amount is reduced by the amount specified, if 0 FT amount is left and no NFT present, the token is "destroyed"
|
|
1230
1226
|
*
|
|
1231
1227
|
* Refer to spec https://github.com/bitjson/cashtokens
|
|
1232
|
-
* @param {string} burnRequest.
|
|
1228
|
+
* @param {string} burnRequest.category category of a token to burn
|
|
1233
1229
|
* @param {NFTCapability} burnRequest.capability capability of the NFT token to select, optional
|
|
1234
1230
|
* @param {string?} burnRequest.commitment commitment of the NFT token to select, optional
|
|
1235
1231
|
* @param {number?} burnRequest.amount amount of fungible tokens to burn, optional
|
|
@@ -1242,16 +1238,16 @@ export class BaseWallet implements WalletI {
|
|
|
1242
1238
|
message?: string,
|
|
1243
1239
|
options?: SendRequestOptionsI
|
|
1244
1240
|
): Promise<SendResponse> {
|
|
1245
|
-
if (burnRequest.
|
|
1246
|
-
throw Error(`Invalid
|
|
1241
|
+
if (burnRequest.category?.length !== 64) {
|
|
1242
|
+
throw Error(`Invalid category supplied: ${burnRequest.category}`);
|
|
1247
1243
|
}
|
|
1248
1244
|
|
|
1249
1245
|
const utxos = await this.getUtxos();
|
|
1250
1246
|
const tokenUtxos = utxos.filter(
|
|
1251
1247
|
(val) =>
|
|
1252
|
-
val.token?.
|
|
1253
|
-
val.token?.capability === burnRequest.capability &&
|
|
1254
|
-
val.token?.commitment === burnRequest.commitment
|
|
1248
|
+
val.token?.category === burnRequest.category &&
|
|
1249
|
+
val.token?.nft?.capability === burnRequest.nft?.capability &&
|
|
1250
|
+
val.token?.nft?.commitment === burnRequest.nft?.commitment
|
|
1255
1251
|
);
|
|
1256
1252
|
|
|
1257
1253
|
if (!tokenUtxos.length) {
|
|
@@ -1265,7 +1261,7 @@ export class BaseWallet implements WalletI {
|
|
|
1265
1261
|
let fungibleBurnAmount =
|
|
1266
1262
|
burnRequest.amount && burnRequest.amount > 0 ? burnRequest.amount! : 0n;
|
|
1267
1263
|
fungibleBurnAmount = BigInt(fungibleBurnAmount);
|
|
1268
|
-
const hasNFT = burnRequest.
|
|
1264
|
+
const hasNFT = burnRequest.nft !== undefined;
|
|
1269
1265
|
|
|
1270
1266
|
let utxoIds: Utxo[] = [];
|
|
1271
1267
|
let changeSendRequests: TokenSendRequest[];
|
|
@@ -1292,9 +1288,8 @@ export class BaseWallet implements WalletI {
|
|
|
1292
1288
|
new TokenSendRequest({
|
|
1293
1289
|
cashaddr:
|
|
1294
1290
|
burnRequest.cashaddr || toTokenaddr(this.getChangeAddress()),
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
commitment: burnRequest.commitment,
|
|
1291
|
+
category: burnRequest.category,
|
|
1292
|
+
nft: burnRequest.nft,
|
|
1298
1293
|
amount: safeNewAmount,
|
|
1299
1294
|
value: tokenUtxos[0].satoshis,
|
|
1300
1295
|
}),
|
|
@@ -1323,7 +1318,7 @@ export class BaseWallet implements WalletI {
|
|
|
1323
1318
|
new TokenSendRequest({
|
|
1324
1319
|
cashaddr:
|
|
1325
1320
|
burnRequest.cashaddr || toTokenaddr(this.getChangeAddress()),
|
|
1326
|
-
|
|
1321
|
+
category: burnRequest.category,
|
|
1327
1322
|
amount: safeNewAmount,
|
|
1328
1323
|
value: tokenUtxos.reduce((a, c) => a + c.satoshis, 0n),
|
|
1329
1324
|
}),
|
|
@@ -1343,78 +1338,78 @@ export class BaseWallet implements WalletI {
|
|
|
1343
1338
|
|
|
1344
1339
|
/**
|
|
1345
1340
|
* getTokenUtxos Get unspent token outputs for the wallet
|
|
1346
|
-
* will return utxos only for the specified token if `
|
|
1347
|
-
* @param {string?}
|
|
1341
|
+
* will return utxos only for the specified token if `category` provided
|
|
1342
|
+
* @param {string?} category category to filter utxos by, if not set will return utxos from all tokens
|
|
1348
1343
|
* @returns {Utxo[]} token utxos
|
|
1349
1344
|
*/
|
|
1350
|
-
public async getTokenUtxos(
|
|
1345
|
+
public async getTokenUtxos(category?: string): Promise<Utxo[]> {
|
|
1351
1346
|
const utxos = await this.getUtxos();
|
|
1352
1347
|
return utxos.filter((val) =>
|
|
1353
|
-
|
|
1348
|
+
category ? val.token?.category === category : val.token
|
|
1354
1349
|
);
|
|
1355
1350
|
}
|
|
1356
1351
|
|
|
1357
1352
|
/**
|
|
1358
1353
|
* getTokenBalance Gets fungible token balance
|
|
1359
1354
|
* for NFT token balance see @ref getNftTokenBalance
|
|
1360
|
-
* @param {string}
|
|
1355
|
+
* @param {string} category category to get balance for
|
|
1361
1356
|
* @returns {bigint} fungible token balance
|
|
1362
1357
|
*/
|
|
1363
|
-
public async getTokenBalance(
|
|
1364
|
-
const utxos = (await this.getTokenUtxos(
|
|
1358
|
+
public async getTokenBalance(category: string): Promise<bigint> {
|
|
1359
|
+
const utxos = (await this.getTokenUtxos(category)).filter(
|
|
1365
1360
|
(val) => val.token?.amount
|
|
1366
1361
|
);
|
|
1367
|
-
return sumTokenAmounts(utxos,
|
|
1362
|
+
return sumTokenAmounts(utxos, category);
|
|
1368
1363
|
}
|
|
1369
1364
|
|
|
1370
1365
|
/**
|
|
1371
|
-
* getNftTokenBalance Gets non-fungible token (NFT) balance for a particular
|
|
1366
|
+
* getNftTokenBalance Gets non-fungible token (NFT) balance for a particular category
|
|
1372
1367
|
* disregards fungible token balances
|
|
1373
1368
|
* for fungible token balance see @ref getTokenBalance
|
|
1374
|
-
* @param {string}
|
|
1369
|
+
* @param {string} category category to get balance for
|
|
1375
1370
|
* @returns {number} non-fungible token balance
|
|
1376
1371
|
*/
|
|
1377
|
-
public async getNftTokenBalance(
|
|
1378
|
-
const utxos = (await this.getTokenUtxos(
|
|
1379
|
-
(val) => val.token?.commitment !== undefined
|
|
1372
|
+
public async getNftTokenBalance(category: string): Promise<number> {
|
|
1373
|
+
const utxos = (await this.getTokenUtxos(category)).filter(
|
|
1374
|
+
(val) => val.token?.nft?.commitment !== undefined
|
|
1380
1375
|
);
|
|
1381
1376
|
return utxos.length;
|
|
1382
1377
|
}
|
|
1383
1378
|
|
|
1384
1379
|
/**
|
|
1385
1380
|
* getAllTokenBalances Gets all fungible token balances in this wallet
|
|
1386
|
-
* @returns {Object} a map [
|
|
1381
|
+
* @returns {Object} a map [category => balance] for all tokens in this wallet
|
|
1387
1382
|
*/
|
|
1388
|
-
public async getAllTokenBalances(): Promise<{ [
|
|
1383
|
+
public async getAllTokenBalances(): Promise<{ [category: string]: bigint }> {
|
|
1389
1384
|
const result = {};
|
|
1390
1385
|
const utxos = (await this.getTokenUtxos()).filter(
|
|
1391
1386
|
(val) => val.token?.amount
|
|
1392
1387
|
);
|
|
1393
1388
|
for (const utxo of utxos) {
|
|
1394
|
-
if (!result[utxo.token!.
|
|
1395
|
-
result[utxo.token!.
|
|
1389
|
+
if (!result[utxo.token!.category]) {
|
|
1390
|
+
result[utxo.token!.category] = 0n;
|
|
1396
1391
|
}
|
|
1397
|
-
result[utxo.token!.
|
|
1392
|
+
result[utxo.token!.category] += utxo.token!.amount;
|
|
1398
1393
|
}
|
|
1399
1394
|
return result;
|
|
1400
1395
|
}
|
|
1401
1396
|
|
|
1402
1397
|
/**
|
|
1403
1398
|
* getAllNftTokenBalances Gets all non-fungible token (NFT) balances in this wallet
|
|
1404
|
-
* @returns {Object} a map [
|
|
1399
|
+
* @returns {Object} a map [category => balance] for all NFTs in this wallet
|
|
1405
1400
|
*/
|
|
1406
1401
|
public async getAllNftTokenBalances(): Promise<{
|
|
1407
|
-
[
|
|
1402
|
+
[category: string]: number;
|
|
1408
1403
|
}> {
|
|
1409
1404
|
const result = {};
|
|
1410
1405
|
const utxos = (await this.getTokenUtxos()).filter(
|
|
1411
|
-
(val) => val.token?.commitment !== undefined
|
|
1406
|
+
(val) => val.token?.nft?.commitment !== undefined
|
|
1412
1407
|
);
|
|
1413
1408
|
for (const utxo of utxos) {
|
|
1414
|
-
if (!result[utxo.token!.
|
|
1415
|
-
result[utxo.token!.
|
|
1409
|
+
if (!result[utxo.token!.category]) {
|
|
1410
|
+
result[utxo.token!.category] = 0;
|
|
1416
1411
|
}
|
|
1417
|
-
result[utxo.token!.
|
|
1412
|
+
result[utxo.token!.category] += 1;
|
|
1418
1413
|
}
|
|
1419
1414
|
return result;
|
|
1420
1415
|
}
|