@buildonspark/issuer-sdk 0.0.83 → 0.0.85
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/CHANGELOG.md +21 -0
- package/dist/chunk-HOLMXKFE.js +663 -0
- package/dist/index.cjs +61 -64
- package/dist/index.d.cts +8 -156
- package/dist/index.d.ts +8 -156
- package/dist/index.js +13 -669
- package/dist/index.node.cjs +708 -0
- package/dist/index.node.d.cts +14 -0
- package/dist/index.node.d.ts +14 -0
- package/dist/index.node.js +34 -0
- package/dist/issuer-spark-wallet-BFzkv4X5.d.cts +164 -0
- package/dist/issuer-spark-wallet-BFzkv4X5.d.ts +164 -0
- package/package.json +23 -5
- package/src/index.node.ts +2 -0
- package/src/index.ts +1 -1
- package/src/issuer-wallet/issuer-spark-wallet.browser.ts +33 -0
- package/src/issuer-wallet/issuer-spark-wallet.node.ts +33 -0
- package/src/issuer-wallet/issuer-spark-wallet.ts +46 -66
- package/src/tests/integration/spark.test.ts +434 -375
- package/src/tests/stress/transfers.test.ts +12 -19
- package/src/tests/utils/issuer-test-wallet.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @buildonspark/issuer-sdk
|
|
2
2
|
|
|
3
|
+
## 0.0.85
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- -- Opentelemetry improvements
|
|
8
|
+
-- Utility function to decode bech32mtokenidentifiers to raw token identifiers
|
|
9
|
+
-- Add userRequest to transfer in getTransfer() if it exists
|
|
10
|
+
-- Fixes to getIssuerTokenIdentifier() types
|
|
11
|
+
-- Migrates some internal filtering logic to key on token identifiers
|
|
12
|
+
-- Testing improvements
|
|
13
|
+
- Updated dependencies
|
|
14
|
+
- @buildonspark/lrc20-sdk@0.0.61
|
|
15
|
+
- @buildonspark/spark-sdk@0.2.6
|
|
16
|
+
|
|
17
|
+
## 0.0.84
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies
|
|
22
|
+
- @buildonspark/spark-sdk@0.2.5
|
|
23
|
+
|
|
3
24
|
## 0.0.83
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Buffer
|
|
3
|
+
} from "./chunk-7B4B24XF.js";
|
|
4
|
+
|
|
5
|
+
// src/issuer-wallet/issuer-spark-wallet.ts
|
|
6
|
+
import { TokenPubkey, TokenPubkeyAnnouncement } from "@buildonspark/lrc20-sdk";
|
|
7
|
+
import {
|
|
8
|
+
NetworkError as NetworkError2,
|
|
9
|
+
SparkWallet,
|
|
10
|
+
ValidationError as ValidationError3
|
|
11
|
+
} from "@buildonspark/spark-sdk";
|
|
12
|
+
import {
|
|
13
|
+
decodeSparkAddress,
|
|
14
|
+
encodeSparkAddress
|
|
15
|
+
} from "@buildonspark/spark-sdk";
|
|
16
|
+
import {
|
|
17
|
+
bytesToHex,
|
|
18
|
+
bytesToNumberBE,
|
|
19
|
+
hexToBytes as hexToBytes2
|
|
20
|
+
} from "@noble/curves/abstract/utils";
|
|
21
|
+
import { decodeBech32mTokenIdentifier } from "@buildonspark/spark-sdk";
|
|
22
|
+
|
|
23
|
+
// src/services/freeze.ts
|
|
24
|
+
import {
|
|
25
|
+
NetworkError,
|
|
26
|
+
collectResponses
|
|
27
|
+
} from "@buildonspark/spark-sdk";
|
|
28
|
+
|
|
29
|
+
// src/utils/token-hashing.ts
|
|
30
|
+
import { sha256 } from "@scure/btc-signer/utils";
|
|
31
|
+
import { ValidationError } from "@buildonspark/spark-sdk";
|
|
32
|
+
function hashFreezeTokensPayload(payload) {
|
|
33
|
+
if (!payload) {
|
|
34
|
+
throw new ValidationError("Freeze tokens payload cannot be nil", {
|
|
35
|
+
field: "payload",
|
|
36
|
+
value: payload,
|
|
37
|
+
expected: "valid freeze tokens payload"
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
let allHashes = [];
|
|
41
|
+
const versionHashObj = sha256.create();
|
|
42
|
+
const versionBytes = new Uint8Array(4);
|
|
43
|
+
new DataView(versionBytes.buffer).setUint32(
|
|
44
|
+
0,
|
|
45
|
+
payload.version,
|
|
46
|
+
false
|
|
47
|
+
// false for big-endian
|
|
48
|
+
);
|
|
49
|
+
versionHashObj.update(versionBytes);
|
|
50
|
+
allHashes.push(versionHashObj.digest());
|
|
51
|
+
const ownerPubKeyHash = sha256.create();
|
|
52
|
+
if (payload.ownerPublicKey) {
|
|
53
|
+
ownerPubKeyHash.update(payload.ownerPublicKey);
|
|
54
|
+
}
|
|
55
|
+
allHashes.push(ownerPubKeyHash.digest());
|
|
56
|
+
const tokenIdentifierHash = sha256.create();
|
|
57
|
+
if (payload.tokenIdentifier) {
|
|
58
|
+
tokenIdentifierHash.update(payload.tokenIdentifier);
|
|
59
|
+
}
|
|
60
|
+
allHashes.push(tokenIdentifierHash.digest());
|
|
61
|
+
const shouldUnfreezeHash = sha256.create();
|
|
62
|
+
shouldUnfreezeHash.update(new Uint8Array([payload.shouldUnfreeze ? 1 : 0]));
|
|
63
|
+
allHashes.push(shouldUnfreezeHash.digest());
|
|
64
|
+
const timestampHash = sha256.create();
|
|
65
|
+
if (payload.issuerProvidedTimestamp) {
|
|
66
|
+
const timestampBytes = new Uint8Array(8);
|
|
67
|
+
new DataView(timestampBytes.buffer).setBigUint64(
|
|
68
|
+
0,
|
|
69
|
+
BigInt(payload.issuerProvidedTimestamp),
|
|
70
|
+
true
|
|
71
|
+
// true for little-endian
|
|
72
|
+
);
|
|
73
|
+
timestampHash.update(timestampBytes);
|
|
74
|
+
}
|
|
75
|
+
allHashes.push(timestampHash.digest());
|
|
76
|
+
const operatorPubKeyHash = sha256.create();
|
|
77
|
+
if (payload.operatorIdentityPublicKey) {
|
|
78
|
+
operatorPubKeyHash.update(payload.operatorIdentityPublicKey);
|
|
79
|
+
}
|
|
80
|
+
allHashes.push(operatorPubKeyHash.digest());
|
|
81
|
+
const finalHash = sha256.create();
|
|
82
|
+
for (const hash of allHashes) {
|
|
83
|
+
finalHash.update(hash);
|
|
84
|
+
}
|
|
85
|
+
return finalHash.digest();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/services/freeze.ts
|
|
89
|
+
import { hexToBytes } from "@noble/curves/abstract/utils";
|
|
90
|
+
var TokenFreezeService = class {
|
|
91
|
+
config;
|
|
92
|
+
connectionManager;
|
|
93
|
+
constructor(config, connectionManager) {
|
|
94
|
+
this.config = config;
|
|
95
|
+
this.connectionManager = connectionManager;
|
|
96
|
+
}
|
|
97
|
+
async freezeTokens({
|
|
98
|
+
ownerPublicKey,
|
|
99
|
+
tokenIdentifier
|
|
100
|
+
}) {
|
|
101
|
+
return this.freezeOperation(ownerPublicKey, false, tokenIdentifier);
|
|
102
|
+
}
|
|
103
|
+
async unfreezeTokens({
|
|
104
|
+
ownerPublicKey,
|
|
105
|
+
tokenIdentifier
|
|
106
|
+
}) {
|
|
107
|
+
return this.freezeOperation(ownerPublicKey, true, tokenIdentifier);
|
|
108
|
+
}
|
|
109
|
+
async freezeOperation(ownerPublicKey, shouldUnfreeze, tokenIdentifier) {
|
|
110
|
+
const signingOperators = this.config.getSigningOperators();
|
|
111
|
+
const issuerProvidedTimestamp = Date.now();
|
|
112
|
+
const freezeResponses = await Promise.allSettled(
|
|
113
|
+
Object.entries(signingOperators).map(async ([identifier, operator]) => {
|
|
114
|
+
const sparkTokenClient = await this.connectionManager.createSparkTokenClient(operator.address);
|
|
115
|
+
const freezeTokensPayload = {
|
|
116
|
+
version: 1,
|
|
117
|
+
ownerPublicKey,
|
|
118
|
+
tokenIdentifier,
|
|
119
|
+
shouldUnfreeze,
|
|
120
|
+
issuerProvidedTimestamp,
|
|
121
|
+
operatorIdentityPublicKey: hexToBytes(operator.identityPublicKey)
|
|
122
|
+
};
|
|
123
|
+
const hashedPayload = hashFreezeTokensPayload(freezeTokensPayload);
|
|
124
|
+
const issuerSignature = await this.config.signer.signMessageWithIdentityKey(hashedPayload);
|
|
125
|
+
try {
|
|
126
|
+
const response = await sparkTokenClient.freeze_tokens({
|
|
127
|
+
freezeTokensPayload,
|
|
128
|
+
issuerSignature
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
identifier,
|
|
132
|
+
response
|
|
133
|
+
};
|
|
134
|
+
} catch (error) {
|
|
135
|
+
throw new NetworkError(
|
|
136
|
+
`Failed to send a freeze/unfreeze operation to operator: ${operator.address}`,
|
|
137
|
+
{
|
|
138
|
+
operation: "freeze_tokens",
|
|
139
|
+
errorCount: 1,
|
|
140
|
+
errors: error instanceof Error ? error.message : String(error)
|
|
141
|
+
},
|
|
142
|
+
error instanceof Error ? error : void 0
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
const successfulResponses = collectResponses(freezeResponses);
|
|
148
|
+
return successfulResponses[0].response;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// src/services/token-transactions.ts
|
|
153
|
+
import { TokenTransactionService } from "@buildonspark/spark-sdk";
|
|
154
|
+
import { numberToBytesBE } from "@noble/curves/abstract/utils";
|
|
155
|
+
var IssuerTokenTransactionService = class extends TokenTransactionService {
|
|
156
|
+
constructor(config, connectionManager) {
|
|
157
|
+
super(config, connectionManager);
|
|
158
|
+
}
|
|
159
|
+
async constructMintTokenTransactionV0(tokenPublicKey, tokenAmount) {
|
|
160
|
+
return {
|
|
161
|
+
network: this.config.getNetworkProto(),
|
|
162
|
+
tokenInputs: {
|
|
163
|
+
$case: "mintInput",
|
|
164
|
+
mintInput: {
|
|
165
|
+
issuerPublicKey: tokenPublicKey,
|
|
166
|
+
issuerProvidedTimestamp: Date.now()
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
tokenOutputs: [
|
|
170
|
+
{
|
|
171
|
+
ownerPublicKey: tokenPublicKey,
|
|
172
|
+
tokenPublicKey,
|
|
173
|
+
tokenAmount: numberToBytesBE(tokenAmount, 16)
|
|
174
|
+
}
|
|
175
|
+
],
|
|
176
|
+
sparkOperatorIdentityPublicKeys: super.collectOperatorIdentityPublicKeys()
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
async constructMintTokenTransaction(rawTokenIdentifierBytes, issuerTokenPublicKey, tokenAmount) {
|
|
180
|
+
return {
|
|
181
|
+
version: 1,
|
|
182
|
+
network: this.config.getNetworkProto(),
|
|
183
|
+
tokenInputs: {
|
|
184
|
+
$case: "mintInput",
|
|
185
|
+
mintInput: {
|
|
186
|
+
issuerPublicKey: issuerTokenPublicKey,
|
|
187
|
+
tokenIdentifier: rawTokenIdentifierBytes
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
tokenOutputs: [
|
|
191
|
+
{
|
|
192
|
+
ownerPublicKey: issuerTokenPublicKey,
|
|
193
|
+
tokenIdentifier: rawTokenIdentifierBytes,
|
|
194
|
+
tokenAmount: numberToBytesBE(tokenAmount, 16)
|
|
195
|
+
}
|
|
196
|
+
],
|
|
197
|
+
clientCreatedTimestamp: /* @__PURE__ */ new Date(),
|
|
198
|
+
sparkOperatorIdentityPublicKeys: super.collectOperatorIdentityPublicKeys(),
|
|
199
|
+
expiryTime: void 0
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async constructCreateTokenTransaction(tokenPublicKey, tokenName, tokenTicker, decimals, maxSupply, isFreezable) {
|
|
203
|
+
return {
|
|
204
|
+
version: 1,
|
|
205
|
+
network: this.config.getNetworkProto(),
|
|
206
|
+
tokenInputs: {
|
|
207
|
+
$case: "createInput",
|
|
208
|
+
createInput: {
|
|
209
|
+
issuerPublicKey: tokenPublicKey,
|
|
210
|
+
tokenName,
|
|
211
|
+
tokenTicker,
|
|
212
|
+
decimals,
|
|
213
|
+
maxSupply: numberToBytesBE(maxSupply, 16),
|
|
214
|
+
isFreezable
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
tokenOutputs: [],
|
|
218
|
+
clientCreatedTimestamp: /* @__PURE__ */ new Date(),
|
|
219
|
+
sparkOperatorIdentityPublicKeys: super.collectOperatorIdentityPublicKeys(),
|
|
220
|
+
expiryTime: void 0
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// src/issuer-wallet/issuer-spark-wallet.ts
|
|
226
|
+
import { NotImplementedError } from "@buildonspark/spark-sdk";
|
|
227
|
+
|
|
228
|
+
// src/utils/create-validation.ts
|
|
229
|
+
import { ValidationError as ValidationError2 } from "@buildonspark/spark-sdk";
|
|
230
|
+
function isNfcNormalized(value) {
|
|
231
|
+
return value.normalize("NFC") === value;
|
|
232
|
+
}
|
|
233
|
+
var MIN_NAME_SIZE = 3;
|
|
234
|
+
var MAX_NAME_SIZE = 20;
|
|
235
|
+
var MIN_SYMBOL_SIZE = 3;
|
|
236
|
+
var MAX_SYMBOL_SIZE = 6;
|
|
237
|
+
var MAX_DECIMALS = 255;
|
|
238
|
+
var MAXIMUM_MAX_SUPPLY = (1n << 128n) - 1n;
|
|
239
|
+
function validateTokenParameters(tokenName, tokenTicker, decimals, maxSupply) {
|
|
240
|
+
if (!isNfcNormalized(tokenName)) {
|
|
241
|
+
throw new ValidationError2("Token name must be NFC-normalised UTF-8", {
|
|
242
|
+
field: "tokenName",
|
|
243
|
+
value: tokenName,
|
|
244
|
+
expected: "NFC normalised string"
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
if (!isNfcNormalized(tokenTicker)) {
|
|
248
|
+
throw new ValidationError2("Token ticker must be NFC-normalised UTF-8", {
|
|
249
|
+
field: "tokenTicker",
|
|
250
|
+
value: tokenTicker,
|
|
251
|
+
expected: "NFC normalised string"
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const nameBytes = Buffer.from(tokenName, "utf-8").length;
|
|
255
|
+
if (nameBytes < MIN_NAME_SIZE || nameBytes > MAX_NAME_SIZE) {
|
|
256
|
+
throw new ValidationError2(
|
|
257
|
+
`Token name must be between ${MIN_NAME_SIZE} and ${MAX_NAME_SIZE} bytes`,
|
|
258
|
+
{
|
|
259
|
+
field: "tokenName",
|
|
260
|
+
value: tokenName,
|
|
261
|
+
actualLength: nameBytes,
|
|
262
|
+
expected: `>=${MIN_NAME_SIZE} and <=${MAX_NAME_SIZE}`
|
|
263
|
+
}
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
const tickerBytes = Buffer.from(tokenTicker, "utf-8").length;
|
|
267
|
+
if (tickerBytes < MIN_SYMBOL_SIZE || tickerBytes > MAX_SYMBOL_SIZE) {
|
|
268
|
+
throw new ValidationError2(
|
|
269
|
+
`Token ticker must be between ${MIN_SYMBOL_SIZE} and ${MAX_SYMBOL_SIZE} bytes`,
|
|
270
|
+
{
|
|
271
|
+
field: "tokenTicker",
|
|
272
|
+
value: tokenTicker,
|
|
273
|
+
actualLength: tickerBytes,
|
|
274
|
+
expected: `>=${MIN_SYMBOL_SIZE} and <=${MAX_SYMBOL_SIZE}`
|
|
275
|
+
}
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (!Number.isSafeInteger(decimals) || decimals < 0 || decimals > MAX_DECIMALS) {
|
|
279
|
+
throw new ValidationError2(
|
|
280
|
+
`Decimals must be an integer between 0 and ${MAX_DECIMALS}`,
|
|
281
|
+
{
|
|
282
|
+
field: "decimals",
|
|
283
|
+
value: decimals,
|
|
284
|
+
expected: `>=0 and <=${MAX_DECIMALS}`
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
if (maxSupply < 0n || maxSupply > MAXIMUM_MAX_SUPPLY) {
|
|
289
|
+
throw new ValidationError2(`maxSupply must be between 0 and 2^128-1`, {
|
|
290
|
+
field: "maxSupply",
|
|
291
|
+
value: maxSupply.toString(),
|
|
292
|
+
expected: `>=0 and <=${MAXIMUM_MAX_SUPPLY.toString()}`
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/issuer-wallet/issuer-spark-wallet.ts
|
|
298
|
+
import {
|
|
299
|
+
encodeBech32mTokenIdentifier
|
|
300
|
+
} from "@buildonspark/spark-sdk";
|
|
301
|
+
var BURN_ADDRESS = "02".repeat(33);
|
|
302
|
+
var IssuerSparkWallet = class _IssuerSparkWallet extends SparkWallet {
|
|
303
|
+
issuerTokenTransactionService;
|
|
304
|
+
tokenFreezeService;
|
|
305
|
+
tracerId = "issuer-sdk";
|
|
306
|
+
/**
|
|
307
|
+
* Initializes a new IssuerSparkWallet instance.
|
|
308
|
+
* @param options - Configuration options for the wallet
|
|
309
|
+
* @returns An object containing the initialized wallet and initialization response
|
|
310
|
+
*/
|
|
311
|
+
static async initialize({
|
|
312
|
+
mnemonicOrSeed,
|
|
313
|
+
accountNumber,
|
|
314
|
+
signer,
|
|
315
|
+
options
|
|
316
|
+
}) {
|
|
317
|
+
const wallet = new _IssuerSparkWallet(options, signer);
|
|
318
|
+
wallet.initializeTracer(wallet);
|
|
319
|
+
const initResponse = await wallet.initWallet(mnemonicOrSeed, accountNumber);
|
|
320
|
+
return {
|
|
321
|
+
wallet,
|
|
322
|
+
...initResponse
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
constructor(configOptions, signer) {
|
|
326
|
+
super(configOptions, signer);
|
|
327
|
+
this.issuerTokenTransactionService = new IssuerTokenTransactionService(
|
|
328
|
+
this.config,
|
|
329
|
+
this.connectionManager
|
|
330
|
+
);
|
|
331
|
+
this.tokenFreezeService = new TokenFreezeService(
|
|
332
|
+
this.config,
|
|
333
|
+
this.connectionManager
|
|
334
|
+
);
|
|
335
|
+
this.wrapIssuerSparkWalletMethodsWithTracing();
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Gets the token balance for the issuer's token.
|
|
339
|
+
* @returns An object containing the token balance as a bigint
|
|
340
|
+
*/
|
|
341
|
+
async getIssuerTokenBalance() {
|
|
342
|
+
const publicKey = await super.getIdentityPublicKey();
|
|
343
|
+
const balanceObj = await this.getBalance();
|
|
344
|
+
const issuerBalance = [...balanceObj.tokenBalances.entries()].find(
|
|
345
|
+
([, info]) => info.tokenMetadata.tokenPublicKey === publicKey
|
|
346
|
+
);
|
|
347
|
+
if (!balanceObj.tokenBalances || issuerBalance === void 0) {
|
|
348
|
+
return {
|
|
349
|
+
tokenIdentifier: void 0,
|
|
350
|
+
balance: 0n
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
tokenIdentifier: issuerBalance[0] ?? void 0,
|
|
355
|
+
balance: issuerBalance[1].balance
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Retrieves information about the issuer's token.
|
|
360
|
+
* @returns An object containing token information including public key, name, symbol, decimals, max supply, and freeze status
|
|
361
|
+
* @throws {NetworkError} If the token metadata cannot be retrieved
|
|
362
|
+
*/
|
|
363
|
+
async getIssuerTokenMetadata() {
|
|
364
|
+
const issuerPublicKey = await super.getIdentityPublicKey();
|
|
365
|
+
const tokenMetadata = this.tokenMetadata;
|
|
366
|
+
const cachedIssuerTokenMetadata = [...tokenMetadata.entries()].find(
|
|
367
|
+
([, metadata]) => bytesToHex(metadata.issuerPublicKey) === issuerPublicKey
|
|
368
|
+
);
|
|
369
|
+
if (cachedIssuerTokenMetadata !== void 0) {
|
|
370
|
+
const metadata = cachedIssuerTokenMetadata[1];
|
|
371
|
+
return {
|
|
372
|
+
tokenPublicKey: bytesToHex(metadata.issuerPublicKey),
|
|
373
|
+
rawTokenIdentifier: metadata.tokenIdentifier,
|
|
374
|
+
tokenName: metadata.tokenName,
|
|
375
|
+
tokenTicker: metadata.tokenTicker,
|
|
376
|
+
decimals: metadata.decimals,
|
|
377
|
+
maxSupply: bytesToNumberBE(metadata.maxSupply),
|
|
378
|
+
isFreezable: metadata.isFreezable
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const sparkTokenClient = await this.connectionManager.createSparkTokenClient(
|
|
382
|
+
this.config.getCoordinatorAddress()
|
|
383
|
+
);
|
|
384
|
+
try {
|
|
385
|
+
const response = await sparkTokenClient.query_token_metadata({
|
|
386
|
+
issuerPublicKeys: Array.of(hexToBytes2(issuerPublicKey))
|
|
387
|
+
});
|
|
388
|
+
if (response.tokenMetadata.length === 0) {
|
|
389
|
+
throw new ValidationError3(
|
|
390
|
+
"Token metadata not found - If a token has not yet been announced, please announce. If a token was recently announced, it is being confirmed. Try again in a few seconds.",
|
|
391
|
+
{
|
|
392
|
+
field: "tokenMetadata",
|
|
393
|
+
value: response.tokenMetadata,
|
|
394
|
+
expected: "non-empty array",
|
|
395
|
+
actualLength: response.tokenMetadata.length,
|
|
396
|
+
expectedLength: 1
|
|
397
|
+
}
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
const metadata = response.tokenMetadata[0];
|
|
401
|
+
const tokenIdentifier = encodeBech32mTokenIdentifier({
|
|
402
|
+
tokenIdentifier: metadata.tokenIdentifier,
|
|
403
|
+
network: this.config.getNetworkType()
|
|
404
|
+
});
|
|
405
|
+
this.tokenMetadata.set(tokenIdentifier, metadata);
|
|
406
|
+
return {
|
|
407
|
+
tokenPublicKey: bytesToHex(metadata.issuerPublicKey),
|
|
408
|
+
rawTokenIdentifier: metadata.tokenIdentifier,
|
|
409
|
+
tokenName: metadata.tokenName,
|
|
410
|
+
tokenTicker: metadata.tokenTicker,
|
|
411
|
+
decimals: metadata.decimals,
|
|
412
|
+
maxSupply: bytesToNumberBE(metadata.maxSupply),
|
|
413
|
+
isFreezable: metadata.isFreezable
|
|
414
|
+
};
|
|
415
|
+
} catch (error) {
|
|
416
|
+
throw new NetworkError2("Failed to fetch token metadata", {
|
|
417
|
+
errorCount: 1,
|
|
418
|
+
errors: error instanceof Error ? error.message : String(error)
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Retrieves the bech32m encoded token identifier for the issuer's token.
|
|
424
|
+
* @returns The bech32m encoded token identifier for the issuer's token
|
|
425
|
+
* @throws {NetworkError} If the token identifier cannot be retrieved
|
|
426
|
+
*/
|
|
427
|
+
async getIssuerTokenIdentifier() {
|
|
428
|
+
const tokenMetadata = await this.getIssuerTokenMetadata();
|
|
429
|
+
return encodeBech32mTokenIdentifier({
|
|
430
|
+
tokenIdentifier: tokenMetadata.rawTokenIdentifier,
|
|
431
|
+
network: this.config.getNetworkType()
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Create a new token on Spark.
|
|
436
|
+
*
|
|
437
|
+
* @param params - Object containing token creation parameters.
|
|
438
|
+
* @param params.tokenName - The name of the token.
|
|
439
|
+
* @param params.tokenTicker - The ticker symbol for the token.
|
|
440
|
+
* @param params.decimals - The number of decimal places for the token.
|
|
441
|
+
* @param params.isFreezable - Whether the token can be frozen.
|
|
442
|
+
* @param [params.maxSupply=0n] - (Optional) The maximum supply of the token. Defaults to <code>0n</code>.
|
|
443
|
+
*
|
|
444
|
+
* @returns The transaction ID of the announcement.
|
|
445
|
+
*
|
|
446
|
+
* @throws {ValidationError} If `decimals` is not a safe integer or other validation fails.
|
|
447
|
+
* @throws {NetworkError} If the announcement transaction cannot be broadcast.
|
|
448
|
+
*/
|
|
449
|
+
async createToken({
|
|
450
|
+
tokenName,
|
|
451
|
+
tokenTicker,
|
|
452
|
+
decimals,
|
|
453
|
+
isFreezable,
|
|
454
|
+
maxSupply = 0n
|
|
455
|
+
}) {
|
|
456
|
+
validateTokenParameters(tokenName, tokenTicker, decimals, maxSupply);
|
|
457
|
+
const issuerPublicKey = await super.getIdentityPublicKey();
|
|
458
|
+
const tokenTransaction = await this.issuerTokenTransactionService.constructCreateTokenTransaction(
|
|
459
|
+
hexToBytes2(issuerPublicKey),
|
|
460
|
+
tokenName,
|
|
461
|
+
tokenTicker,
|
|
462
|
+
decimals,
|
|
463
|
+
maxSupply,
|
|
464
|
+
isFreezable
|
|
465
|
+
);
|
|
466
|
+
return await this.issuerTokenTransactionService.broadcastTokenTransaction(
|
|
467
|
+
tokenTransaction
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Mints new tokens
|
|
472
|
+
* @param tokenAmount - The amount of tokens to mint
|
|
473
|
+
* @returns The transaction ID of the mint operation
|
|
474
|
+
*/
|
|
475
|
+
async mintTokens(tokenAmount) {
|
|
476
|
+
let tokenTransaction;
|
|
477
|
+
const issuerTokenPublicKey = await super.getIdentityPublicKey();
|
|
478
|
+
const issuerTokenPublicKeyBytes = hexToBytes2(issuerTokenPublicKey);
|
|
479
|
+
const tokenMetadata = await this.getIssuerTokenMetadata();
|
|
480
|
+
const rawTokenIdentifier = tokenMetadata.rawTokenIdentifier;
|
|
481
|
+
if (this.config.getTokenTransactionVersion() === "V0") {
|
|
482
|
+
tokenTransaction = await this.issuerTokenTransactionService.constructMintTokenTransactionV0(
|
|
483
|
+
issuerTokenPublicKeyBytes,
|
|
484
|
+
tokenAmount
|
|
485
|
+
);
|
|
486
|
+
} else {
|
|
487
|
+
tokenTransaction = await this.issuerTokenTransactionService.constructMintTokenTransaction(
|
|
488
|
+
rawTokenIdentifier,
|
|
489
|
+
issuerTokenPublicKeyBytes,
|
|
490
|
+
tokenAmount
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
return await this.issuerTokenTransactionService.broadcastTokenTransaction(
|
|
494
|
+
tokenTransaction
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Burns issuer's tokens
|
|
499
|
+
* @param tokenAmount - The amount of tokens to burn
|
|
500
|
+
* @param selectedOutputs - Optional array of outputs to use for the burn operation
|
|
501
|
+
* @returns The transaction ID of the burn operation
|
|
502
|
+
*/
|
|
503
|
+
async burnTokens(tokenAmount, selectedOutputs) {
|
|
504
|
+
const burnAddress = encodeSparkAddress({
|
|
505
|
+
identityPublicKey: BURN_ADDRESS,
|
|
506
|
+
network: this.config.getNetworkType()
|
|
507
|
+
});
|
|
508
|
+
const issuerTokenIdentifier = await this.getIssuerTokenIdentifier();
|
|
509
|
+
return await this.transferTokens({
|
|
510
|
+
tokenIdentifier: issuerTokenIdentifier,
|
|
511
|
+
tokenAmount,
|
|
512
|
+
receiverSparkAddress: burnAddress,
|
|
513
|
+
selectedOutputs
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Freezes tokens associated with a specific Spark address.
|
|
518
|
+
* @param sparkAddress - The Spark address whose tokens should be frozen
|
|
519
|
+
* @returns An object containing the IDs of impacted outputs and the total amount of frozen tokens
|
|
520
|
+
*/
|
|
521
|
+
async freezeTokens(sparkAddress) {
|
|
522
|
+
await this.syncTokenOutputs();
|
|
523
|
+
const decodedOwnerPubkey = decodeSparkAddress(
|
|
524
|
+
sparkAddress,
|
|
525
|
+
this.config.getNetworkType()
|
|
526
|
+
);
|
|
527
|
+
const issuerTokenIdentifier = await this.getIssuerTokenIdentifier();
|
|
528
|
+
const rawTokenIdentifier = decodeBech32mTokenIdentifier(
|
|
529
|
+
issuerTokenIdentifier,
|
|
530
|
+
this.config.getNetworkType()
|
|
531
|
+
).tokenIdentifier;
|
|
532
|
+
const response = await this.tokenFreezeService.freezeTokens({
|
|
533
|
+
ownerPublicKey: hexToBytes2(decodedOwnerPubkey.identityPublicKey),
|
|
534
|
+
tokenIdentifier: rawTokenIdentifier
|
|
535
|
+
});
|
|
536
|
+
const tokenAmount = bytesToNumberBE(response.impactedTokenAmount);
|
|
537
|
+
return {
|
|
538
|
+
impactedOutputIds: response.impactedOutputIds,
|
|
539
|
+
impactedTokenAmount: tokenAmount
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Unfreezes previously frozen tokens associated with a specific Spark address.
|
|
544
|
+
* @param sparkAddress - The Spark address whose tokens should be unfrozen
|
|
545
|
+
* @returns An object containing the IDs of impacted outputs and the total amount of unfrozen tokens
|
|
546
|
+
*/
|
|
547
|
+
async unfreezeTokens(sparkAddress) {
|
|
548
|
+
await this.syncTokenOutputs();
|
|
549
|
+
const decodedOwnerPubkey = decodeSparkAddress(
|
|
550
|
+
sparkAddress,
|
|
551
|
+
this.config.getNetworkType()
|
|
552
|
+
);
|
|
553
|
+
const issuerTokenIdentifier = await this.getIssuerTokenIdentifier();
|
|
554
|
+
const rawTokenIdentifier = decodeBech32mTokenIdentifier(
|
|
555
|
+
issuerTokenIdentifier,
|
|
556
|
+
this.config.getNetworkType()
|
|
557
|
+
).tokenIdentifier;
|
|
558
|
+
const response = await this.tokenFreezeService.unfreezeTokens({
|
|
559
|
+
ownerPublicKey: hexToBytes2(decodedOwnerPubkey.identityPublicKey),
|
|
560
|
+
tokenIdentifier: rawTokenIdentifier
|
|
561
|
+
});
|
|
562
|
+
const tokenAmount = bytesToNumberBE(response.impactedTokenAmount);
|
|
563
|
+
return {
|
|
564
|
+
impactedOutputIds: response.impactedOutputIds,
|
|
565
|
+
impactedTokenAmount: tokenAmount
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Retrieves the distribution information for the issuer's token.
|
|
570
|
+
* @throws {NotImplementedError} This feature is not yet supported
|
|
571
|
+
*/
|
|
572
|
+
async getIssuerTokenDistribution() {
|
|
573
|
+
throw new NotImplementedError("Token distribution is not yet supported");
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Announces a new token on the L1 (Bitcoin) network.
|
|
577
|
+
* @param tokenName - The name of the token
|
|
578
|
+
* @param tokenTicker - The ticker symbol for the token
|
|
579
|
+
* @param decimals - The number of decimal places for the token
|
|
580
|
+
* @param maxSupply - The maximum supply of the token
|
|
581
|
+
* @param isFreezable - Whether the token can be frozen
|
|
582
|
+
* @param feeRateSatsPerVb - The fee rate in satoshis per virtual byte (default: 4.0)
|
|
583
|
+
* @returns The transaction ID of the announcement
|
|
584
|
+
* @throws {ValidationError} If decimals is not a safe integer
|
|
585
|
+
* @throws {NetworkError} If the announcement transaction cannot be broadcast
|
|
586
|
+
*/
|
|
587
|
+
async announceTokenL1(tokenName, tokenTicker, decimals, maxSupply, isFreezable, feeRateSatsPerVb = 4) {
|
|
588
|
+
validateTokenParameters(tokenName, tokenTicker, decimals, maxSupply);
|
|
589
|
+
if (!Number.isSafeInteger(decimals)) {
|
|
590
|
+
throw new ValidationError3("Decimals must be less than 2^53", {
|
|
591
|
+
field: "decimals",
|
|
592
|
+
value: decimals,
|
|
593
|
+
expected: "smaller or equal to " + Number.MAX_SAFE_INTEGER
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
await this.lrc20Wallet.syncWallet();
|
|
597
|
+
const tokenPublicKey = new TokenPubkey(this.lrc20Wallet.pubkey);
|
|
598
|
+
const announcement = new TokenPubkeyAnnouncement(
|
|
599
|
+
tokenPublicKey,
|
|
600
|
+
tokenName,
|
|
601
|
+
tokenTicker,
|
|
602
|
+
decimals,
|
|
603
|
+
maxSupply,
|
|
604
|
+
isFreezable
|
|
605
|
+
);
|
|
606
|
+
try {
|
|
607
|
+
const tx = await this.lrc20Wallet.prepareAnnouncement(
|
|
608
|
+
announcement,
|
|
609
|
+
feeRateSatsPerVb
|
|
610
|
+
);
|
|
611
|
+
const txId = await this.lrc20Wallet.broadcastRawBtcTransaction(
|
|
612
|
+
tx.bitcoin_tx.toHex()
|
|
613
|
+
);
|
|
614
|
+
return txId;
|
|
615
|
+
} catch (error) {
|
|
616
|
+
throw new NetworkError2(
|
|
617
|
+
"Failed to broadcast announcement transaction on L1",
|
|
618
|
+
{
|
|
619
|
+
operation: "broadcastRawBtcTransaction",
|
|
620
|
+
errorCount: 1,
|
|
621
|
+
errors: error instanceof Error ? error.message : String(error)
|
|
622
|
+
}
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
getTraceName(methodName) {
|
|
627
|
+
return `IssuerSparkWallet.${methodName}`;
|
|
628
|
+
}
|
|
629
|
+
wrapPublicIssuerSparkWalletMethodWithOtelSpan(methodName) {
|
|
630
|
+
const original = this[methodName];
|
|
631
|
+
if (typeof original !== "function") {
|
|
632
|
+
throw new Error(
|
|
633
|
+
`Method ${methodName} is not a function on IssuerSparkWallet.`
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
const wrapped = this.wrapWithOtelSpan(
|
|
637
|
+
this.getTraceName(methodName),
|
|
638
|
+
original.bind(this)
|
|
639
|
+
);
|
|
640
|
+
this[methodName] = wrapped;
|
|
641
|
+
}
|
|
642
|
+
wrapIssuerSparkWalletMethodsWithTracing() {
|
|
643
|
+
const methods = [
|
|
644
|
+
"getIssuerTokenBalance",
|
|
645
|
+
"getIssuerTokenMetadata",
|
|
646
|
+
"getIssuerTokenIdentifier",
|
|
647
|
+
"createToken",
|
|
648
|
+
"mintTokens",
|
|
649
|
+
"burnTokens",
|
|
650
|
+
"freezeTokens",
|
|
651
|
+
"unfreezeTokens",
|
|
652
|
+
"getIssuerTokenDistribution",
|
|
653
|
+
"announceTokenL1"
|
|
654
|
+
];
|
|
655
|
+
methods.forEach(
|
|
656
|
+
(m) => this.wrapPublicIssuerSparkWalletMethodWithOtelSpan(m)
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
export {
|
|
662
|
+
IssuerSparkWallet
|
|
663
|
+
};
|