@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.
Files changed (146) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/{chunk-I54FARY2.js → chunk-EAP3U3CW.js} +14 -14
  3. package/dist/chunk-GWFQ7EBA.js +3773 -0
  4. package/dist/{chunk-J2IE4Z7Y.js → chunk-NNX4OK44.js} +3487 -934
  5. package/dist/{RequestLightningSendInput-Du0z7Om7.d.cts → client-CvpTRpcw.d.cts} +422 -212
  6. package/dist/{RequestLightningSendInput-DEPd_fPO.d.ts → client-D7KgLN44.d.ts} +422 -212
  7. package/dist/graphql/objects/index.d.cts +5 -9
  8. package/dist/graphql/objects/index.d.ts +5 -9
  9. package/dist/graphql/objects/index.js +1 -1
  10. package/dist/index.cjs +20461 -23377
  11. package/dist/index.d.cts +15 -769
  12. package/dist/index.d.ts +15 -769
  13. package/dist/index.js +81 -71
  14. package/dist/index.node.cjs +21994 -25018
  15. package/dist/index.node.d.cts +312 -34
  16. package/dist/index.node.d.ts +312 -34
  17. package/dist/index.node.js +82 -176
  18. package/dist/native/index.cjs +22847 -25841
  19. package/dist/native/index.d.cts +974 -1138
  20. package/dist/native/index.d.ts +974 -1138
  21. package/dist/native/index.js +10604 -13592
  22. package/dist/proto/lrc20.d.cts +2 -2
  23. package/dist/proto/lrc20.d.ts +2 -2
  24. package/dist/proto/lrc20.js +3098 -46
  25. package/dist/proto/spark.d.cts +1 -1
  26. package/dist/proto/spark.d.ts +1 -1
  27. package/dist/proto/spark_token.d.cts +1 -1
  28. package/dist/proto/spark_token.d.ts +1 -1
  29. package/dist/{sdk-types-Cc4l4kb1.d.ts → sdk-types-BGCeea0G.d.ts} +1 -1
  30. package/dist/{sdk-types-B0SwjolI.d.cts → sdk-types-XUeQMLFP.d.cts} +1 -1
  31. package/dist/{spark-dM7EYXYQ.d.cts → spark-BbUrbvZz.d.cts} +1 -1
  32. package/dist/{spark-dM7EYXYQ.d.ts → spark-BbUrbvZz.d.ts} +1 -1
  33. package/dist/spark-wallet-BAFPpPtY.d.cts +923 -0
  34. package/dist/spark-wallet-CJkQW8pK.d.ts +923 -0
  35. package/dist/spark_bindings/native/index.d.cts +1 -1
  36. package/dist/spark_bindings/native/index.d.ts +1 -1
  37. package/dist/spark_bindings/wasm/index.d.cts +1 -1
  38. package/dist/spark_bindings/wasm/index.d.ts +1 -1
  39. package/dist/{services/index.cjs → tests/test-utils.cjs} +2512 -4380
  40. package/dist/tests/test-utils.d.cts +79 -0
  41. package/dist/tests/test-utils.d.ts +79 -0
  42. package/dist/tests/test-utils.js +85 -0
  43. package/dist/types/index.d.cts +5 -9
  44. package/dist/types/index.d.ts +5 -9
  45. package/dist/types/index.js +5 -5
  46. package/dist/{types-C-Rp0Oo7.d.cts → types-BADxR3bm.d.cts} +1 -1
  47. package/dist/{types-C-Rp0Oo7.d.ts → types-BADxR3bm.d.ts} +1 -1
  48. package/package.json +7 -35
  49. package/src/graphql/client.ts +59 -20
  50. package/src/index.node.ts +28 -2
  51. package/src/index.ts +31 -1
  52. package/src/native/index.ts +16 -2
  53. package/src/services/config.ts +4 -6
  54. package/src/services/connection.ts +131 -64
  55. package/src/services/lightning.ts +1 -2
  56. package/src/services/token-transactions.ts +7 -7
  57. package/src/services/transfer.ts +1 -1
  58. package/src/services/tree-creation.ts +1 -1
  59. package/src/services/wallet-config.ts +18 -10
  60. package/src/signer/signer.react-native.ts +2 -5
  61. package/src/signer/signer.ts +138 -64
  62. package/src/signer/types.ts +52 -0
  63. package/src/spark-wallet/spark-wallet.ts +79 -36
  64. package/src/spark-wallet/types.ts +4 -4
  65. package/src/tests/integration/coop-exit.test.ts +2 -1
  66. package/src/tests/integration/lightning.test.ts +2 -2
  67. package/src/tests/integration/swap.test.ts +1 -1
  68. package/src/tests/integration/transfer.test.ts +5 -5
  69. package/src/tests/integration/tree-creation.test.ts +1 -1
  70. package/src/tests/integration/wallet.test.ts +1 -0
  71. package/src/tests/isHermeticTest.ts +3 -24
  72. package/src/tests/{test-util.ts → test-utils.ts} +3 -7
  73. package/src/tests/wrapWithOtelSpan.test.ts +1 -1
  74. package/src/{address → utils}/address.ts +1 -1
  75. package/src/utils/crypto.ts +19 -9
  76. package/src/utils/index.ts +2 -0
  77. package/src/utils/network.ts +17 -0
  78. package/src/utils/secret-sharing.ts +1 -2
  79. package/src/utils/signing.ts +1 -1
  80. package/src/utils/token-transactions.ts +3 -3
  81. package/src/utils/unilateral-exit.ts +32 -0
  82. package/src/utils/xchain-address.ts +1 -1
  83. package/dist/BitcoinNetwork-TnABML0T.d.cts +0 -18
  84. package/dist/BitcoinNetwork-TnABML0T.d.ts +0 -18
  85. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.cts +0 -10
  86. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.ts +0 -10
  87. package/dist/address/index.cjs +0 -458
  88. package/dist/address/index.d.cts +0 -32
  89. package/dist/address/index.d.ts +0 -32
  90. package/dist/address/index.js +0 -17
  91. package/dist/chunk-5FUB65LX.js +0 -838
  92. package/dist/chunk-6264CGDM.js +0 -113
  93. package/dist/chunk-7V6N75CC.js +0 -24
  94. package/dist/chunk-C2S227QR.js +0 -2336
  95. package/dist/chunk-GSI4OLXZ.js +0 -117
  96. package/dist/chunk-GZ5IPPJ2.js +0 -170
  97. package/dist/chunk-HWJWKEIU.js +0 -75
  98. package/dist/chunk-KMUMFYFX.js +0 -137
  99. package/dist/chunk-L3EHBOUX.js +0 -0
  100. package/dist/chunk-NSJF5F5O.js +0 -325
  101. package/dist/chunk-NTFKFRQ2.js +0 -3146
  102. package/dist/chunk-PQN3C2MF.js +0 -1122
  103. package/dist/chunk-QNNSEJ4P.js +0 -232
  104. package/dist/chunk-R5PXJZQS.js +0 -277
  105. package/dist/chunk-VTUGIIWI.js +0 -0
  106. package/dist/chunk-YUPMXTCJ.js +0 -622
  107. package/dist/chunk-Z5HIAYFT.js +0 -84
  108. package/dist/index-B2AwKW5J.d.cts +0 -214
  109. package/dist/index-CJDi1HWc.d.ts +0 -214
  110. package/dist/network-BTJl-Sul.d.ts +0 -46
  111. package/dist/network-CqgsdUF2.d.cts +0 -46
  112. package/dist/services/config.cjs +0 -2354
  113. package/dist/services/config.d.cts +0 -42
  114. package/dist/services/config.d.ts +0 -42
  115. package/dist/services/config.js +0 -17
  116. package/dist/services/connection.cjs +0 -17691
  117. package/dist/services/connection.d.cts +0 -95
  118. package/dist/services/connection.d.ts +0 -95
  119. package/dist/services/connection.js +0 -11
  120. package/dist/services/index.d.cts +0 -21
  121. package/dist/services/index.d.ts +0 -21
  122. package/dist/services/index.js +0 -58
  123. package/dist/services/lrc-connection.cjs +0 -4713
  124. package/dist/services/lrc-connection.d.cts +0 -34
  125. package/dist/services/lrc-connection.d.ts +0 -34
  126. package/dist/services/lrc-connection.js +0 -11
  127. package/dist/services/token-transactions.cjs +0 -2877
  128. package/dist/services/token-transactions.d.cts +0 -75
  129. package/dist/services/token-transactions.d.ts +0 -75
  130. package/dist/services/token-transactions.js +0 -15
  131. package/dist/services/wallet-config.cjs +0 -340
  132. package/dist/services/wallet-config.d.cts +0 -56
  133. package/dist/services/wallet-config.d.ts +0 -56
  134. package/dist/services/wallet-config.js +0 -33
  135. package/dist/signer/signer.cjs +0 -2004
  136. package/dist/signer/signer.d.cts +0 -10
  137. package/dist/signer/signer.d.ts +0 -10
  138. package/dist/signer/signer.js +0 -24
  139. package/dist/signer-BocS_J6B.d.ts +0 -187
  140. package/dist/signer-DKS0AJkw.d.cts +0 -187
  141. package/dist/utils/index.cjs +0 -2947
  142. package/dist/utils/index.d.cts +0 -18
  143. package/dist/utils/index.d.ts +0 -18
  144. package/dist/utils/index.js +0 -157
  145. package/src/address/index.ts +0 -1
  146. package/src/services/lrc-connection.ts +0 -215
