@buildonspark/spark-sdk 0.1.45 → 0.1.47
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 +22 -0
- package/dist/{chunk-I54FARY2.js → chunk-EAP3U3CW.js} +14 -14
- package/dist/chunk-GWFQ7EBA.js +3773 -0
- package/dist/{chunk-J2IE4Z7Y.js → chunk-NNX4OK44.js} +3487 -934
- package/dist/{RequestLightningSendInput-Du0z7Om7.d.cts → client-CvpTRpcw.d.cts} +422 -212
- package/dist/{RequestLightningSendInput-DEPd_fPO.d.ts → client-D7KgLN44.d.ts} +422 -212
- package/dist/graphql/objects/index.d.cts +5 -9
- package/dist/graphql/objects/index.d.ts +5 -9
- package/dist/graphql/objects/index.js +1 -1
- package/dist/index.cjs +20461 -23377
- package/dist/index.d.cts +15 -769
- package/dist/index.d.ts +15 -769
- package/dist/index.js +81 -71
- package/dist/index.node.cjs +21994 -25018
- package/dist/index.node.d.cts +312 -34
- package/dist/index.node.d.ts +312 -34
- package/dist/index.node.js +82 -176
- package/dist/native/index.cjs +22847 -25841
- package/dist/native/index.d.cts +974 -1138
- package/dist/native/index.d.ts +974 -1138
- package/dist/native/index.js +10604 -13592
- package/dist/proto/lrc20.d.cts +2 -2
- package/dist/proto/lrc20.d.ts +2 -2
- package/dist/proto/lrc20.js +3098 -46
- package/dist/proto/spark.d.cts +1 -1
- package/dist/proto/spark.d.ts +1 -1
- package/dist/proto/spark_token.d.cts +1 -1
- package/dist/proto/spark_token.d.ts +1 -1
- package/dist/{sdk-types-Cc4l4kb1.d.ts → sdk-types-BGCeea0G.d.ts} +1 -1
- package/dist/{sdk-types-B0SwjolI.d.cts → sdk-types-XUeQMLFP.d.cts} +1 -1
- package/dist/{spark-dM7EYXYQ.d.cts → spark-BbUrbvZz.d.cts} +1 -1
- package/dist/{spark-dM7EYXYQ.d.ts → spark-BbUrbvZz.d.ts} +1 -1
- package/dist/spark-wallet-BAFPpPtY.d.cts +923 -0
- package/dist/spark-wallet-CJkQW8pK.d.ts +923 -0
- package/dist/spark_bindings/native/index.d.cts +1 -1
- package/dist/spark_bindings/native/index.d.ts +1 -1
- package/dist/spark_bindings/wasm/index.d.cts +1 -1
- package/dist/spark_bindings/wasm/index.d.ts +1 -1
- package/dist/{services/index.cjs → tests/test-utils.cjs} +2512 -4380
- package/dist/tests/test-utils.d.cts +79 -0
- package/dist/tests/test-utils.d.ts +79 -0
- package/dist/tests/test-utils.js +85 -0
- package/dist/types/index.d.cts +5 -9
- package/dist/types/index.d.ts +5 -9
- package/dist/types/index.js +5 -5
- package/dist/{types-C-Rp0Oo7.d.cts → types-BADxR3bm.d.cts} +1 -1
- package/dist/{types-C-Rp0Oo7.d.ts → types-BADxR3bm.d.ts} +1 -1
- package/package.json +7 -35
- package/src/graphql/client.ts +59 -20
- package/src/index.node.ts +28 -2
- package/src/index.ts +31 -1
- package/src/native/index.ts +16 -2
- package/src/services/config.ts +4 -6
- package/src/services/connection.ts +131 -64
- package/src/services/lightning.ts +1 -2
- package/src/services/token-transactions.ts +7 -7
- package/src/services/transfer.ts +1 -1
- package/src/services/tree-creation.ts +1 -1
- package/src/services/wallet-config.ts +18 -10
- package/src/signer/signer.react-native.ts +2 -5
- package/src/signer/signer.ts +138 -64
- package/src/signer/types.ts +52 -0
- package/src/spark-wallet/spark-wallet.ts +79 -36
- package/src/spark-wallet/types.ts +4 -4
- package/src/tests/integration/coop-exit.test.ts +2 -1
- package/src/tests/integration/lightning.test.ts +2 -2
- package/src/tests/integration/swap.test.ts +1 -1
- package/src/tests/integration/transfer.test.ts +5 -5
- package/src/tests/integration/tree-creation.test.ts +1 -1
- package/src/tests/integration/wallet.test.ts +1 -0
- package/src/tests/isHermeticTest.ts +3 -24
- package/src/tests/{test-util.ts → test-utils.ts} +3 -7
- package/src/tests/wrapWithOtelSpan.test.ts +1 -1
- package/src/{address → utils}/address.ts +1 -1
- package/src/utils/crypto.ts +19 -9
- package/src/utils/index.ts +2 -0
- package/src/utils/network.ts +17 -0
- package/src/utils/secret-sharing.ts +1 -2
- package/src/utils/signing.ts +1 -1
- package/src/utils/token-transactions.ts +3 -3
- package/src/utils/unilateral-exit.ts +32 -0
- package/src/utils/xchain-address.ts +1 -1
- package/dist/BitcoinNetwork-TnABML0T.d.cts +0 -18
- package/dist/BitcoinNetwork-TnABML0T.d.ts +0 -18
- package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.cts +0 -10
- package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.ts +0 -10
- package/dist/address/index.cjs +0 -458
- package/dist/address/index.d.cts +0 -32
- package/dist/address/index.d.ts +0 -32
- package/dist/address/index.js +0 -17
- package/dist/chunk-5FUB65LX.js +0 -838
- package/dist/chunk-6264CGDM.js +0 -113
- package/dist/chunk-7V6N75CC.js +0 -24
- package/dist/chunk-C2S227QR.js +0 -2336
- package/dist/chunk-GSI4OLXZ.js +0 -117
- package/dist/chunk-GZ5IPPJ2.js +0 -170
- package/dist/chunk-HWJWKEIU.js +0 -75
- package/dist/chunk-KMUMFYFX.js +0 -137
- package/dist/chunk-L3EHBOUX.js +0 -0
- package/dist/chunk-NSJF5F5O.js +0 -325
- package/dist/chunk-NTFKFRQ2.js +0 -3146
- package/dist/chunk-PQN3C2MF.js +0 -1122
- package/dist/chunk-QNNSEJ4P.js +0 -232
- package/dist/chunk-R5PXJZQS.js +0 -277
- package/dist/chunk-VTUGIIWI.js +0 -0
- package/dist/chunk-YUPMXTCJ.js +0 -622
- package/dist/chunk-Z5HIAYFT.js +0 -84
- package/dist/index-B2AwKW5J.d.cts +0 -214
- package/dist/index-CJDi1HWc.d.ts +0 -214
- package/dist/network-BTJl-Sul.d.ts +0 -46
- package/dist/network-CqgsdUF2.d.cts +0 -46
- package/dist/services/config.cjs +0 -2354
- package/dist/services/config.d.cts +0 -42
- package/dist/services/config.d.ts +0 -42
- package/dist/services/config.js +0 -17
- package/dist/services/connection.cjs +0 -17691
- package/dist/services/connection.d.cts +0 -95
- package/dist/services/connection.d.ts +0 -95
- package/dist/services/connection.js +0 -11
- package/dist/services/index.d.cts +0 -21
- package/dist/services/index.d.ts +0 -21
- package/dist/services/index.js +0 -58
- package/dist/services/lrc-connection.cjs +0 -4713
- package/dist/services/lrc-connection.d.cts +0 -34
- package/dist/services/lrc-connection.d.ts +0 -34
- package/dist/services/lrc-connection.js +0 -11
- package/dist/services/token-transactions.cjs +0 -2877
- package/dist/services/token-transactions.d.cts +0 -75
- package/dist/services/token-transactions.d.ts +0 -75
- package/dist/services/token-transactions.js +0 -15
- package/dist/services/wallet-config.cjs +0 -340
- package/dist/services/wallet-config.d.cts +0 -56
- package/dist/services/wallet-config.d.ts +0 -56
- package/dist/services/wallet-config.js +0 -33
- package/dist/signer/signer.cjs +0 -2004
- package/dist/signer/signer.d.cts +0 -10
- package/dist/signer/signer.d.ts +0 -10
- package/dist/signer/signer.js +0 -24
- package/dist/signer-BocS_J6B.d.ts +0 -187
- package/dist/signer-DKS0AJkw.d.cts +0 -187
- package/dist/utils/index.cjs +0 -2947
- package/dist/utils/index.d.cts +0 -18
- package/dist/utils/index.d.ts +0 -18
- package/dist/utils/index.js +0 -157
- package/src/address/index.ts +0 -1
- package/src/services/lrc-connection.ts +0 -215
package/dist/chunk-GSI4OLXZ.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
// src/errors/base.ts
|
|
2
|
-
import { bytesToHex } from "@noble/hashes/utils";
|
|
3
|
-
var SparkSDKError = class extends Error {
|
|
4
|
-
context;
|
|
5
|
-
originalError;
|
|
6
|
-
constructor(message, context = {}, originalError) {
|
|
7
|
-
const msg = getMessage(message, context, originalError);
|
|
8
|
-
super(msg);
|
|
9
|
-
this.name = this.constructor.name;
|
|
10
|
-
this.context = context;
|
|
11
|
-
this.originalError = originalError;
|
|
12
|
-
if (Error.captureStackTrace) {
|
|
13
|
-
Error.captureStackTrace(this, this.constructor);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
toString() {
|
|
17
|
-
return this.message;
|
|
18
|
-
}
|
|
19
|
-
toJSON() {
|
|
20
|
-
return {
|
|
21
|
-
name: this.name,
|
|
22
|
-
message: this.message,
|
|
23
|
-
context: this.context,
|
|
24
|
-
originalError: this.originalError ? {
|
|
25
|
-
name: this.originalError.name,
|
|
26
|
-
message: this.originalError.message,
|
|
27
|
-
stack: this.originalError.stack
|
|
28
|
-
} : void 0,
|
|
29
|
-
stack: this.stack
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
function getMessage(message, context = {}, originalError) {
|
|
34
|
-
const contextStr = Object.entries(context).map(([key, value]) => `${key}: ${safeStringify(value)}`).join(", ");
|
|
35
|
-
const originalErrorStr = originalError ? `
|
|
36
|
-
Original Error: ${originalError.message}` : "";
|
|
37
|
-
return `SparkSDKError: ${message}${contextStr ? `
|
|
38
|
-
Context: ${contextStr}` : ""}${originalErrorStr}`;
|
|
39
|
-
}
|
|
40
|
-
function safeStringify(value) {
|
|
41
|
-
const replacer = (_, v) => {
|
|
42
|
-
if (typeof v === "bigint") {
|
|
43
|
-
return v.toString();
|
|
44
|
-
}
|
|
45
|
-
if (v instanceof Uint8Array) {
|
|
46
|
-
return formatUint8Array(v);
|
|
47
|
-
}
|
|
48
|
-
return v;
|
|
49
|
-
};
|
|
50
|
-
if (typeof value === "bigint") {
|
|
51
|
-
return `"${value.toString()}"`;
|
|
52
|
-
}
|
|
53
|
-
if (value instanceof Uint8Array) {
|
|
54
|
-
return `"${formatUint8Array(value)}"`;
|
|
55
|
-
}
|
|
56
|
-
try {
|
|
57
|
-
const result = JSON.stringify(value, replacer);
|
|
58
|
-
return result === void 0 ? String(value) : result;
|
|
59
|
-
} catch {
|
|
60
|
-
try {
|
|
61
|
-
return String(value);
|
|
62
|
-
} catch {
|
|
63
|
-
return "[Unserializable]";
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function formatUint8Array(arr) {
|
|
68
|
-
return `Uint8Array(0x${bytesToHex(arr)})`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/errors/types.ts
|
|
72
|
-
var NetworkError = class extends SparkSDKError {
|
|
73
|
-
constructor(message, context = {}, originalError) {
|
|
74
|
-
super(message, context, originalError);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
var ValidationError = class extends SparkSDKError {
|
|
78
|
-
constructor(message, context = {}, originalError) {
|
|
79
|
-
super(message, context, originalError);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var InternalValidationError = class extends SparkSDKError {
|
|
83
|
-
constructor(message, context = {}, originalError) {
|
|
84
|
-
super(message, context, originalError);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
var AuthenticationError = class extends SparkSDKError {
|
|
88
|
-
constructor(message, context = {}, originalError) {
|
|
89
|
-
super(message, context, originalError);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
var RPCError = class extends SparkSDKError {
|
|
93
|
-
constructor(message, context = {}, originalError) {
|
|
94
|
-
super(message, context, originalError);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
var ConfigurationError = class extends SparkSDKError {
|
|
98
|
-
constructor(message, context = {}, originalError) {
|
|
99
|
-
super(message, context, originalError);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
var NotImplementedError = class extends SparkSDKError {
|
|
103
|
-
constructor(message, context, originalError) {
|
|
104
|
-
super(message, context, originalError);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export {
|
|
109
|
-
SparkSDKError,
|
|
110
|
-
NetworkError,
|
|
111
|
-
ValidationError,
|
|
112
|
-
InternalValidationError,
|
|
113
|
-
AuthenticationError,
|
|
114
|
-
RPCError,
|
|
115
|
-
ConfigurationError,
|
|
116
|
-
NotImplementedError
|
|
117
|
-
};
|
package/dist/chunk-GZ5IPPJ2.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
SparkServiceDefinition
|
|
3
|
-
} from "./chunk-NTFKFRQ2.js";
|
|
4
|
-
import {
|
|
5
|
-
clientEnv,
|
|
6
|
-
isBun,
|
|
7
|
-
isReactNative
|
|
8
|
-
} from "./chunk-7V6N75CC.js";
|
|
9
|
-
import {
|
|
10
|
-
NetworkError
|
|
11
|
-
} from "./chunk-GSI4OLXZ.js";
|
|
12
|
-
|
|
13
|
-
// src/services/lrc-connection.ts
|
|
14
|
-
import { isNode } from "@lightsparkdev/core";
|
|
15
|
-
import { retryMiddleware } from "nice-grpc-client-middleware-retry";
|
|
16
|
-
import { Metadata } from "nice-grpc-common";
|
|
17
|
-
var Lrc20ConnectionManager = class {
|
|
18
|
-
config;
|
|
19
|
-
lrc20Client;
|
|
20
|
-
constructor(config) {
|
|
21
|
-
this.config = config;
|
|
22
|
-
}
|
|
23
|
-
async closeConnection() {
|
|
24
|
-
this.lrc20Client?.close?.();
|
|
25
|
-
}
|
|
26
|
-
async createChannelWithTLS(address, certPath) {
|
|
27
|
-
try {
|
|
28
|
-
if (isNode && !isBun) {
|
|
29
|
-
const grpcModule = await import("nice-grpc");
|
|
30
|
-
const { ChannelCredentials, createChannel } = "default" in grpcModule ? grpcModule.default : grpcModule;
|
|
31
|
-
if (certPath) {
|
|
32
|
-
try {
|
|
33
|
-
const fs = await import("fs");
|
|
34
|
-
const cert = fs.readFileSync(certPath);
|
|
35
|
-
return createChannel(address, ChannelCredentials.createSsl(cert));
|
|
36
|
-
} catch (error) {
|
|
37
|
-
console.error("Error reading certificate:", error);
|
|
38
|
-
return createChannel(
|
|
39
|
-
address,
|
|
40
|
-
ChannelCredentials.createSsl(null, null, null, {
|
|
41
|
-
rejectUnauthorized: false
|
|
42
|
-
})
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
} else {
|
|
46
|
-
return createChannel(
|
|
47
|
-
address,
|
|
48
|
-
ChannelCredentials.createSsl(null, null, null, {
|
|
49
|
-
rejectUnauthorized: false
|
|
50
|
-
})
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
const grpcModule = await import("nice-grpc-web");
|
|
55
|
-
const { createChannel, FetchTransport } = "default" in grpcModule ? grpcModule.default : grpcModule;
|
|
56
|
-
const { XHRTransport } = await import("./xhr-transport-RH6LDRXS.js");
|
|
57
|
-
return createChannel(
|
|
58
|
-
address,
|
|
59
|
-
isReactNative ? XHRTransport() : FetchTransport()
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error("Channel creation error:", error);
|
|
64
|
-
throw new NetworkError(
|
|
65
|
-
"Failed to create channel",
|
|
66
|
-
{
|
|
67
|
-
url: address,
|
|
68
|
-
operation: "createChannel",
|
|
69
|
-
errorCount: 1,
|
|
70
|
-
errors: error instanceof Error ? error.message : String(error)
|
|
71
|
-
},
|
|
72
|
-
error
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async createLrc20Client(certPath) {
|
|
77
|
-
if (this.lrc20Client) {
|
|
78
|
-
return this.lrc20Client;
|
|
79
|
-
}
|
|
80
|
-
const channel = await this.createChannelWithTLS(
|
|
81
|
-
this.config.getLrc20Address(),
|
|
82
|
-
certPath
|
|
83
|
-
);
|
|
84
|
-
const middleware = this.createMiddleware();
|
|
85
|
-
const client = await this.createGrpcClient(
|
|
86
|
-
SparkServiceDefinition,
|
|
87
|
-
channel,
|
|
88
|
-
true,
|
|
89
|
-
middleware
|
|
90
|
-
);
|
|
91
|
-
this.lrc20Client = client;
|
|
92
|
-
return client;
|
|
93
|
-
}
|
|
94
|
-
createMiddleware() {
|
|
95
|
-
if (isNode) {
|
|
96
|
-
return this.createNodeMiddleware();
|
|
97
|
-
} else {
|
|
98
|
-
return this.createBrowserMiddleware();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
createNodeMiddleware() {
|
|
102
|
-
return async function* (call, options) {
|
|
103
|
-
return yield* call.next(call.request, {
|
|
104
|
-
...options,
|
|
105
|
-
metadata: Metadata(options.metadata).set("X-Client-Env", clientEnv)
|
|
106
|
-
});
|
|
107
|
-
}.bind(this);
|
|
108
|
-
}
|
|
109
|
-
createBrowserMiddleware() {
|
|
110
|
-
return async function* (call, options) {
|
|
111
|
-
return yield* call.next(call.request, {
|
|
112
|
-
...options,
|
|
113
|
-
metadata: Metadata(options.metadata).set("X-Requested-With", "XMLHttpRequest").set("X-Grpc-Web", "1").set("Content-Type", "application/grpc-web+proto").set("X-Client-Env", clientEnv)
|
|
114
|
-
});
|
|
115
|
-
}.bind(this);
|
|
116
|
-
}
|
|
117
|
-
async createGrpcClient(defintion, channel, withRetries, middleware) {
|
|
118
|
-
let clientFactory;
|
|
119
|
-
const retryOptions = {
|
|
120
|
-
retry: true,
|
|
121
|
-
retryMaxAttempts: 3
|
|
122
|
-
};
|
|
123
|
-
let options = {};
|
|
124
|
-
const isNodeChannel = "close" in channel;
|
|
125
|
-
if (isNode && isNodeChannel && !isBun) {
|
|
126
|
-
const grpcModule = await import("nice-grpc");
|
|
127
|
-
const { openTelemetryClientMiddleware } = await import("nice-grpc-opentelemetry");
|
|
128
|
-
const { createClientFactory } = "default" in grpcModule ? grpcModule.default : grpcModule;
|
|
129
|
-
clientFactory = createClientFactory();
|
|
130
|
-
if (withRetries) {
|
|
131
|
-
options = retryOptions;
|
|
132
|
-
clientFactory = clientFactory.use(openTelemetryClientMiddleware()).use(retryMiddleware);
|
|
133
|
-
}
|
|
134
|
-
if (middleware) {
|
|
135
|
-
clientFactory = clientFactory.use(middleware);
|
|
136
|
-
}
|
|
137
|
-
const client = clientFactory.create(defintion, channel, {
|
|
138
|
-
"*": options
|
|
139
|
-
});
|
|
140
|
-
return {
|
|
141
|
-
...client,
|
|
142
|
-
close: channel.close.bind(channel)
|
|
143
|
-
};
|
|
144
|
-
} else if (!isNodeChannel) {
|
|
145
|
-
const grpcModule = await import("nice-grpc-web");
|
|
146
|
-
const { createClientFactory } = "default" in grpcModule ? grpcModule.default : grpcModule;
|
|
147
|
-
clientFactory = createClientFactory();
|
|
148
|
-
if (withRetries) {
|
|
149
|
-
options = retryOptions;
|
|
150
|
-
clientFactory = clientFactory.use(retryMiddleware);
|
|
151
|
-
}
|
|
152
|
-
if (middleware) {
|
|
153
|
-
clientFactory = clientFactory.use(middleware);
|
|
154
|
-
}
|
|
155
|
-
const client = clientFactory.create(defintion, channel, {
|
|
156
|
-
"*": options
|
|
157
|
-
});
|
|
158
|
-
return {
|
|
159
|
-
...client,
|
|
160
|
-
close: void 0
|
|
161
|
-
};
|
|
162
|
-
} else {
|
|
163
|
-
throw new Error("Channel does not have close in NodeJS environment");
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
export {
|
|
169
|
-
Lrc20ConnectionManager
|
|
170
|
-
};
|
package/dist/chunk-HWJWKEIU.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
NetworkError
|
|
3
|
-
} from "./chunk-GSI4OLXZ.js";
|
|
4
|
-
|
|
5
|
-
// src/utils/token-transactions.ts
|
|
6
|
-
import { bytesToHex, bytesToNumberBE } from "@noble/curves/abstract/utils";
|
|
7
|
-
function calculateAvailableTokenAmount(outputLeaves) {
|
|
8
|
-
return outputLeaves.reduce(
|
|
9
|
-
(sum, output) => sum + BigInt(bytesToNumberBE(output.output.tokenAmount)),
|
|
10
|
-
BigInt(0)
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
function checkIfSelectedOutputsAreAvailable(selectedOutputs, tokenOutputs, tokenPublicKey) {
|
|
14
|
-
const tokenPubKeyHex = bytesToHex(tokenPublicKey);
|
|
15
|
-
const tokenOutputsAvailable = tokenOutputs.get(tokenPubKeyHex);
|
|
16
|
-
if (!tokenOutputsAvailable) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
if (selectedOutputs.length === 0 || tokenOutputsAvailable.length < selectedOutputs.length) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
const availableOutputIds = new Set(
|
|
23
|
-
tokenOutputsAvailable.map((output) => output.output.id)
|
|
24
|
-
);
|
|
25
|
-
for (const selectedOutput of selectedOutputs) {
|
|
26
|
-
if (!selectedOutput.output?.id || !availableOutputIds.has(selectedOutput.output.id)) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
function filterTokenBalanceForTokenPublicKey(tokenBalances, publicKey) {
|
|
33
|
-
if (!tokenBalances) {
|
|
34
|
-
return { balance: 0n };
|
|
35
|
-
}
|
|
36
|
-
const tokenBalance = [...tokenBalances.entries()].find(
|
|
37
|
-
([, info]) => info.tokenMetadata.tokenPublicKey === publicKey
|
|
38
|
-
);
|
|
39
|
-
if (!tokenBalance) {
|
|
40
|
-
return {
|
|
41
|
-
balance: 0n
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
balance: tokenBalance[1].balance
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/utils/response-validation.ts
|
|
50
|
-
function collectResponses(responses) {
|
|
51
|
-
const successfulResponses = responses.filter(
|
|
52
|
-
(result) => result.status === "fulfilled"
|
|
53
|
-
).map((result) => result.value);
|
|
54
|
-
const failedResponses = responses.filter(
|
|
55
|
-
(result) => result.status === "rejected"
|
|
56
|
-
);
|
|
57
|
-
if (failedResponses.length > 0) {
|
|
58
|
-
const errors = failedResponses.map((result) => result.reason).join("\n");
|
|
59
|
-
throw new NetworkError(
|
|
60
|
-
`${failedResponses.length} out of ${responses.length} requests failed, please try again`,
|
|
61
|
-
{
|
|
62
|
-
errorCount: failedResponses.length,
|
|
63
|
-
errors
|
|
64
|
-
}
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
return successfulResponses;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export {
|
|
71
|
-
calculateAvailableTokenAmount,
|
|
72
|
-
checkIfSelectedOutputsAreAvailable,
|
|
73
|
-
filterTokenBalanceForTokenPublicKey,
|
|
74
|
-
collectResponses
|
|
75
|
-
};
|
package/dist/chunk-KMUMFYFX.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ValidationError
|
|
3
|
-
} from "./chunk-GSI4OLXZ.js";
|
|
4
|
-
import {
|
|
5
|
-
SparkAddress
|
|
6
|
-
} from "./chunk-BGGEVUJK.js";
|
|
7
|
-
|
|
8
|
-
// src/address/address.ts
|
|
9
|
-
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
10
|
-
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
11
|
-
import { bech32m } from "@scure/base";
|
|
12
|
-
import { UUID } from "uuidv7";
|
|
13
|
-
import { bytesToNumberBE } from "@noble/curves/abstract/utils";
|
|
14
|
-
var AddressNetwork = {
|
|
15
|
-
MAINNET: "sp",
|
|
16
|
-
TESTNET: "spt",
|
|
17
|
-
REGTEST: "sprt",
|
|
18
|
-
SIGNET: "sps",
|
|
19
|
-
LOCAL: "spl"
|
|
20
|
-
};
|
|
21
|
-
function encodeSparkAddress(payload) {
|
|
22
|
-
try {
|
|
23
|
-
isValidPublicKey(payload.identityPublicKey);
|
|
24
|
-
let paymentIntentFields;
|
|
25
|
-
if (payload.paymentIntentFields) {
|
|
26
|
-
paymentIntentFields = payload.paymentIntentFields;
|
|
27
|
-
}
|
|
28
|
-
const sparkAddressProto = SparkAddress.create({
|
|
29
|
-
identityPublicKey: hexToBytes(payload.identityPublicKey),
|
|
30
|
-
paymentIntentFields
|
|
31
|
-
});
|
|
32
|
-
const serializedPayload = SparkAddress.encode(sparkAddressProto).finish();
|
|
33
|
-
const words = bech32m.toWords(serializedPayload);
|
|
34
|
-
return bech32m.encode(
|
|
35
|
-
AddressNetwork[payload.network],
|
|
36
|
-
words,
|
|
37
|
-
500
|
|
38
|
-
);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
throw new ValidationError(
|
|
41
|
-
"Failed to encode Spark address",
|
|
42
|
-
{
|
|
43
|
-
field: "publicKey",
|
|
44
|
-
value: payload.identityPublicKey
|
|
45
|
-
},
|
|
46
|
-
error
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
function decodeSparkAddress(address, network) {
|
|
51
|
-
try {
|
|
52
|
-
const decoded = bech32m.decode(address, 500);
|
|
53
|
-
if (decoded.prefix !== AddressNetwork[network]) {
|
|
54
|
-
throw new ValidationError("Invalid Spark address prefix", {
|
|
55
|
-
field: "address",
|
|
56
|
-
value: address,
|
|
57
|
-
expected: `prefix='${AddressNetwork[network]}'`
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
const payload = SparkAddress.decode(bech32m.fromWords(decoded.words));
|
|
61
|
-
const publicKey = bytesToHex(payload.identityPublicKey);
|
|
62
|
-
isValidPublicKey(publicKey);
|
|
63
|
-
const paymentIntentFields = payload.paymentIntentFields;
|
|
64
|
-
return {
|
|
65
|
-
identityPublicKey: publicKey,
|
|
66
|
-
network,
|
|
67
|
-
paymentIntentFields: paymentIntentFields && {
|
|
68
|
-
id: UUID.ofInner(paymentIntentFields.id).toString(),
|
|
69
|
-
assetIdentifier: paymentIntentFields.assetIdentifier ? bytesToHex(paymentIntentFields.assetIdentifier) : void 0,
|
|
70
|
-
assetAmount: bytesToNumberBE(paymentIntentFields.assetAmount),
|
|
71
|
-
memo: paymentIntentFields.memo
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
} catch (error) {
|
|
75
|
-
if (error instanceof ValidationError) {
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
throw new ValidationError(
|
|
79
|
-
"Failed to decode Spark address",
|
|
80
|
-
{
|
|
81
|
-
field: "address",
|
|
82
|
-
value: address
|
|
83
|
-
},
|
|
84
|
-
error
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
function isValidSparkAddress(address) {
|
|
89
|
-
try {
|
|
90
|
-
const network = Object.entries(AddressNetwork).find(
|
|
91
|
-
([_, prefix]) => address.startsWith(prefix)
|
|
92
|
-
)?.[0];
|
|
93
|
-
if (!network) {
|
|
94
|
-
throw new ValidationError("Invalid Spark address network", {
|
|
95
|
-
field: "network",
|
|
96
|
-
value: address,
|
|
97
|
-
expected: Object.values(AddressNetwork)
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
decodeSparkAddress(address, network);
|
|
101
|
-
return true;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
if (error instanceof ValidationError) {
|
|
104
|
-
throw error;
|
|
105
|
-
}
|
|
106
|
-
throw new ValidationError(
|
|
107
|
-
"Invalid Spark address",
|
|
108
|
-
{
|
|
109
|
-
field: "address",
|
|
110
|
-
value: address
|
|
111
|
-
},
|
|
112
|
-
error
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function isValidPublicKey(publicKey) {
|
|
117
|
-
try {
|
|
118
|
-
const point = secp256k1.ProjectivePoint.fromHex(publicKey);
|
|
119
|
-
point.assertValidity();
|
|
120
|
-
} catch (error) {
|
|
121
|
-
throw new ValidationError(
|
|
122
|
-
"Invalid public key",
|
|
123
|
-
{
|
|
124
|
-
field: "publicKey",
|
|
125
|
-
value: publicKey
|
|
126
|
-
},
|
|
127
|
-
error
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export {
|
|
133
|
-
encodeSparkAddress,
|
|
134
|
-
decodeSparkAddress,
|
|
135
|
-
isValidSparkAddress,
|
|
136
|
-
isValidPublicKey
|
|
137
|
-
};
|
package/dist/chunk-L3EHBOUX.js
DELETED
|
File without changes
|