@buildonspark/spark-sdk 0.0.15 → 0.0.16

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.
Files changed (131) hide show
  1. package/dist/services/wallet-config.d.ts +1 -0
  2. package/dist/services/wallet-config.js +1 -0
  3. package/dist/services/wallet-config.js.map +1 -1
  4. package/dist/spark-sdk.d.ts +1 -1
  5. package/dist/spark-sdk.js +3 -3
  6. package/dist/spark-sdk.js.map +1 -1
  7. package/package.json +4 -3
  8. package/src/examples/example.js +247 -0
  9. package/src/examples/example.ts +207 -0
  10. package/src/graphql/client.ts +282 -0
  11. package/src/graphql/mutations/CompleteCoopExit.ts +19 -0
  12. package/src/graphql/mutations/CompleteLeavesSwap.ts +17 -0
  13. package/src/graphql/mutations/RequestCoopExit.ts +20 -0
  14. package/src/graphql/mutations/RequestLightningReceive.ts +26 -0
  15. package/src/graphql/mutations/RequestLightningSend.ts +17 -0
  16. package/src/graphql/mutations/RequestSwapLeaves.ts +24 -0
  17. package/src/graphql/objects/BitcoinNetwork.ts +22 -0
  18. package/src/graphql/objects/CompleteCoopExitInput.ts +41 -0
  19. package/src/graphql/objects/CompleteCoopExitOutput.ts +45 -0
  20. package/src/graphql/objects/CompleteLeavesSwapInput.ts +45 -0
  21. package/src/graphql/objects/CompleteLeavesSwapOutput.ts +45 -0
  22. package/src/graphql/objects/CompleteSeedReleaseInput.ts +41 -0
  23. package/src/graphql/objects/CompleteSeedReleaseOutput.ts +43 -0
  24. package/src/graphql/objects/Connection.ts +90 -0
  25. package/src/graphql/objects/CoopExitFeeEstimateInput.ts +41 -0
  26. package/src/graphql/objects/CoopExitFeeEstimateOutput.ts +52 -0
  27. package/src/graphql/objects/CoopExitRequest.ts +118 -0
  28. package/src/graphql/objects/CurrencyAmount.ts +74 -0
  29. package/src/graphql/objects/CurrencyUnit.ts +32 -0
  30. package/src/graphql/objects/Entity.ts +202 -0
  31. package/src/graphql/objects/GetChallengeInput.ts +37 -0
  32. package/src/graphql/objects/GetChallengeOutput.ts +43 -0
  33. package/src/graphql/objects/Invoice.ts +83 -0
  34. package/src/graphql/objects/Leaf.ts +59 -0
  35. package/src/graphql/objects/LeavesSwapFeeEstimateInput.ts +37 -0
  36. package/src/graphql/objects/LeavesSwapFeeEstimateOutput.ts +52 -0
  37. package/src/graphql/objects/LeavesSwapRequest.ts +192 -0
  38. package/src/graphql/objects/LightningReceiveFeeEstimateInput.ts +41 -0
  39. package/src/graphql/objects/LightningReceiveFeeEstimateOutput.ts +52 -0
  40. package/src/graphql/objects/LightningReceiveRequest.ts +147 -0
  41. package/src/graphql/objects/LightningReceiveRequestStatus.ts +34 -0
  42. package/src/graphql/objects/LightningSendFeeEstimateInput.ts +37 -0
  43. package/src/graphql/objects/LightningSendFeeEstimateOutput.ts +52 -0
  44. package/src/graphql/objects/LightningSendRequest.ts +134 -0
  45. package/src/graphql/objects/LightningSendRequestStatus.ts +28 -0
  46. package/src/graphql/objects/NotifyReceiverTransferInput.ts +41 -0
  47. package/src/graphql/objects/PageInfo.ts +58 -0
  48. package/src/graphql/objects/Provider.ts +41 -0
  49. package/src/graphql/objects/RequestCoopExitInput.ts +41 -0
  50. package/src/graphql/objects/RequestCoopExitOutput.ts +45 -0
  51. package/src/graphql/objects/RequestLeavesSwapInput.ts +55 -0
  52. package/src/graphql/objects/RequestLeavesSwapOutput.ts +45 -0
  53. package/src/graphql/objects/RequestLightningReceiveInput.ts +58 -0
  54. package/src/graphql/objects/RequestLightningReceiveOutput.ts +45 -0
  55. package/src/graphql/objects/RequestLightningSendInput.ts +41 -0
  56. package/src/graphql/objects/RequestLightningSendOutput.ts +45 -0
  57. package/src/graphql/objects/SparkCoopExitRequestStatus.ts +20 -0
  58. package/src/graphql/objects/SparkLeavesSwapRequestStatus.ts +20 -0
  59. package/src/graphql/objects/SparkTransferToLeavesConnection.ts +79 -0
  60. package/src/graphql/objects/SparkWalletUser.ts +86 -0
  61. package/src/graphql/objects/StartSeedReleaseInput.ts +37 -0
  62. package/src/graphql/objects/SwapLeaf.ts +53 -0
  63. package/src/graphql/objects/Transfer.ts +98 -0
  64. package/src/graphql/objects/UserLeafInput.ts +28 -0
  65. package/src/graphql/objects/VerifyChallengeInput.ts +51 -0
  66. package/src/graphql/objects/VerifyChallengeOutput.ts +43 -0
  67. package/src/graphql/objects/WalletUserIdentityPublicKeyInput.ts +37 -0
  68. package/src/graphql/objects/WalletUserIdentityPublicKeyOutput.ts +43 -0
  69. package/src/graphql/objects/index.ts +67 -0
  70. package/src/graphql/queries/CoopExitFeeEstimate.ts +18 -0
  71. package/src/graphql/queries/CurrentUser.ts +10 -0
  72. package/src/graphql/queries/LightningReceiveFeeEstimate.ts +18 -0
  73. package/src/graphql/queries/LightningSendFeeEstimate.ts +16 -0
  74. package/src/proto/common.ts +431 -0
  75. package/src/proto/google/protobuf/descriptor.ts +6625 -0
  76. package/src/proto/google/protobuf/duration.ts +197 -0
  77. package/src/proto/google/protobuf/empty.ts +83 -0
  78. package/src/proto/google/protobuf/timestamp.ts +226 -0
  79. package/src/proto/mock.ts +151 -0
  80. package/src/proto/spark.ts +12727 -0
  81. package/src/proto/spark_authn.ts +673 -0
  82. package/src/proto/validate/validate.ts +6047 -0
  83. package/src/services/config.ts +71 -0
  84. package/src/services/connection.ts +264 -0
  85. package/src/services/coop-exit.ts +190 -0
  86. package/src/services/deposit.ts +327 -0
  87. package/src/services/lightning.ts +341 -0
  88. package/src/services/lrc20.ts +42 -0
  89. package/src/services/token-transactions.ts +499 -0
  90. package/src/services/transfer.ts +1188 -0
  91. package/src/services/tree-creation.ts +618 -0
  92. package/src/services/wallet-config.ts +141 -0
  93. package/src/signer/signer.ts +531 -0
  94. package/src/spark-sdk.ts +1644 -0
  95. package/src/tests/adaptor-signature.test.ts +64 -0
  96. package/src/tests/bitcoin.test.ts +122 -0
  97. package/src/tests/coop-exit.test.ts +233 -0
  98. package/src/tests/deposit.test.ts +98 -0
  99. package/src/tests/keys.test.ts +82 -0
  100. package/src/tests/lightning.test.ts +307 -0
  101. package/src/tests/secret-sharing.test.ts +63 -0
  102. package/src/tests/swap.test.ts +252 -0
  103. package/src/tests/test-util.ts +92 -0
  104. package/src/tests/tokens.test.ts +47 -0
  105. package/src/tests/transfer.test.ts +371 -0
  106. package/src/tests/tree-creation.test.ts +56 -0
  107. package/src/tests/utils/spark-testing-wallet.ts +37 -0
  108. package/src/tests/utils/test-faucet.ts +257 -0
  109. package/src/types/grpc.ts +8 -0
  110. package/src/types/index.ts +3 -0
  111. package/src/utils/adaptor-signature.ts +189 -0
  112. package/src/utils/bitcoin.ts +138 -0
  113. package/src/utils/crypto.ts +14 -0
  114. package/src/utils/index.ts +12 -0
  115. package/src/utils/keys.ts +92 -0
  116. package/src/utils/mempool.ts +42 -0
  117. package/src/utils/network.ts +70 -0
  118. package/src/utils/proof.ts +17 -0
  119. package/src/utils/response-validation.ts +26 -0
  120. package/src/utils/secret-sharing.ts +263 -0
  121. package/src/utils/signing.ts +96 -0
  122. package/src/utils/token-hashing.ts +163 -0
  123. package/src/utils/token-keyshares.ts +31 -0
  124. package/src/utils/token-transactions.ts +71 -0
  125. package/src/utils/transaction.ts +45 -0
  126. package/src/utils/wasm-wrapper.ts +57 -0
  127. package/src/utils/wasm.ts +154 -0
  128. package/src/wasm/spark_bindings.d.ts +208 -0
  129. package/src/wasm/spark_bindings.js +1161 -0
  130. package/src/wasm/spark_bindings_bg.wasm +0 -0
  131. package/src/wasm/spark_bindings_bg.wasm.d.ts +136 -0