@@ -1,18 +0,0 @@
1
- export { a6 as BroadcastConfig, a7 as BroadcastResult, U as DEFAULT_FEE_SATS, a4 as FeeBumpTxChain, a3 as FeeBumpTxPackage, a2 as FeeRate, a0 as LeafInfo, a5 as TxChain, a1 as Utxo, t as addPrivateKeys, q as addPublicKeys, e as applyAdaptorToSignature, r as applyAdditiveTweakToPublicKey, N as calculateAvailableTokenAmount, O as checkIfSelectedOutputsAreAvailable, Z as checkIfValidSequence, B as collectResponses, f as computeTaprootKeyNoScript, ab as constructFeeBumpTx, aa as constructUnilateralExitFeeBumpPackages, a9 as constructUnilateralExitTxs, W as createRefundTx, K as createSigningCommitment, F as createSigningNonce, M as decodeBytesToSigningCommitment, J as decodeBytesToSigningNonce, L as encodeSigningCommitmentToBytes, H as encodeSigningNonceToBytes, Q as filterTokenBalanceForTokenPublicKey, d as generateAdaptorFromSignature, g as generateSignatureFromExistingAdaptor, X as getCurrentTimelock, $ as getEphemeralAnchorOutput, y as getLatestDepositTxId, _ as getNextTransactionSequence, j as getP2TRAddressFromPkScript, i as getP2TRAddressFromPublicKey, h as getP2TRScriptFromPublicKey, k as getP2WPKHAddressFromPublicKey, E as getRandomSigningNonce, n as getSigHashFromTx, G as getSigningCommitmentFromNonce, ac as getSparkAddressFromTaproot, Y as getTransactionSequence, R as getTransferPackageSigningPayload, m as getTxFromRawTxBytes, l as getTxFromRawTxHex, o as getTxId, p as getTxIdNoReverse, a8 as isEphemeralAnchorOutput, z as isTxBroadcast, x as lastKeyWithTarget, V as maybeApplyFee, A as proofOfPossessionMessageHashForDepositAddress, u as subtractPrivateKeys, s as subtractPublicKeys, w as sumOfPrivateKeys, v as validateOutboundAdaptorSignature } from '../index-B2AwKW5J.cjs';
2
- export { L as LRC_WALLET_NETWORK, c as LRC_WALLET_NETWORK_TYPE, N as Network, b as NetworkToProto, a as NetworkType, g as getNetwork, d as getNetworkFromAddress, e as getNetworkFromString } from '../network-CqgsdUF2.cjs';
3
- export { V as VerifiableSecretShare, d as bigIntToPrivateKey, c as computerLagrangeCoefficients, e as evaluatePolynomial, f as fieldDiv, a as generatePolynomialForSecretSharing, g as getRandomBigInt, m as modInverse, r as recoverSecret, s as splitSecret, b as splitSecretWithProofs, v as validateShare } from '../signer-DKS0AJkw.cjs';
4
- import '@scure/btc-signer';
5
- import '@scure/btc-signer/psbt';
6
- import '../spark-dM7EYXYQ.cjs';
7
- import '@bufbuild/protobuf/wire';
8
- import 'nice-grpc-common';
9
- import '../services/wallet-config.cjs';
10
- import '@buildonspark/lrc20-sdk';
11
- import '../RequestLightningSendInput-Du0z7Om7.cjs';
12
- import '@lightsparkdev/core';
13
- import '../BitcoinNetwork-TnABML0T.cjs';
14
- import '@scure/bip32';
15
- import '../types-C-Rp0Oo7.cjs';
16
- import 'bitcoinjs-lib';
17
- import '@buildonspark/lrc20-sdk/lrc/types';
18
- import '../address/index.cjs';
@@ -1,18 +0,0 @@
1
- export { a6 as BroadcastConfig, a7 as BroadcastResult, U as DEFAULT_FEE_SATS, a4 as FeeBumpTxChain, a3 as FeeBumpTxPackage, a2 as FeeRate, a0 as LeafInfo, a5 as TxChain, a1 as Utxo, t as addPrivateKeys, q as addPublicKeys, e as applyAdaptorToSignature, r as applyAdditiveTweakToPublicKey, N as calculateAvailableTokenAmount, O as checkIfSelectedOutputsAreAvailable, Z as checkIfValidSequence, B as collectResponses, f as computeTaprootKeyNoScript, ab as constructFeeBumpTx, aa as constructUnilateralExitFeeBumpPackages, a9 as constructUnilateralExitTxs, W as createRefundTx, K as createSigningCommitment, F as createSigningNonce, M as decodeBytesToSigningCommitment, J as decodeBytesToSigningNonce, L as encodeSigningCommitmentToBytes, H as encodeSigningNonceToBytes, Q as filterTokenBalanceForTokenPublicKey, d as generateAdaptorFromSignature, g as generateSignatureFromExistingAdaptor, X as getCurrentTimelock, $ as getEphemeralAnchorOutput, y as getLatestDepositTxId, _ as getNextTransactionSequence, j as getP2TRAddressFromPkScript, i as getP2TRAddressFromPublicKey, h as getP2TRScriptFromPublicKey, k as getP2WPKHAddressFromPublicKey, E as getRandomSigningNonce, n as getSigHashFromTx, G as getSigningCommitmentFromNonce, ac as getSparkAddressFromTaproot, Y as getTransactionSequence, R as getTransferPackageSigningPayload, m as getTxFromRawTxBytes, l as getTxFromRawTxHex, o as getTxId, p as getTxIdNoReverse, a8 as isEphemeralAnchorOutput, z as isTxBroadcast, x as lastKeyWithTarget, V as maybeApplyFee, A as proofOfPossessionMessageHashForDepositAddress, u as subtractPrivateKeys, s as subtractPublicKeys, w as sumOfPrivateKeys, v as validateOutboundAdaptorSignature } from '../index-CJDi1HWc.js';
2
- export { L as LRC_WALLET_NETWORK, c as LRC_WALLET_NETWORK_TYPE, N as Network, b as NetworkToProto, a as NetworkType, g as getNetwork, d as getNetworkFromAddress, e as getNetworkFromString } from '../network-BTJl-Sul.js';
3
- export { V as VerifiableSecretShare, d as bigIntToPrivateKey, c as computerLagrangeCoefficients, e as evaluatePolynomial, f as fieldDiv, a as generatePolynomialForSecretSharing, g as getRandomBigInt, m as modInverse, r as recoverSecret, s as splitSecret, b as splitSecretWithProofs, v as validateShare } from '../signer-BocS_J6B.js';
4
- import '@scure/btc-signer';
5
- import '@scure/btc-signer/psbt';
6
- import '../spark-dM7EYXYQ.js';
7
- import '@bufbuild/protobuf/wire';
8
- import 'nice-grpc-common';
9
- import '../services/wallet-config.js';
10
- import '@buildonspark/lrc20-sdk';
11
- import '../RequestLightningSendInput-DEPd_fPO.js';
12
- import '@lightsparkdev/core';
13
- import '../BitcoinNetwork-TnABML0T.js';
14
- import '@scure/bip32';
15
- import '../types-C-Rp0Oo7.js';
16
- import 'bitcoinjs-lib';
17
- import '@buildonspark/lrc20-sdk/lrc/types';
18
- import '../address/index.js';
@@ -1,157 +0,0 @@
1
- import {
2
- DEFAULT_FEE_SATS,
3
- checkIfValidSequence,
4
- computeTaprootKeyNoScript,
5
- constructFeeBumpTx,
6
- constructUnilateralExitFeeBumpPackages,
7
- constructUnilateralExitTxs,
8
- createRefundTx,
9
- getCurrentTimelock,
10
- getEphemeralAnchorOutput,
11
- getLatestDepositTxId,
12
- getNextTransactionSequence,
13
- getP2TRAddressFromPkScript,
14
- getP2TRAddressFromPublicKey,
15
- getP2TRScriptFromPublicKey,
16
- getP2WPKHAddressFromPublicKey,
17
- getSigHashFromTx,
18
- getSparkAddressFromTaproot,
19
- getTransactionSequence,
20
- getTransferPackageSigningPayload,
21
- getTxFromRawTxBytes,
22
- getTxFromRawTxHex,
23
- getTxId,
24
- getTxIdNoReverse,
25
- isEphemeralAnchorOutput,
26
- isTxBroadcast,
27
- maybeApplyFee,
28
- proofOfPossessionMessageHashForDepositAddress
29
- } from "../chunk-5FUB65LX.js";
30
- import {
31
- calculateAvailableTokenAmount,
32
- checkIfSelectedOutputsAreAvailable,
33
- collectResponses,
34
- filterTokenBalanceForTokenPublicKey
35
- } from "../chunk-HWJWKEIU.js";
36
- import "../chunk-OBFKIEMP.js";
37
- import "../chunk-I54FARY2.js";
38
- import "../chunk-KMUMFYFX.js";
39
- import {
40
- LRC_WALLET_NETWORK,
41
- LRC_WALLET_NETWORK_TYPE,
42
- Network,
43
- NetworkToProto,
44
- getNetwork,
45
- getNetworkFromAddress,
46
- getNetworkFromString
47
- } from "../chunk-Z5HIAYFT.js";
48
- import "../chunk-R5PXJZQS.js";
49
- import {
50
- addPrivateKeys,
51
- addPublicKeys,
52
- applyAdaptorToSignature,
53
- applyAdditiveTweakToPublicKey,
54
- createSigningCommitment,
55
- createSigningNonce,
56
- decodeBytesToSigningCommitment,
57
- decodeBytesToSigningNonce,
58
- encodeSigningCommitmentToBytes,
59
- encodeSigningNonceToBytes,
60
- generateAdaptorFromSignature,
61
- generateSignatureFromExistingAdaptor,
62
- getRandomSigningNonce,
63
- getSigningCommitmentFromNonce,
64
- lastKeyWithTarget,
65
- subtractPrivateKeys,
66
- subtractPublicKeys,
67
- sumOfPrivateKeys,
68
- validateOutboundAdaptorSignature
69
- } from "../chunk-NSJF5F5O.js";
70
- import {
71
- bigIntToPrivateKey,
72
- computerLagrangeCoefficients,
73
- evaluatePolynomial,
74
- fieldDiv,
75
- generatePolynomialForSecretSharing,
76
- getRandomBigInt,
77
- modInverse,
78
- recoverSecret,
79
- splitSecret,
80
- splitSecretWithProofs,
81
- validateShare
82
- } from "../chunk-QNNSEJ4P.js";
83
- import "../chunk-HMLOC6TE.js";
84
- import "../chunk-VTUGIIWI.js";
85
- import "../chunk-GSI4OLXZ.js";
86
- import "../chunk-BGGEVUJK.js";
87
- import "../chunk-MVRQ5US7.js";
88
- export {
89
- DEFAULT_FEE_SATS,
90
- LRC_WALLET_NETWORK,
91
- LRC_WALLET_NETWORK_TYPE,
92
- Network,
93
- NetworkToProto,
94
- addPrivateKeys,
95
- addPublicKeys,
96
- applyAdaptorToSignature,
97
- applyAdditiveTweakToPublicKey,
98
- bigIntToPrivateKey,
99
- calculateAvailableTokenAmount,
100
- checkIfSelectedOutputsAreAvailable,
101
- checkIfValidSequence,
102
- collectResponses,
103
- computeTaprootKeyNoScript,
104
- computerLagrangeCoefficients,
105
- constructFeeBumpTx,
106
- constructUnilateralExitFeeBumpPackages,
107
- constructUnilateralExitTxs,
108
- createRefundTx,
109
- createSigningCommitment,
110
- createSigningNonce,
111
- decodeBytesToSigningCommitment,
112
- decodeBytesToSigningNonce,
113
- encodeSigningCommitmentToBytes,
114
- encodeSigningNonceToBytes,
115
- evaluatePolynomial,
116
- fieldDiv,
117
- filterTokenBalanceForTokenPublicKey,
118
- generateAdaptorFromSignature,
119
- generatePolynomialForSecretSharing,
120
- generateSignatureFromExistingAdaptor,
121
- getCurrentTimelock,
122
- getEphemeralAnchorOutput,
123
- getLatestDepositTxId,
124
- getNetwork,
125
- getNetworkFromAddress,
126
- getNetworkFromString,
127
- getNextTransactionSequence,
128
- getP2TRAddressFromPkScript,
129
- getP2TRAddressFromPublicKey,
130
- getP2TRScriptFromPublicKey,
131
- getP2WPKHAddressFromPublicKey,
132
- getRandomBigInt,
133
- getRandomSigningNonce,
134
- getSigHashFromTx,
135
- getSigningCommitmentFromNonce,
136
- getSparkAddressFromTaproot,
137
- getTransactionSequence,
138
- getTransferPackageSigningPayload,
139
- getTxFromRawTxBytes,
140
- getTxFromRawTxHex,
141
- getTxId,
142
- getTxIdNoReverse,
143
- isEphemeralAnchorOutput,
144
- isTxBroadcast,
145
- lastKeyWithTarget,
146
- maybeApplyFee,
147
- modInverse,
148
- proofOfPossessionMessageHashForDepositAddress,
149
- recoverSecret,
150
- splitSecret,
151
- splitSecretWithProofs,
152
- subtractPrivateKeys,
153
- subtractPublicKeys,
154
- sumOfPrivateKeys,
155
- validateOutboundAdaptorSignature,
156
- validateShare
157
- };
@@ -1 +0,0 @@
1
- export * from "./address.js";
@@ -1,215 +0,0 @@
1
- import { isNode } from "@lightsparkdev/core";
2
- import type { Channel, ClientFactory } from "nice-grpc";
3
- import { retryMiddleware } from "nice-grpc-client-middleware-retry";
4
- import { ClientMiddlewareCall, Metadata } from "nice-grpc-common";
5
- import type {
6
- Channel as ChannelWeb,
7
- ClientFactory as ClientFactoryWeb,
8
- } from "nice-grpc-web";
9
- import { isBun, isReactNative, clientEnv } from "../constants.js";
10
- import { NetworkError } from "../errors/types.js";
11
- import { SparkServiceClient, SparkServiceDefinition } from "../proto/lrc20.js";
12
- import { RetryOptions, SparkCallOptions } from "../types/grpc.js";
13
- import { WalletConfigService } from "./config.js";
14
-
15
- // TODO: Some sort of client cleanup
16
- export class Lrc20ConnectionManager {
17
- private config: WalletConfigService;
18
- private lrc20Client:
19
- | (SparkServiceClient & { close?: () => void })
20
- | undefined;
21
-
22
- constructor(config: WalletConfigService) {
23
- this.config = config;
24
- }
25
-
26
- public async closeConnection() {
27
- this.lrc20Client?.close?.();
28
- }
29
-
30
- private async createChannelWithTLS(address: string, certPath?: string) {
31
- try {
32
- if (isNode && !isBun) {
33
- const grpcModule = await import("nice-grpc");
34
- const { ChannelCredentials, createChannel } =
35
- "default" in grpcModule ? grpcModule.default : grpcModule;
36
-
37
- if (certPath) {
38
- try {
39
- // Dynamic import for Node.js only
40
- const fs = await import("fs");
41
- const cert = fs.readFileSync(certPath);
42
- return createChannel(address, ChannelCredentials.createSsl(cert));
43
- } catch (error) {
44
- console.error("Error reading certificate:", error);
45
- // Fallback to insecure for development
46
- return createChannel(
47
- address,
48
- ChannelCredentials.createSsl(null, null, null, {
49
- rejectUnauthorized: false,
50
- }),
51
- );
52
- }
53
- } else {
54
- // No cert provided, use insecure SSL for development
55
- return createChannel(
56
- address,
57
- ChannelCredentials.createSsl(null, null, null, {
58
- rejectUnauthorized: false,
59
- }),
60
- );
61
- }
62
- } else {
63
- // Browser environment - nice-grpc-web handles TLS automatically
64
- const grpcModule = await import("nice-grpc-web");
65
- const { createChannel, FetchTransport } =
66
- "default" in grpcModule ? grpcModule.default : grpcModule;
67
- const { XHRTransport } = await import("./xhr-transport.js");
68
-
69
- return createChannel(
70
- address,
71
- isReactNative ? XHRTransport() : FetchTransport(),
72
- );
73
- }
74
- } catch (error) {
75
- console.error("Channel creation error:", error);
76
- throw new NetworkError(
77
- "Failed to create channel",
78
- {
79
- url: address,
80
- operation: "createChannel",
81
- errorCount: 1,
82
- errors: error instanceof Error ? error.message : String(error),
83
- },
84
- error as Error,
85
- );
86
- }
87
- }
88
-
89
- async createLrc20Client(
90
- certPath?: string,
91
- ): Promise<SparkServiceClient & { close?: () => void }> {
92
- if (this.lrc20Client) {
93
- return this.lrc20Client;
94
- }
95
-
96
- const channel = await this.createChannelWithTLS(
97
- this.config.getLrc20Address(),
98
- certPath,
99
- );
100
-
101
- const middleware = this.createMiddleware();
102
- const client = await this.createGrpcClient<SparkServiceClient>(
103
- SparkServiceDefinition,
104
- channel,
105
- true,
106
- middleware,
107
- );
108
-
109
- this.lrc20Client = client;
110
- return client;
111
- }
112
-
113
- private createMiddleware() {
114
- if (isNode) {
115
- return this.createNodeMiddleware();
116
- } else {
117
- return this.createBrowserMiddleware();
118
- }
119
- }
120
-
121
- private createNodeMiddleware() {
122
- return async function* (
123
- this: Lrc20ConnectionManager,
124
- call: ClientMiddlewareCall<any, any>,
125
- options: SparkCallOptions,
126
- ) {
127
- return yield* call.next(call.request, {
128
- ...options,
129
- metadata: Metadata(options.metadata).set("X-Client-Env", clientEnv),
130
- });
131
- }.bind(this);
132
- }
133
-
134
- private createBrowserMiddleware() {
135
- return async function* (
136
- this: Lrc20ConnectionManager,
137
- call: ClientMiddlewareCall<any, any>,
138
- options: SparkCallOptions,
139
- ) {
140
- return yield* call.next(call.request, {
141
- ...options,
142
- metadata: Metadata(options.metadata)
143
- .set("X-Requested-With", "XMLHttpRequest")
144
- .set("X-Grpc-Web", "1")
145
- .set("Content-Type", "application/grpc-web+proto")
146
- .set("X-Client-Env", clientEnv),
147
- });
148
- }.bind(this);
149
- }
150
-
151
- private async createGrpcClient<T>(
152
- defintion: SparkServiceDefinition,
153
- channel: Channel | ChannelWeb,
154
- withRetries: boolean,
155
- middleware?: any,
156
- ): Promise<T & { close?: () => void }> {
157
- let clientFactory: ClientFactory | ClientFactoryWeb;
158
-
159
- const retryOptions = {
160
- retry: true,
161
- retryMaxAttempts: 3,
162
- };
163
- let options: RetryOptions = {};
164
- const isNodeChannel = "close" in channel;
165
-
166
- if (isNode && isNodeChannel && !isBun) {
167
- const grpcModule = await import("nice-grpc");
168
- const { openTelemetryClientMiddleware } = await import(
169
- "nice-grpc-opentelemetry"
170
- );
171
- const { createClientFactory } =
172
- "default" in grpcModule ? grpcModule.default : grpcModule;
173
-
174
- clientFactory = createClientFactory();
175
- if (withRetries) {
176
- options = retryOptions;
177
- clientFactory = clientFactory
178
- .use(openTelemetryClientMiddleware())
179
- .use(retryMiddleware);
180
- }
181
- if (middleware) {
182
- clientFactory = clientFactory.use(middleware);
183
- }
184
- const client = clientFactory.create(defintion, channel, {
185
- "*": options,
186
- }) as T;
187
- return {
188
- ...client,
189
- close: channel.close.bind(channel),
190
- };
191
- } else if (!isNodeChannel) {
192
- const grpcModule = await import("nice-grpc-web");
193
- const { createClientFactory } =
194
- "default" in grpcModule ? grpcModule.default : grpcModule;
195
-
196
- clientFactory = createClientFactory();
197
- if (withRetries) {
198
- options = retryOptions;
199
- clientFactory = clientFactory.use(retryMiddleware);
200
- }
201
- if (middleware) {
202
- clientFactory = clientFactory.use(middleware);
203
- }
204
- const client = clientFactory.create(defintion, channel, {
205
- "*": options,
206
- }) as T;
207
- return {
208
- ...client,
209
- close: undefined,
210
- };
211
- } else {
212
- throw new Error("Channel does not have close in NodeJS environment");
213
- }
214
- }
215
- }