@@ -0,0 +1,207 @@
1
+ import readline from "readline";
2
+ import { ConfigOptions } from "../../dist/services/wallet-config.js";
3
+ import { SparkWallet } from "../../dist/spark-sdk";
4
+ import { getLatestDepositTxId } from "../../dist/utils/mempool.js";
5
+
6
+ // Initialize Spark Wallet
7
+ const walletMnemonic =
8
+ "cctypical stereo dose party penalty decline neglect feel harvest abstract stage winter";
9
+
10
+ async function runCLI() {
11
+ // Get network from command line args
12
+ const network = process.argv.includes("mainnet") ? "MAINNET" : "REGTEST";
13
+ let wallet: SparkWallet | undefined;
14
+
15
+ const rl = readline.createInterface({
16
+ input: process.stdin,
17
+ output: process.stdout,
18
+ });
19
+ const helpMessage = `
20
+ Available commands:
21
+ initwallet [mnemonic | seed] - Create a new wallet from a mnemonic or seed. If no mnemonic or seed is provided, a new mnemonic will be generated.
22
+ getbalance - Get the wallet's balance
23
+ getdepositaddress - Get an address to deposit funds from L1 to Spark
24
+ getsparkaddress - Get the wallet's spark address
25
+ getlatesttx <address> - Get the latest deposit transaction id for an address
26
+ claimdeposit <txid> - Claim any pending deposits to the wallet
27
+ createinvoice <amount> <memo> - Create a new lightning invoice
28
+ payinvoice <invoice> - Pay a lightning invoice
29
+ sendtransfer <amount> <receiverSparkAddress> - Send a spark transfer
30
+ withdraw <onchainAddress> <amount> - Withdraw funds to an L1 address
31
+ sendtokentransfer <tokenPubKey> <amount> <receiverSparkAddress> - Transfer tokens
32
+ help - Show this help message
33
+ exit/quit
34
+
35
+ L1 commands:
36
+ tokenwithdraw <tokenPublicKey> [receiverPublicKey] - Unilaterally withdraw tokens to L1- Exit the program
37
+ `;
38
+ console.log(helpMessage);
39
+
40
+ while (true) {
41
+ const command = await new Promise<string>((resolve) => {
42
+ rl.question("> ", resolve);
43
+ });
44
+
45
+ const [firstWord, ...args] = command.split(" ");
46
+ const lowerCommand = firstWord.toLowerCase();
47
+
48
+ if (lowerCommand === "exit" || lowerCommand === "quit") {
49
+ rl.close();
50
+ break;
51
+ }
52
+
53
+ switch (lowerCommand) {
54
+ case "help":
55
+ console.log(helpMessage);
56
+ break;
57
+ case "getlatesttx":
58
+ const latestTx = await getLatestDepositTxId(args[0]);
59
+ console.log(latestTx);
60
+ break;
61
+ case "claimdeposit":
62
+ if (!wallet) {
63
+ console.log("Please initialize a wallet first");
64
+ break;
65
+ }
66
+ const depositResult = await wallet.claimDeposit(args[0]);
67
+ console.log(depositResult);
68
+ break;
69
+ case "initwallet":
70
+ const mnemonicOrSeed = args.join(" ");
71
+ const options: ConfigOptions = {
72
+ network: "REGTEST",
73
+ };
74
+ const { wallet: newWallet, mnemonic: newMnemonic } =
75
+ await SparkWallet.create({
76
+ mnemonicOrSeed,
77
+ options,
78
+ });
79
+ wallet = newWallet;
80
+ console.log("Mnemonic:", newMnemonic);
81
+ break;
82
+ case "getbalance":
83
+ if (!wallet) {
84
+ console.log("Please initialize a wallet first");
85
+ break;
86
+ }
87
+ const balanceInfo = await wallet.getBalance(true);
88
+ console.log("Sats Balance: " + balanceInfo.balance);
89
+ if (balanceInfo.tokenBalances && balanceInfo.tokenBalances.size > 0) {
90
+ console.log("\nToken Balances:");
91
+ for (const [
92
+ tokenPublicKey,
93
+ tokenInfo,
94
+ ] of balanceInfo.tokenBalances.entries()) {
95
+ console.log(` Token (${tokenPublicKey}):`);
96
+ console.log(` Balance: ${tokenInfo.balance}`);
97
+ }
98
+ }
99
+ break;
100
+ case "getdepositaddress":
101
+ if (!wallet) {
102
+ console.log("Please initialize a wallet first");
103
+ break;
104
+ }
105
+ const depositAddress = await wallet.getDepositAddress();
106
+ console.log(depositAddress);
107
+ break;
108
+ case "getsparkaddress":
109
+ if (!wallet) {
110
+ console.log("Please initialize a wallet first");
111
+ break;
112
+ }
113
+ const sparkAddress = await wallet.getSparkAddress();
114
+ console.log(sparkAddress);
115
+ break;
116
+ case "createinvoice":
117
+ if (!wallet) {
118
+ console.log("Please initialize a wallet first");
119
+ break;
120
+ }
121
+ const invoice = await wallet.createLightningInvoice({
122
+ amountSats: parseInt(args[0]),
123
+ memo: args[1],
124
+ });
125
+ console.log(invoice);
126
+ break;
127
+ case "payinvoice":
128
+ if (!wallet) {
129
+ console.log("Please initialize a wallet first");
130
+ break;
131
+ }
132
+ const payment = await wallet.payLightningInvoice({
133
+ invoice: args[0],
134
+ });
135
+ console.log(payment);
136
+ break;
137
+ case "sendtransfer":
138
+ if (!wallet) {
139
+ console.log("Please initialize a wallet first");
140
+ break;
141
+ }
142
+ const transfer = await wallet.sendSparkTransfer({
143
+ amountSats: parseInt(args[0]),
144
+ receiverSparkAddress: args[1],
145
+ });
146
+ console.log(transfer);
147
+ break;
148
+ case "sendtokentransfer":
149
+ if (!wallet) {
150
+ console.log("Please initialize a wallet first");
151
+ break;
152
+ }
153
+ if (args.length < 3) {
154
+ console.log(
155
+ "Usage: sendtokentransfer <tokenPubKey> <amount> <receiverPubKey>",
156
+ );
157
+ break;
158
+ }
159
+
160
+ const tokenPubKey = args[0];
161
+ const tokenAmount = BigInt(parseInt(args[1]));
162
+ const tokenReceiverPubKey = args[2];
163
+
164
+ try {
165
+ const result = await wallet.transferTokens({
166
+ tokenPublicKey: tokenPubKey,
167
+ tokenAmount: tokenAmount,
168
+ receiverSparkAddress: tokenReceiverPubKey,
169
+ });
170
+ console.log(result);
171
+ } catch (error) {
172
+ console.error("Failed to transfer tokens:", error.message);
173
+ }
174
+ break;
175
+ case "withdraw":
176
+ if (!wallet) {
177
+ console.log("Please initialize a wallet first");
178
+ break;
179
+ }
180
+ const withdrawal = await wallet.withdraw({
181
+ onchainAddress: args[0],
182
+ targetAmountSats: parseInt(args[1]),
183
+ });
184
+ console.log(withdrawal);
185
+ break;
186
+ case "tokenwithdraw": {
187
+ if (!wallet) {
188
+ console.log("Please initialize a wallet first");
189
+ break;
190
+ }
191
+ const tokenPublicKey = args[0];
192
+ const receiverPublicKey = args[1];
193
+
194
+ let withdrawResult = await wallet.withdrawTokens(
195
+ tokenPublicKey,
196
+ receiverPublicKey,
197
+ );
198
+ if (withdrawResult) {
199
+ console.log("Withdrawal L1 Transaction ID:", withdrawResult.txid);
200
+ }
201
+ break;
202
+ }
203
+ }
204
+ }
205
+ }
206
+
207
+ runCLI();
@@ -0,0 +1,282 @@
1
+ import {
2
+ AuthProvider,
3
+ DefaultCrypto,
4
+ NodeKeyCache,
5
+ Query,
6
+ Requester,
7
+ SigningKey,
8
+ } from "@lightsparkdev/core";
9
+ import { CompleteCoopExit } from "./mutations/CompleteCoopExit.js";
10
+ import { CompleteLeavesSwap } from "./mutations/CompleteLeavesSwap.js";
11
+ import { RequestCoopExit } from "./mutations/RequestCoopExit.js";
12
+ import { RequestLightningReceive } from "./mutations/RequestLightningReceive.js";
13
+ import { RequestLightningSend } from "./mutations/RequestLightningSend.js";
14
+ import { RequestSwapLeaves } from "./mutations/RequestSwapLeaves.js";
15
+ import { CoopExitFeeEstimateOutputFromJson } from "./objects/CoopExitFeeEstimateOutput.js";
16
+ import CoopExitRequest, {
17
+ CoopExitRequestFromJson,
18
+ } from "./objects/CoopExitRequest.js";
19
+ import {
20
+ BitcoinNetwork,
21
+ CompleteCoopExitInput,
22
+ CompleteLeavesSwapInput,
23
+ CoopExitFeeEstimateInput,
24
+ CoopExitFeeEstimateOutput,
25
+ LightningSendRequest,
26
+ RequestCoopExitInput,
27
+ RequestLeavesSwapInput,
28
+ RequestLightningReceiveInput,
29
+ RequestLightningSendInput,
30
+ } from "./objects/index.js";
31
+ import LeavesSwapRequest, {
32
+ LeavesSwapRequestFromJson,
33
+ } from "./objects/LeavesSwapRequest.js";
34
+ import LightningReceiveFeeEstimateOutput, {
35
+ LightningReceiveFeeEstimateOutputFromJson,
36
+ } from "./objects/LightningReceiveFeeEstimateOutput.js";
37
+ import LightningReceiveRequest, {
38
+ LightningReceiveRequestFromJson,
39
+ } from "./objects/LightningReceiveRequest.js";
40
+ import LightningSendFeeEstimateOutput, {
41
+ LightningSendFeeEstimateOutputFromJson,
42
+ } from "./objects/LightningSendFeeEstimateOutput.js";
43
+ import { LightningSendRequestFromJson } from "./objects/LightningSendRequest.js";
44
+ import { CoopExitFeeEstimate } from "./queries/CoopExitFeeEstimate.js";
45
+ import { LightningReceiveFeeEstimate } from "./queries/LightningReceiveFeeEstimate.js";
46
+ import { LightningSendFeeEstimate } from "./queries/LightningSendFeeEstimate.js";
47
+
48
+ export default class SspClient {
49
+ private readonly requester: Requester;
50
+ private identityPublicKey: string;
51
+ private readonly signingKey?: SigningKey;
52
+
53
+ constructor(identityPublicKey: string) {
54
+ this.identityPublicKey = identityPublicKey;
55
+
56
+ const fetchFunction =
57
+ typeof window !== "undefined" ? window.fetch.bind(window) : fetch;
58
+
59
+ this.requester = new Requester(
60
+ new NodeKeyCache(DefaultCrypto),
61
+ "graphql/spark/rc",
62
+ `spark-sdk/0.0.0`,
63
+ new SparkAuthProvider(identityPublicKey),
64
+ "https://api.dev.dev.sparkinfra.net",
65
+ DefaultCrypto,
66
+ this.signingKey,
67
+ fetchFunction,
68
+ );
69
+ }
70
+
71
+ async executeRawQuery<T>(query: Query<T>): Promise<T | null> {
72
+ return await this.requester.executeQuery(query);
73
+ }
74
+
75
+ async getLightningReceiveFeeEstimate(
76
+ amountSats: number,
77
+ network: BitcoinNetwork,
78
+ ): Promise<LightningReceiveFeeEstimateOutput | null> {
79
+ return await this.executeRawQuery({
80
+ queryPayload: LightningReceiveFeeEstimate,
81
+ variables: {
82
+ amount_sats: amountSats,
83
+ network: network,
84
+ },
85
+ constructObject: (response: { lightning_receive_fee_estimate: any }) => {
86
+ return LightningReceiveFeeEstimateOutputFromJson(
87
+ response.lightning_receive_fee_estimate,
88
+ );
89
+ },
90
+ });
91
+ }
92
+
93
+ async getLightningSendFeeEstimate(
94
+ encodedInvoice: string,
95
+ ): Promise<LightningSendFeeEstimateOutput | null> {
96
+ return await this.executeRawQuery({
97
+ queryPayload: LightningSendFeeEstimate,
98
+ variables: {
99
+ encoded_invoice: encodedInvoice,
100
+ },
101
+ constructObject: (response: { lightning_send_fee_estimate: any }) => {
102
+ return LightningSendFeeEstimateOutputFromJson(
103
+ response.lightning_send_fee_estimate,
104
+ );
105
+ },
106
+ });
107
+ }
108
+
109
+ async getCoopExitFeeEstimate({
110
+ leafExternalIds,
111
+ withdrawalAddress,
112
+ }: CoopExitFeeEstimateInput): Promise<CoopExitFeeEstimateOutput | null> {
113
+ return await this.executeRawQuery({
114
+ queryPayload: CoopExitFeeEstimate,
115
+ variables: {
116
+ leaf_external_ids: leafExternalIds,
117
+ withdrawal_address: withdrawalAddress,
118
+ },
119
+ constructObject: (response: { coop_exit_fee_estimate: any }) => {
120
+ return CoopExitFeeEstimateOutputFromJson(
121
+ response.coop_exit_fee_estimate,
122
+ );
123
+ },
124
+ });
125
+ }
126
+
127
+ // TODO: Might not need
128
+ async getCurrentUser() {
129
+ throw new Error("Not implemented");
130
+ }
131
+
132
+ async completeCoopExit({
133
+ userOutboundTransferExternalId,
134
+ coopExitRequestId,
135
+ }: CompleteCoopExitInput): Promise<CoopExitRequest | null> {
136
+ return await this.executeRawQuery({
137
+ queryPayload: CompleteCoopExit,
138
+ variables: {
139
+ user_outbound_transfer_external_id: userOutboundTransferExternalId,
140
+ coop_exit_request_id: coopExitRequestId,
141
+ },
142
+ constructObject: (response: { complete_coop_exit: any }) => {
143
+ return CoopExitRequestFromJson(response.complete_coop_exit.request);
144
+ },
145
+ });
146
+ }
147
+
148
+ async requestCoopExit({
149
+ leafExternalIds,
150
+ withdrawalAddress,
151
+ }: RequestCoopExitInput): Promise<CoopExitRequest | null> {
152
+ return await this.executeRawQuery({
153
+ queryPayload: RequestCoopExit,
154
+ variables: {
155
+ leaf_external_ids: leafExternalIds,
156
+ withdrawal_address: withdrawalAddress,
157
+ },
158
+ constructObject: (response: { request_coop_exit: any }) => {
159
+ return CoopExitRequestFromJson(response.request_coop_exit.request);
160
+ },
161
+ });
162
+ }
163
+
164
+ // TODO: Lets name this better
165
+ async requestLightningReceive({
166
+ amountSats,
167
+ network,
168
+ paymentHash,
169
+ expirySecs,
170
+ memo,
171
+ }: RequestLightningReceiveInput): Promise<LightningReceiveRequest | null> {
172
+ return await this.executeRawQuery({
173
+ queryPayload: RequestLightningReceive,
174
+ variables: {
175
+ amount_sats: amountSats,
176
+ network: network,
177
+ payment_hash: paymentHash,
178
+ expiry_secs: expirySecs,
179
+ memo: memo,
180
+ },
181
+ constructObject: (response: { request_lightning_receive: any }) => {
182
+ return LightningReceiveRequestFromJson(
183
+ response.request_lightning_receive.request,
184
+ );
185
+ },
186
+ });
187
+ }
188
+
189
+ async requestLightningSend({
190
+ encodedInvoice,
191
+ idempotencyKey,
192
+ }: RequestLightningSendInput): Promise<LightningSendRequest | null> {
193
+ return await this.executeRawQuery({
194
+ queryPayload: RequestLightningSend,
195
+ variables: {
196
+ encoded_invoice: encodedInvoice,
197
+ idempotency_key: idempotencyKey,
198
+ },
199
+ constructObject: (response: { request_lightning_send: any }) => {
200
+ return LightningSendRequestFromJson(
201
+ response.request_lightning_send.request,
202
+ );
203
+ },
204
+ });
205
+ }
206
+
207
+ async requestLeaveSwap({
208
+ adaptorPubkey,
209
+ totalAmountSats,
210
+ targetAmountSats,
211
+ feeSats,
212
+ userLeaves,
213
+ }: RequestLeavesSwapInput): Promise<LeavesSwapRequest | null> {
214
+ const query = {
215
+ queryPayload: RequestSwapLeaves,
216
+ variables: {
217
+ adaptor_pubkey: adaptorPubkey,
218
+ total_amount_sats: totalAmountSats,
219
+ target_amount_sats: targetAmountSats,
220
+ fee_sats: feeSats,
221
+ user_leaves: userLeaves,
222
+ },
223
+ constructObject: (response: { request_leaves_swap: any }) => {
224
+ if (!response.request_leaves_swap) {
225
+ return null;
226
+ }
227
+
228
+ return LeavesSwapRequestFromJson(response.request_leaves_swap.request);
229
+ },
230
+ };
231
+ return await this.executeRawQuery(query);
232
+ }
233
+
234
+ async completeLeaveSwap({
235
+ adaptorSecretKey,
236
+ userOutboundTransferExternalId,
237
+ leavesSwapRequestId,
238
+ }: CompleteLeavesSwapInput): Promise<LeavesSwapRequest | null> {
239
+ return await this.executeRawQuery({
240
+ queryPayload: CompleteLeavesSwap,
241
+ variables: {
242
+ adaptor_secret_key: adaptorSecretKey,
243
+ user_outbound_transfer_external_id: userOutboundTransferExternalId,
244
+ leaves_swap_request_id: leavesSwapRequestId,
245
+ },
246
+ constructObject: (response: { complete_leaves_swap: any }) => {
247
+ return LeavesSwapRequestFromJson(response.complete_leaves_swap.request);
248
+ },
249
+ });
250
+ }
251
+ }
252
+
253
+ class SparkAuthProvider implements AuthProvider {
254
+ private publicKey: string;
255
+
256
+ constructor(publicKey: string) {
257
+ this.publicKey = publicKey;
258
+ }
259
+
260
+ async addAuthHeaders(
261
+ headers: Record<string, string>,
262
+ ): Promise<Record<string, string>> {
263
+ const _headers = {
264
+ "Spark-Identity-Public-Key": this.publicKey,
265
+ "Content-Type": "application/json",
266
+ };
267
+ return Promise.resolve(_headers);
268
+ }
269
+
270
+ async isAuthorized(): Promise<boolean> {
271
+ return Promise.resolve(true);
272
+ }
273
+
274
+ async addWsConnectionParams(
275
+ params: Record<string, unknown>,
276
+ ): Promise<Record<string, unknown>> {
277
+ return Promise.resolve({
278
+ ...params,
279
+ "Spark-Identity-Public-Key": this.publicKey,
280
+ });
281
+ }
282
+ }
@@ -0,0 +1,19 @@
1
+ import { FRAGMENT as CompleteCoopExitOutputFragment } from "../objects/CoopExitRequest.js";
2
+
3
+ export const CompleteCoopExit = `
4
+ mutation CompleteCoopExit(
5
+ $user_outbound_transfer_external_id: UUID!
6
+ $coop_exit_request_id: ID!
7
+ ) {
8
+ complete_coop_exit(input: {
9
+ user_outbound_transfer_external_id: $user_outbound_transfer_external_id
10
+ coop_exit_request_id: $coop_exit_request_id
11
+ }) {
12
+ request {
13
+ ...CoopExitRequestFragment
14
+ }
15
+ }
16
+ }
17
+
18
+ ${CompleteCoopExitOutputFragment}
19
+ `;
@@ -0,0 +1,17 @@
1
+ import { FRAGMENT as LeavesSwapRequestFragment } from "../objects/LeavesSwapRequest.js";
2
+
3
+ export const CompleteLeavesSwap = `
4
+ mutation CompleteLeavesSwap(
5
+ $adaptor_secret_key: String!
6
+ $user_outbound_transfer_external_id: UUID!
7
+ $leaves_swap_request_id: ID!
8
+ ) {
9
+ complete_leaves_swap(input: { adaptor_secret_key: $adaptor_secret_key, user_outbound_transfer_external_id: $user_outbound_transfer_external_id, leaves_swap_request_id: $leaves_swap_request_id }) {
10
+ request {
11
+ ...LeavesSwapRequestFragment
12
+ }
13
+ }
14
+ }
15
+
16
+ ${LeavesSwapRequestFragment}
17
+ `;
@@ -0,0 +1,20 @@
1
+ import { FRAGMENT as RequestCoopExitOutputFragment } from "../objects/CoopExitRequest.js";
2
+
3
+ export const RequestCoopExit = `
4
+ mutation RequestCoopExit(
5
+ $leaf_external_ids: [UUID!]!
6
+ $withdrawal_address: String!
7
+ ) {
8
+ request_coop_exit(
9
+ input: {
10
+ leaf_external_ids: $leaf_external_ids
11
+ withdrawal_address: $withdrawal_address
12
+ }
13
+ ) {
14
+ request {
15
+ ...CoopExitRequestFragment
16
+ }
17
+ }
18
+ }
19
+ ${RequestCoopExitOutputFragment}
20
+ `;
@@ -0,0 +1,26 @@
1
+ import { FRAGMENT as RequestLightningReceiveOutputFragment } from "../objects/LightningReceiveRequest.js";
2
+
3
+ export const RequestLightningReceive = `
4
+ mutation RequestLightningReceive(
5
+ $network: BitcoinNetwork!
6
+ $amount_sats: Long!
7
+ $payment_hash: Hash32!
8
+ $expiry_secs: Int
9
+ $memo: String
10
+ ) {
11
+ request_lightning_receive(
12
+ input: {
13
+ network: $network
14
+ amount_sats: $amount_sats
15
+ payment_hash: $payment_hash
16
+ expiry_secs: $expiry_secs
17
+ memo: $memo
18
+ }
19
+ ) {
20
+ request {
21
+ ...LightningReceiveRequestFragment
22
+ }
23
+ }
24
+ }
25
+ ${RequestLightningReceiveOutputFragment}
26
+ `;
@@ -0,0 +1,17 @@
1
+ import { FRAGMENT as RequestLightningSendOutputFragment } from "../objects/LightningSendRequest.js";
2
+ export const RequestLightningSend = `
3
+ mutation RequestLightningSend(
4
+ $encoded_invoice: String!
5
+ $idempotency_key: String!
6
+ ) {
7
+ request_lightning_send(input: {
8
+ encoded_invoice: $encoded_invoice
9
+ idempotency_key: $idempotency_key
10
+ }) {
11
+ request {
12
+ ...LightningSendRequestFragment
13
+ }
14
+ }
15
+ }
16
+ ${RequestLightningSendOutputFragment}
17
+ `;
@@ -0,0 +1,24 @@
1
+ import { FRAGMENT as LeavesSwapRequestFragment } from "../objects/LeavesSwapRequest.js";
2
+
3
+ export const RequestSwapLeaves = `
4
+ mutation RequestSwapLeaves(
5
+ $adaptor_pubkey: String!
6
+ $total_amount_sats: Long!
7
+ $target_amount_sats: Long!
8
+ $fee_sats: Long!
9
+ $user_leaves: [UserLeafInput!]!
10
+ ) {
11
+ request_leaves_swap(input: {
12
+ adaptor_pubkey: $adaptor_pubkey
13
+ total_amount_sats: $total_amount_sats
14
+ target_amount_sats: $target_amount_sats
15
+ fee_sats: $fee_sats
16
+ user_leaves: $user_leaves
17
+ }) {
18
+ request {
19
+ ...LeavesSwapRequestFragment
20
+ }
21
+ }
22
+ }
23
+ ${LeavesSwapRequestFragment}
24
+ `;
@@ -0,0 +1,22 @@
1
+
2
+ // Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
3
+
4
+ /** This is an enum identifying a particular Bitcoin Network. **/
5
+ export enum BitcoinNetwork {
6
+ /**
7
+ * This is an enum value that represents values that could be added in the future.
8
+ * Clients should support unknown values as more of them could be added without notice.
9
+ */
10
+ FUTURE_VALUE = "FUTURE_VALUE",
11
+ /** The production version of the Bitcoin Blockchain. **/
12
+ MAINNET = "MAINNET",
13
+ /** A test version of the Bitcoin Blockchain, maintained by Lightspark. **/
14
+ REGTEST = "REGTEST",
15
+ /** A test version of the Bitcoin Blockchain, maintained by a centralized organization. Not in use at Lightspark. **/
16
+ SIGNET = "SIGNET",
17
+ /** A test version of the Bitcoin Blockchain, publicly available. **/
18
+ TESTNET = "TESTNET",
19
+
20
+ }
21
+
22
+ export default BitcoinNetwork;
@@ -0,0 +1,41 @@
1
+
2
+ // Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
3
+
4
+
5
+
6
+
7
+
8
+ interface CompleteCoopExitInput {
9
+
10
+
11
+ userOutboundTransferExternalId: string;
12
+
13
+ coopExitRequestId: string;
14
+
15
+
16
+
17
+
18
+ }
19
+
20
+ export const CompleteCoopExitInputFromJson = (obj: any): CompleteCoopExitInput => {
21
+ return {
22
+ userOutboundTransferExternalId: obj["complete_coop_exit_input_user_outbound_transfer_external_id"],
23
+ coopExitRequestId: obj["complete_coop_exit_input_coop_exit_request_id"],
24
+
25
+ } as CompleteCoopExitInput;
26
+
27
+ }
28
+ export const CompleteCoopExitInputToJson = (obj: CompleteCoopExitInput): any => {
29
+ return {
30
+ complete_coop_exit_input_user_outbound_transfer_external_id: obj.userOutboundTransferExternalId,
31
+ complete_coop_exit_input_coop_exit_request_id: obj.coopExitRequestId,
32
+
33
+ }
34
+
35
+ }
36
+
37
+
38
+
39
+
40
+
41
+ export default CompleteCoopExitInput;