@thruputchain/client 0.0.0-stage → 0.25.0

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 (182) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +935 -2
  3. package/dist/actions-CdFSUTO2.js +298 -0
  4. package/dist/actions-CdFSUTO2.js.map +1 -0
  5. package/dist/actions-DxJZ3yIv.cjs +357 -0
  6. package/dist/actions-DxJZ3yIv.cjs.map +1 -0
  7. package/dist/actions.cjs +19 -0
  8. package/dist/actions.d.cts +4 -0
  9. package/dist/actions.d.ts +4 -0
  10. package/dist/actions.js +3 -0
  11. package/dist/client-BxB6ktQd.d.cts +690 -0
  12. package/dist/client-BxB6ktQd.d.cts.map +1 -0
  13. package/dist/client-BxB6ktQd.d.ts +690 -0
  14. package/dist/client-BxB6ktQd.d.ts.map +1 -0
  15. package/dist/codec-B7HJjMzI.cjs +8190 -0
  16. package/dist/codec-B7HJjMzI.cjs.map +1 -0
  17. package/dist/codec-CX-19jac.js +8130 -0
  18. package/dist/codec-CX-19jac.js.map +1 -0
  19. package/dist/contract-BpxjDMHw.js +1105 -0
  20. package/dist/contract-BpxjDMHw.js.map +1 -0
  21. package/dist/contract-D0wCFcAN.cjs +1219 -0
  22. package/dist/contract-D0wCFcAN.cjs.map +1 -0
  23. package/dist/contract.cjs +58 -0
  24. package/dist/contract.d.cts +3 -0
  25. package/dist/contract.d.ts +3 -0
  26. package/dist/contract.js +2 -0
  27. package/dist/contracts.cjs +1050 -0
  28. package/dist/contracts.cjs.map +1 -0
  29. package/dist/contracts.d.cts +397 -0
  30. package/dist/contracts.d.cts.map +1 -0
  31. package/dist/contracts.d.ts +397 -0
  32. package/dist/contracts.d.ts.map +1 -0
  33. package/dist/contracts.js +1030 -0
  34. package/dist/contracts.js.map +1 -0
  35. package/dist/dev_wallet-BXWK5KR6.cjs +530 -0
  36. package/dist/dev_wallet-BXWK5KR6.cjs.map +1 -0
  37. package/dist/dev_wallet-C9eMQROk.js +497 -0
  38. package/dist/dev_wallet-C9eMQROk.js.map +1 -0
  39. package/dist/domainSeparator-B_vU_zUD.cjs +102 -0
  40. package/dist/domainSeparator-B_vU_zUD.cjs.map +1 -0
  41. package/dist/domainSeparator-CBCvxIj-.js +79 -0
  42. package/dist/domainSeparator-CBCvxIj-.js.map +1 -0
  43. package/dist/execution-CRDyKu45.d.ts +227 -0
  44. package/dist/execution-CRDyKu45.d.ts.map +1 -0
  45. package/dist/execution-DyxaIG4v.d.cts +227 -0
  46. package/dist/execution-DyxaIG4v.d.cts.map +1 -0
  47. package/dist/hex-DIcaALXL.js +112 -0
  48. package/dist/hex-DIcaALXL.js.map +1 -0
  49. package/dist/hex-DkGpiZ5e.cjs +189 -0
  50. package/dist/hex-DkGpiZ5e.cjs.map +1 -0
  51. package/dist/index--5LsCotr.d.ts +192 -0
  52. package/dist/index--5LsCotr.d.ts.map +1 -0
  53. package/dist/index-B5KqSHeL.d.cts +192 -0
  54. package/dist/index-B5KqSHeL.d.cts.map +1 -0
  55. package/dist/index-BZ0O5jZG.d.cts +163 -0
  56. package/dist/index-BZ0O5jZG.d.cts.map +1 -0
  57. package/dist/index-BuPQcAK2.d.ts +23 -0
  58. package/dist/index-BuPQcAK2.d.ts.map +1 -0
  59. package/dist/index-ByNNqY7S.d.cts +83 -0
  60. package/dist/index-ByNNqY7S.d.cts.map +1 -0
  61. package/dist/index-C-M-4jMR.d.cts +263 -0
  62. package/dist/index-C-M-4jMR.d.cts.map +1 -0
  63. package/dist/index-C97zw2rk.d.ts +137 -0
  64. package/dist/index-C97zw2rk.d.ts.map +1 -0
  65. package/dist/index-CIHK7Ew9.d.cts +303 -0
  66. package/dist/index-CIHK7Ew9.d.cts.map +1 -0
  67. package/dist/index-CIHK7Ew9.d.ts +303 -0
  68. package/dist/index-CIHK7Ew9.d.ts.map +1 -0
  69. package/dist/index-CXuO2Zjx.d.ts +263 -0
  70. package/dist/index-CXuO2Zjx.d.ts.map +1 -0
  71. package/dist/index-DOEBWXY2.d.cts +137 -0
  72. package/dist/index-DOEBWXY2.d.cts.map +1 -0
  73. package/dist/index-DzrLv3EA.d.cts +82 -0
  74. package/dist/index-DzrLv3EA.d.cts.map +1 -0
  75. package/dist/index-DzrLv3EA.d.ts +82 -0
  76. package/dist/index-DzrLv3EA.d.ts.map +1 -0
  77. package/dist/index-fdTURpBk.d.ts +83 -0
  78. package/dist/index-fdTURpBk.d.ts.map +1 -0
  79. package/dist/index-sAzHHayu.d.ts +163 -0
  80. package/dist/index-sAzHHayu.d.ts.map +1 -0
  81. package/dist/index-uQqRk9Ww.d.cts +23 -0
  82. package/dist/index-uQqRk9Ww.d.cts.map +1 -0
  83. package/dist/index.cjs +478 -0
  84. package/dist/index.cjs.map +1 -0
  85. package/dist/index.d.cts +173 -0
  86. package/dist/index.d.cts.map +1 -0
  87. package/dist/index.d.ts +173 -0
  88. package/dist/index.d.ts.map +1 -0
  89. package/dist/index.js +231 -0
  90. package/dist/index.js.map +1 -0
  91. package/dist/provider.cjs +101 -0
  92. package/dist/provider.cjs.map +1 -0
  93. package/dist/provider.d.cts +2 -0
  94. package/dist/provider.d.ts +2 -0
  95. package/dist/provider.js +76 -0
  96. package/dist/provider.js.map +1 -0
  97. package/dist/readError-BVbfTMLv.cjs +604 -0
  98. package/dist/readError-BVbfTMLv.cjs.map +1 -0
  99. package/dist/readError-Dn_XN98x.js +551 -0
  100. package/dist/readError-Dn_XN98x.js.map +1 -0
  101. package/dist/readViews-BKJtE5J3.d.cts +594 -0
  102. package/dist/readViews-BKJtE5J3.d.cts.map +1 -0
  103. package/dist/readViews-C_FX8wby.d.ts +594 -0
  104. package/dist/readViews-C_FX8wby.d.ts.map +1 -0
  105. package/dist/rolldown-runtime-D6vf50IK.cjs +28 -0
  106. package/dist/sendInvocation-2EZanZZr.cjs +661 -0
  107. package/dist/sendInvocation-2EZanZZr.cjs.map +1 -0
  108. package/dist/sendInvocation-C4ruTC-9.js +505 -0
  109. package/dist/sendInvocation-C4ruTC-9.js.map +1 -0
  110. package/dist/sign-BewmV_EL.cjs +139 -0
  111. package/dist/sign-BewmV_EL.cjs.map +1 -0
  112. package/dist/sign-DVIRyW8Y.js +122 -0
  113. package/dist/sign-DVIRyW8Y.js.map +1 -0
  114. package/dist/sign-Ve0eetRO.d.ts +48 -0
  115. package/dist/sign-Ve0eetRO.d.ts.map +1 -0
  116. package/dist/sign-eUT0iGQG.d.cts +48 -0
  117. package/dist/sign-eUT0iGQG.d.cts.map +1 -0
  118. package/dist/signed-op.cjs +9 -0
  119. package/dist/signed-op.d.cts +3 -0
  120. package/dist/signed-op.d.ts +3 -0
  121. package/dist/signed-op.js +3 -0
  122. package/dist/signedOp-Bluo0OAd.cjs +183 -0
  123. package/dist/signedOp-Bluo0OAd.cjs.map +1 -0
  124. package/dist/signedOp-x4fakrr4.js +166 -0
  125. package/dist/signedOp-x4fakrr4.js.map +1 -0
  126. package/dist/signed_op-BzVbBgv4.js +154 -0
  127. package/dist/signed_op-BzVbBgv4.js.map +1 -0
  128. package/dist/signed_op-DFgQ2yFj.d.ts +58 -0
  129. package/dist/signed_op-DFgQ2yFj.d.ts.map +1 -0
  130. package/dist/signed_op-LwxrDud8.d.cts +58 -0
  131. package/dist/signed_op-LwxrDud8.d.cts.map +1 -0
  132. package/dist/signed_op-z_8fqAi1.cjs +175 -0
  133. package/dist/signed_op-z_8fqAi1.cjs.map +1 -0
  134. package/dist/signers-BFP84wJP.cjs +137 -0
  135. package/dist/signers-BFP84wJP.cjs.map +1 -0
  136. package/dist/signers-CzOSedwJ.js +120 -0
  137. package/dist/signers-CzOSedwJ.js.map +1 -0
  138. package/dist/signers.cjs +5 -0
  139. package/dist/signers.d.cts +3 -0
  140. package/dist/signers.d.ts +3 -0
  141. package/dist/signers.js +2 -0
  142. package/dist/size-506oTE28.js +91 -0
  143. package/dist/size-506oTE28.js.map +1 -0
  144. package/dist/size-OAFfOthl.cjs +108 -0
  145. package/dist/size-OAFfOthl.cjs.map +1 -0
  146. package/dist/transaction-CwkQxQOx.js +24 -0
  147. package/dist/transaction-CwkQxQOx.js.map +1 -0
  148. package/dist/transaction-DrSrCJ5y.cjs +47 -0
  149. package/dist/transaction-DrSrCJ5y.cjs.map +1 -0
  150. package/dist/transaction.cjs +13 -0
  151. package/dist/transaction.d.cts +3 -0
  152. package/dist/transaction.d.ts +3 -0
  153. package/dist/transaction.js +4 -0
  154. package/dist/transactionHash-CO9ajbvU.js +24 -0
  155. package/dist/transactionHash-CO9ajbvU.js.map +1 -0
  156. package/dist/transactionHash-D5yf4KoL.cjs +29 -0
  157. package/dist/transactionHash-D5yf4KoL.cjs.map +1 -0
  158. package/dist/transport-B-P16rs6.cjs +252 -0
  159. package/dist/transport-B-P16rs6.cjs.map +1 -0
  160. package/dist/transport-DqyoNtUy.js +234 -0
  161. package/dist/transport-DqyoNtUy.js.map +1 -0
  162. package/dist/transport.cjs +19 -0
  163. package/dist/transport.d.cts +3 -0
  164. package/dist/transport.d.ts +3 -0
  165. package/dist/transport.js +4 -0
  166. package/dist/types-BE40oD_Q.d.cts +18 -0
  167. package/dist/types-BE40oD_Q.d.cts.map +1 -0
  168. package/dist/types-BE40oD_Q.d.ts +18 -0
  169. package/dist/types-BE40oD_Q.d.ts.map +1 -0
  170. package/dist/validation-4w-vDGAi.cjs +543 -0
  171. package/dist/validation-4w-vDGAi.cjs.map +1 -0
  172. package/dist/validation-eXATmgO0.js +411 -0
  173. package/dist/validation-eXATmgO0.js.map +1 -0
  174. package/dist/wallet-Bthapk8i.cjs +709 -0
  175. package/dist/wallet-Bthapk8i.cjs.map +1 -0
  176. package/dist/wallet-CwWPYoHF.js +667 -0
  177. package/dist/wallet-CwWPYoHF.js.map +1 -0
  178. package/dist/wallet.cjs +27 -0
  179. package/dist/wallet.d.cts +3 -0
  180. package/dist/wallet.d.ts +3 -0
  181. package/dist/wallet.js +3 -0
  182. package/package.json +122 -4
@@ -0,0 +1,667 @@
1
+ import { d as deriveContractAddress, f as hashContract, s as bytesEqual } from "./contract-BpxjDMHw.js";
2
+ import { g as toBase58, i as toHex0x, l as hashFromBase58, s as fixedFromBase58 } from "./hex-DIcaALXL.js";
3
+ import { _ as createDefaultWalletBatchInvocation, d as assertTransactionSuccess, f as createSubmissionEvidence, i as ExecutionDecodeError, m as submitAndObserve, n as signWalletTransaction, p as decodeExecution, r as prepareTransaction, s as TransactionObservationError, t as sendInvocation, u as assertTransactionAdmission, v as defaultWalletAdapter } from "./sendInvocation-C4ruTC-9.js";
4
+ import { S as pubkeyAccountPreimage, a as assertWalletTransaction, b as WalletProviderError, c as parseSignedTransactionHex, d as parseWalletAccount, f as parseWalletCapabilities, g as parseWalletProviderError, h as parseWalletProvider, l as parseSubmitTransactionResult, m as parseWalletNetworkHash, s as parseSignTransactionResult, u as parseTransactionHex, v as parseWalletSession, x as derivePubkeyAccountAddress, y as verifySignTransactionResult } from "./validation-eXATmgO0.js";
5
+ import "./provider.js";
6
+ import { AuthClaimType, ContractType, CryptographicScheme, bitMap, codec, contractAddress, deserialize, ed25519PublicKey, kMaxSigners, networkHash, serialize, sha256Hash, weight } from "@thruputchain/protocol";
7
+ import { fixedBytes } from "@thruputchain/xdr";
8
+ import { isBytes } from "@noble/hashes/utils.js";
9
+ //#region src/wallet/account.ts
10
+ const uint32Max = 4294967295n;
11
+ const uint64Max = 18446744073709551615n;
12
+ const sourceAddressKeyMask = (1n << kMaxSigners) - 1n;
13
+ /** Create a protocol `kPubkeySignature` account. */
14
+ function createPubkeyAccount(options) {
15
+ validateSigner(options.signer);
16
+ const nonce = options.nonce ?? 0n;
17
+ validateUnsigned(nonce, uint64Max, "nonce");
18
+ const scheme = options.signer.publicKey.scheme;
19
+ if (scheme !== CryptographicScheme.Ed25519 && scheme !== CryptographicScheme.MlDsa65) throw new Error("Pubkey accounts require an Ed25519 or ML-DSA-65 signer");
20
+ let publicKey;
21
+ try {
22
+ publicKey = deserialize(serialize(options.signer.publicKey, codec.PublicKey), codec.PublicKey);
23
+ } catch {
24
+ throw new Error("Invalid signer public key");
25
+ }
26
+ const preimage = pubkeyAccountPreimage(publicKey, nonce);
27
+ return {
28
+ kind: "pubkey",
29
+ address: derivePubkeyAccountAddress(preimage),
30
+ nonce,
31
+ publicKey,
32
+ authClaim: {
33
+ claim_type: AuthClaimType.kPubkeySignature,
34
+ pubkey_preimage: preimage
35
+ },
36
+ signers: Object.freeze([Object.freeze({
37
+ signer: options.signer,
38
+ publicKeyId: 0n
39
+ })])
40
+ };
41
+ }
42
+ /** Create an account using the protocol's `kSourceAddressAuthData` mode. */
43
+ function createSourceAddressAccount(options) {
44
+ if (options.address.length !== 32) throw new RangeError("address must be 32 bytes");
45
+ validateUnsigned(options.allowedKeys, sourceAddressKeyMask, "allowedKeys");
46
+ if (options.allowedKeys === 0n) throw new RangeError("allowedKeys must select at least one key");
47
+ validateUnsigned(options.requiredWeight, uint32Max, "requiredWeight");
48
+ if (options.signers.length === 0 || options.signers.length > kMaxSigners) throw new RangeError(`signers must contain between 1 and ${kMaxSigners} entries`);
49
+ const ids = /* @__PURE__ */ new Set();
50
+ const signers = options.signers.map((entry) => {
51
+ validateSigner(entry.signer);
52
+ validateUnsigned(entry.publicKeyId, kMaxSigners - 1n, "publicKeyId");
53
+ if (ids.has(entry.publicKeyId)) throw new Error(`Duplicate publicKeyId: ${entry.publicKeyId}`);
54
+ if ((options.allowedKeys & 1n << entry.publicKeyId) === 0n) throw new Error(`publicKeyId ${entry.publicKeyId} is not selected by allowedKeys`);
55
+ ids.add(entry.publicKeyId);
56
+ return {
57
+ signer: entry.signer,
58
+ publicKeyId: entry.publicKeyId
59
+ };
60
+ }).sort((left, right) => left.publicKeyId < right.publicKeyId ? -1 : 1);
61
+ return {
62
+ kind: "sourceAddress",
63
+ address: contractAddress(new Uint8Array(options.address)),
64
+ authClaim: {
65
+ claim_type: AuthClaimType.kSourceAddressAuthData,
66
+ source_address_claim: {
67
+ allowed_keys: bitMap(options.allowedKeys),
68
+ required_weight: weight(options.requiredWeight)
69
+ }
70
+ },
71
+ signers: Object.freeze(signers.map((entry) => Object.freeze(entry)))
72
+ };
73
+ }
74
+ function validateSigner(signer) {
75
+ if (!signer || typeof signer.sign !== "function" || !signer.publicKey || typeof signer.publicKey !== "object") throw new Error("Invalid signer");
76
+ }
77
+ function validateUnsigned(value, max, name) {
78
+ if (typeof value !== "bigint" || value < 0n || value > max) throw new RangeError(`${name} must be between 0 and ${max}`);
79
+ }
80
+ //#endregion
81
+ //#region src/wallet/deployment.ts
82
+ /** Wrap raw RV32 artifact bytes in the canonical protocol Contract. */
83
+ function rv32Contract(artifact) {
84
+ if (artifact.length === 0) throw new RangeError("RV32 artifact must not be empty");
85
+ return {
86
+ contract_type: ContractType.kRv32,
87
+ rv32_contract: { data: new Uint8Array(artifact) }
88
+ };
89
+ }
90
+ /**
91
+ * Hash canonical contract encoding and predict the address without creating an
92
+ * expiring transaction. Funding and initialization are explicit deployment options;
93
+ * this helper performs neither. The deployment nonce differs from the transaction
94
+ * nonce. Factory-created contracts remain factory calls with attached code.
95
+ */
96
+ function prepareContractDeployment(input) {
97
+ const contract = input.contract instanceof Uint8Array ? rv32Contract(input.contract) : deserialize(serialize(input.contract, codec.Contract), codec.Contract);
98
+ const deployer = contractAddress(new Uint8Array(input.deployer));
99
+ const deploymentNonce = sha256Hash(new Uint8Array(fixedBytes(input.deploymentNonce ?? crypto.getRandomValues(new Uint8Array(32)), 32)));
100
+ const contractHash = hashContract(contract);
101
+ return {
102
+ contract,
103
+ deployer,
104
+ deploymentNonce,
105
+ contractHash,
106
+ address: deriveContractAddress(deployer, deploymentNonce, contractHash)
107
+ };
108
+ }
109
+ /** Build a wallet-specific root deployment and attach its canonical code bytes. */
110
+ function deploymentRequest(deployment, account, options, adapter) {
111
+ if (!adapter.createDeploymentInvocation) throw new Error("Wallet adapter does not support deployment");
112
+ const snapshot = prepareContractDeployment(deployment);
113
+ if (!bytesEqual(account, snapshot.deployer) || !bytesEqual(snapshot.address, deployment.address) || !bytesEqual(snapshot.contractHash, deployment.contractHash)) throw new Error("Deployment inputs no longer match the predicted address or wallet account");
114
+ return {
115
+ invocation: adapter.createDeploymentInvocation(account, snapshot, options),
116
+ contractsToDeploy: [snapshot.contract]
117
+ };
118
+ }
119
+ /** Verify committed deployment evidence against the predicted address. */
120
+ function decodeDeployment(receipt, deployment, adapter, submission) {
121
+ assertTransactionSuccess(receipt, submission);
122
+ try {
123
+ if (!adapter.decodeDeploymentReceipt) throw new Error("Wallet adapter does not support deployment decoding");
124
+ const address = adapter.decodeDeploymentReceipt(receipt);
125
+ if (!bytesEqual(address, deployment.address)) throw new Error(`Deployment ${toBase58(receipt.transactionHash)} returned ${toBase58(address)}, expected ${toBase58(deployment.address)}`);
126
+ return address;
127
+ } catch (cause) {
128
+ throw new ExecutionDecodeError(receipt, cause, submission);
129
+ }
130
+ }
131
+ //#endregion
132
+ //#region src/wallet/client.ts
133
+ const walletClientMarker = Symbol("ThruputWalletClient");
134
+ const reservedWalletClientProperties = [
135
+ "account",
136
+ "prepareTransaction",
137
+ "signTransaction",
138
+ "sendInvocation",
139
+ "prepareExecution",
140
+ "execute",
141
+ "executeAndWait",
142
+ "executeCallsAndWait",
143
+ "prepareDeployment",
144
+ "deployContract",
145
+ "deployContractAndWait"
146
+ ];
147
+ function createWalletClient(options) {
148
+ const { client, account, onSigned } = options;
149
+ const sourceNetworkHash = options.networkHash ?? client.discovery.network.networkHash;
150
+ if (sourceNetworkHash.length !== 32 || sourceNetworkHash.some((v, i) => v !== client.discovery.network.networkHash[i])) throw new Error("Wallet signing network does not match endpoint discovery");
151
+ const signingNetworkHash = networkHash(sha256Hash(new Uint8Array(sourceNetworkHash)));
152
+ if (walletClientMarker in client) throw new Error("A client can only be bound to one wallet account");
153
+ const collision = reservedWalletClientProperties.find((property) => property in client);
154
+ if (collision) throw new Error(`Client property ${collision} conflicts with a wallet client property`);
155
+ const getBalance = client.getBalance.bind(client);
156
+ const requestAirdrop = "requestAirdrop" in client ? client.requestAirdrop.bind(client) : void 0;
157
+ const walletActions = {
158
+ account,
159
+ getBalance: (request = {}, options) => getBalance({
160
+ ...request,
161
+ address: request.address ?? account.address
162
+ }, options),
163
+ prepareTransaction: (request) => prepareTransaction(client, account, request),
164
+ signTransaction: (transaction) => signWalletTransaction(account, transaction, signingNetworkHash),
165
+ sendInvocation: (request) => sendInvocation(client, account, signingNetworkHash, request, { onSigned }),
166
+ prepareDeployment: (deployment, options) => prepareTransaction(client, account, deploymentRequest(deployment, account.address, options, requireDeploymentAdapter())),
167
+ deployContract: async (deployment, options) => {
168
+ const request = deploymentRequest(deployment, account.address, options, requireDeploymentAdapter());
169
+ const address = deployment.address.slice();
170
+ return {
171
+ transactionHash: await sendInvocation(client, account, signingNetworkHash, request, { onSigned }),
172
+ address
173
+ };
174
+ },
175
+ deployContractAndWait: async (deployment, options, wait) => {
176
+ const adapter = requireDeploymentAdapter();
177
+ if (!adapter.decodeDeploymentReceipt) throw new Error("Wallet adapter does not support deployment decoding");
178
+ const address = deployment.address.slice();
179
+ const result = await sendAndObserve(deploymentRequest(deployment, account.address, options, adapter), wait);
180
+ decodeDeployment(result.receipt, {
181
+ ...deployment,
182
+ address
183
+ }, adapter, result.submission);
184
+ return {
185
+ ...result,
186
+ address
187
+ };
188
+ }
189
+ };
190
+ const airdrop = requestAirdrop ? { requestAirdrop: (request = {}) => requestAirdrop({
191
+ ...request,
192
+ recipient: request.recipient ?? account.address
193
+ }) } : {};
194
+ const invocationAdapter = options.invocationAdapter ?? (account.kind === "pubkey" ? defaultWalletAdapter : void 0);
195
+ function requireDeploymentAdapter() {
196
+ if (!invocationAdapter) throw new Error("Wallet requires an adapter for deployment");
197
+ return invocationAdapter;
198
+ }
199
+ async function sendAndObserve(request, wait) {
200
+ wait?.signal?.throwIfAborted();
201
+ client.requireMethods([
202
+ "getChainTip",
203
+ "sendTransaction",
204
+ "getTransactionReceipt",
205
+ "getTransactionStatus",
206
+ "getNetworkInfo"
207
+ ]);
208
+ const evidence = createSubmissionEvidence(serialize(await signWalletTransaction(account, await prepareTransaction(client, account, request), signingNetworkHash), codec.SignedTransaction), signingNetworkHash);
209
+ try {
210
+ await onSigned?.(evidence);
211
+ wait?.signal?.throwIfAborted();
212
+ } catch (cause) {
213
+ throw new TransactionObservationError(evidence, cause, void 0, void 0, "beforeSubmission");
214
+ }
215
+ return submitAndObserve(client, evidence, async (signal) => {
216
+ assertTransactionAdmission(await client.sendTransaction({ signedTransaction: evidence.signedTransaction }, { signal }));
217
+ }, {
218
+ ...wait,
219
+ expectedNetworkHash: signingNetworkHash
220
+ });
221
+ }
222
+ const execution = invocationAdapter ? {
223
+ async executeCallsAndWait(request, wait) {
224
+ const calls = [...request.calls];
225
+ if (calls.length === 0) throw new RangeError("Execution requires at least one call");
226
+ const invocations = calls.map((call) => call.invocation);
227
+ const invocation = calls.length === 1 ? invocationAdapter.createRootInvocation(account.address, {
228
+ ...request,
229
+ invocation: invocations[0]
230
+ }) : invocationAdapter.createBatchRootInvocation?.(account.address, {
231
+ ...request,
232
+ invocations
233
+ });
234
+ if (!invocation) throw new Error("Wallet adapter does not support atomic batches");
235
+ const result = await sendAndObserve({
236
+ ...request,
237
+ invocation
238
+ }, wait);
239
+ return {
240
+ ...result,
241
+ values: decodeExecution(result.receipt, calls, requireDeploymentAdapter(), result.submission)
242
+ };
243
+ },
244
+ prepareExecution: (request) => prepareTransaction(client, account, {
245
+ ...request,
246
+ invocation: invocationAdapter.createRootInvocation(account.address, request)
247
+ }),
248
+ execute: (request) => sendInvocation(client, account, signingNetworkHash, {
249
+ ...request,
250
+ invocation: invocationAdapter.createRootInvocation(account.address, request)
251
+ }, { onSigned }),
252
+ executeAndWait: async (request, wait) => {
253
+ const result = await sendAndObserve({
254
+ ...request,
255
+ invocation: invocationAdapter.createRootInvocation(account.address, request)
256
+ }, wait);
257
+ assertTransactionSuccess(result.receipt, result.submission);
258
+ return result;
259
+ }
260
+ } : {};
261
+ const wallet = Object.assign(Object.defineProperties({}, Object.getOwnPropertyDescriptors(client)), walletActions, airdrop, execution);
262
+ Object.defineProperty(wallet, walletClientMarker, { value: true });
263
+ return wallet;
264
+ }
265
+ //#endregion
266
+ //#region src/wallet/providerClient.ts
267
+ /**
268
+ * Synchronously bind a pubkey provider account without modifying the RPC client.
269
+ * Does not connect or request permission: first await provider `connect` and
270
+ * validate its session. Snapshots account/network configuration, validates account
271
+ * derivation and any validated client network identity, and subscribes to changes.
272
+ * Supply the application's trusted expected network hash, not a value adopted from
273
+ * the wallet. The RPC client must be connected to and verified against that same
274
+ * network. Every native client carries a discovered network identity, checked here.
275
+ * See {@link ProviderWalletClient} for invalidation and concurrency rules.
276
+ * Providers own signing UI deadlines. Execution controls bound submission and
277
+ * observation after signing; they cannot cancel a broadcast already sent.
278
+ *
279
+ * @example
280
+ * Await an application-owned `onSigned` callback to durably save the signed result
281
+ * and its original account/network before broadcasting. If saving fails, submission
282
+ * is never requested.
283
+ * `expectedNetworkHash` comes from trusted application configuration, and `client`
284
+ * must already be connected to and verified against that network.
285
+ *
286
+ * ```ts
287
+ * import type { CoreThruputClient } from "@thruputchain/client";
288
+ * import {
289
+ * parseWalletSession,
290
+ * parseWalletNetworkHash,
291
+ * type SignTransactionResult,
292
+ * type WalletAccount,
293
+ * type WalletProvider,
294
+ * } from "@thruputchain/client/provider";
295
+ * import {
296
+ * createProviderWalletClient,
297
+ * type PrepareExecutionRequest,
298
+ * } from "@thruputchain/client/wallet";
299
+ *
300
+ * async function sendWithWallet(
301
+ * client: CoreThruputClient,
302
+ * provider: WalletProvider,
303
+ * expectedNetworkHash: string,
304
+ * request: PrepareExecutionRequest,
305
+ * onSigned: (pending: SignTransactionResult & {
306
+ * account: WalletAccount;
307
+ * networkHash: string;
308
+ * }) => Promise<void>,
309
+ * ) {
310
+ * const networkHash = parseWalletNetworkHash(expectedNetworkHash);
311
+ * const session = parseWalletSession(await provider.request({ method: "connect" }));
312
+ * if (session.networkHash !== networkHash) {
313
+ * throw new Error("Wallet network does not match the application network");
314
+ * }
315
+ * const account = session.accounts[0];
316
+ * if (!account) throw new Error("No authorized account");
317
+ * const wallet = createProviderWalletClient({
318
+ * client, provider, account, networkHash,
319
+ * });
320
+ * try {
321
+ * const transaction = await wallet.prepareExecution(request);
322
+ * const signed = await wallet.signTransaction(transaction);
323
+ * await onSigned({ ...signed, account, networkHash });
324
+ * // On submit failure, recover using the saved hash and original network.
325
+ * return await wallet.submitTransaction(signed);
326
+ * } finally {
327
+ * wallet.dispose();
328
+ * }
329
+ * }
330
+ * ```
331
+ *
332
+ * @see https://docs.thruput.network/tools/wallet-integration/
333
+ * @see https://docs.thruput.network/tools/wallet-provider/
334
+ */
335
+ function createProviderWalletClient(options) {
336
+ const { client } = options;
337
+ const provider = parseWalletProvider(options.provider);
338
+ const account = parseWalletAccount(options.account);
339
+ const networkHash = parseWalletNetworkHash(options.networkHash);
340
+ if (toBase58(client.discovery.network.networkHash) !== networkHash) throw new WalletProviderError(4901, "Client and provider network hashes differ");
341
+ if ("network" in client) {
342
+ const network = client.network;
343
+ if (!network || typeof network !== "object" || !("networkHash" in network) || !isBytes(network.networkHash) || toBase58(network.networkHash) !== networkHash) throw new WalletProviderError(4901, "Client and provider network hashes differ");
344
+ }
345
+ const preparationAccount = {
346
+ address: contractAddress(fixedFromBase58(account.address, 32)),
347
+ authClaim: {
348
+ claim_type: AuthClaimType.kPubkeySignature,
349
+ pubkey_preimage: pubkeyAccountPreimage({
350
+ scheme: CryptographicScheme.Ed25519,
351
+ ed25519: ed25519PublicKey(fixedFromBase58(account.publicKey, 32))
352
+ }, BigInt(account.nonce))
353
+ }
354
+ };
355
+ const providerRequest = provider.request.bind(provider);
356
+ const supportsCapabilities = provider.supportedMethods?.includes("getCapabilities") ?? false;
357
+ let invalidated = false;
358
+ let busy = false;
359
+ let sessionSnapshot;
360
+ let approved;
361
+ const invalidate = () => {
362
+ invalidated = true;
363
+ approved = void 0;
364
+ };
365
+ const unsubscribe = provider.on("change", invalidate);
366
+ if (typeof unsubscribe !== "function") throw new WalletProviderError(-32603, "Provider did not return an unsubscribe function");
367
+ let disposed = false;
368
+ function assertActive() {
369
+ if (invalidated) throw new WalletProviderError(4100, "Wallet session changed or adapter was disposed");
370
+ }
371
+ async function request(value, onDispatch, signal) {
372
+ signal?.throwIfAborted();
373
+ assertActive();
374
+ try {
375
+ onDispatch?.();
376
+ return await providerRequest(value);
377
+ } catch (error) {
378
+ let failure;
379
+ try {
380
+ failure = parseWalletProviderError(error);
381
+ } catch {
382
+ failure = new WalletProviderError(4900, "Wallet provider request failed");
383
+ }
384
+ if ([
385
+ 4100,
386
+ 4900,
387
+ 4901
388
+ ].includes(failure.code)) invalidate();
389
+ throw failure;
390
+ }
391
+ }
392
+ async function checkSession() {
393
+ try {
394
+ const value = await request({ method: "getSession" });
395
+ assertActive();
396
+ let session;
397
+ try {
398
+ session = parseWalletSession(value);
399
+ } catch {
400
+ throw new WalletProviderError(-32603, "Invalid provider session");
401
+ }
402
+ if (session.networkHash !== networkHash) throw new WalletProviderError(4901, "Wallet network changed");
403
+ if (!session.accounts.some((entry) => entry.address === account.address && entry.publicKey === account.publicKey && entry.nonce === account.nonce)) throw new WalletProviderError(4100, "Wallet account is not authorized");
404
+ const snapshot = JSON.stringify(session);
405
+ if (sessionSnapshot !== void 0 && sessionSnapshot !== snapshot) throw new WalletProviderError(4100, "Wallet session changed");
406
+ sessionSnapshot = snapshot;
407
+ } catch (error) {
408
+ invalidate();
409
+ throw error;
410
+ }
411
+ }
412
+ async function run(operation) {
413
+ assertActive();
414
+ if (busy) throw new WalletProviderError(-32002, "Another wallet operation is pending");
415
+ busy = true;
416
+ try {
417
+ const result = await operation();
418
+ assertActive();
419
+ return result;
420
+ } finally {
421
+ busy = false;
422
+ }
423
+ }
424
+ async function readCapabilities() {
425
+ await checkSession();
426
+ if (!supportsCapabilities) return Object.freeze({
427
+ account: account.address,
428
+ networkHash,
429
+ atomicBatch: null
430
+ });
431
+ let value;
432
+ try {
433
+ value = await request({
434
+ method: "getCapabilities",
435
+ params: {
436
+ account: account.address,
437
+ networkHash
438
+ }
439
+ });
440
+ } catch (error) {
441
+ if (!(error instanceof WalletProviderError) || error.code !== 4200) throw error;
442
+ value = {
443
+ account: account.address,
444
+ networkHash,
445
+ atomicBatch: null
446
+ };
447
+ }
448
+ assertActive();
449
+ let capabilities;
450
+ try {
451
+ capabilities = parseWalletCapabilities(value);
452
+ if (capabilities.account !== account.address || capabilities.networkHash !== networkHash) throw new Error("Capability scope mismatch");
453
+ } catch {
454
+ invalidate();
455
+ throw new WalletProviderError(-32603, "Invalid provider capabilities");
456
+ }
457
+ await checkSession();
458
+ assertActive();
459
+ if (capabilities.atomicBatch) Object.freeze(capabilities.atomicBatch);
460
+ return Object.freeze(capabilities);
461
+ }
462
+ function submitTransaction(input, onDispatch, signal) {
463
+ const checkCancellation = () => {
464
+ if (signal?.aborted) {
465
+ approved = void 0;
466
+ signal.throwIfAborted();
467
+ }
468
+ };
469
+ return run(async () => {
470
+ checkCancellation();
471
+ const signed = parseSignTransactionResult(input);
472
+ if (!approved || approved.signedTransaction !== signed.signedTransaction || approved.hash !== signed.hash) throw new WalletProviderError(4100, "Signed bytes were not approved by this adapter");
473
+ await checkSession();
474
+ checkCancellation();
475
+ approved = void 0;
476
+ const value = await request({
477
+ method: "submitTransaction",
478
+ params: {
479
+ account: account.address,
480
+ networkHash,
481
+ signedTransaction: signed.signedTransaction
482
+ }
483
+ }, onDispatch, signal);
484
+ assertActive();
485
+ let result;
486
+ try {
487
+ result = parseSubmitTransactionResult(value);
488
+ if (result.hash !== signed.hash) throw new Error("Hash mismatch");
489
+ } catch {
490
+ invalidate();
491
+ throw new WalletProviderError(-32603, "Provider submission hash mismatch; broadcast may have occurred");
492
+ }
493
+ checkCancellation();
494
+ await checkSession();
495
+ return Object.freeze(result);
496
+ });
497
+ }
498
+ const base = {
499
+ prepareDeployment: (deployment, options) => run(async () => {
500
+ const preparation = deploymentRequest(deployment, preparationAccount.address, options, defaultWalletAdapter);
501
+ await checkSession();
502
+ const tx = await prepareTransaction(client, preparationAccount, preparation);
503
+ assertActive();
504
+ return parseTransactionHex(toHex0x(serialize(tx, codec.Transaction)));
505
+ }),
506
+ getCapabilities: () => run(readCapabilities),
507
+ prepareBatchExecution: (input) => run(async () => {
508
+ const count = input.invocations.length;
509
+ const preparation = {
510
+ ...input,
511
+ invocation: createDefaultWalletBatchInvocation(preparationAccount.address, input),
512
+ contractsToDeploy: input.contractsToDeploy?.map((contract) => deserialize(serialize(contract, codec.Contract), codec.Contract))
513
+ };
514
+ const capabilities = await readCapabilities();
515
+ if (!capabilities.atomicBatch || count > capabilities.atomicBatch.maxCalls) throw new WalletProviderError(4200, "Atomic batch execution is not supported for these calls");
516
+ const tx = await prepareTransaction(client, preparationAccount, preparation);
517
+ assertActive();
518
+ return parseTransactionHex(toHex0x(serialize(tx, codec.Transaction)));
519
+ }),
520
+ prepareExecution: (input) => run(async () => {
521
+ const preparation = {
522
+ ...input,
523
+ invocation: defaultWalletAdapter.createRootInvocation(preparationAccount.address, input),
524
+ contractsToDeploy: input.contractsToDeploy?.map((contract) => deserialize(serialize(contract, codec.Contract), codec.Contract))
525
+ };
526
+ await checkSession();
527
+ const tx = await prepareTransaction(client, preparationAccount, preparation);
528
+ assertActive();
529
+ return parseTransactionHex(toHex0x(serialize(tx, codec.Transaction)));
530
+ }),
531
+ signTransaction: (input) => run(async () => {
532
+ const transaction = toHex0x(serialize(input, codec.Transaction));
533
+ assertWalletTransaction(parseTransactionHex(transaction), account);
534
+ approved = void 0;
535
+ await checkSession();
536
+ const value = await request({
537
+ method: "signTransaction",
538
+ params: {
539
+ account: account.address,
540
+ networkHash,
541
+ transaction
542
+ }
543
+ });
544
+ assertActive();
545
+ let signed;
546
+ try {
547
+ signed = await verifySignTransactionResult(value, transaction, account, networkHash);
548
+ } catch (error) {
549
+ invalidate();
550
+ throw error;
551
+ }
552
+ await checkSession();
553
+ assertActive();
554
+ approved = signed;
555
+ return signed;
556
+ }),
557
+ submitTransaction: (input) => submitTransaction(input),
558
+ dispose() {
559
+ invalidate();
560
+ if (!disposed) {
561
+ disposed = true;
562
+ unsubscribe();
563
+ }
564
+ }
565
+ };
566
+ let executing = false;
567
+ function assertIdle() {
568
+ if (executing) throw new WalletProviderError(-32002, "Another wallet execution is pending");
569
+ }
570
+ async function execute(prepare, options, requireSuccess = true) {
571
+ assertIdle();
572
+ executing = true;
573
+ try {
574
+ options.wait?.signal?.throwIfAborted();
575
+ client.requireMethods([
576
+ "getChainTip",
577
+ "getNetworkInfo",
578
+ "getTransactionReceipt",
579
+ "getTransactionStatus"
580
+ ]);
581
+ const signed = await base.signTransaction(await prepare());
582
+ const evidence = createSubmissionEvidence(serialize(parseSignedTransactionHex(signed.signedTransaction), codec.SignedTransaction), hashFromBase58(networkHash));
583
+ try {
584
+ await options.onSigned(Object.freeze({
585
+ ...signed,
586
+ account: Object.freeze({ ...account }),
587
+ networkHash
588
+ }));
589
+ options.wait?.signal?.throwIfAborted();
590
+ } catch (cause) {
591
+ approved = void 0;
592
+ throw new TransactionObservationError(evidence, cause, void 0, void 0, "beforeSubmission");
593
+ }
594
+ let submissionDispatched = false;
595
+ const result = await submitAndObserve(client, evidence, async (signal) => {
596
+ await submitTransaction(signed, () => {
597
+ submissionDispatched = true;
598
+ }, signal);
599
+ }, {
600
+ ...options.wait,
601
+ expectedNetworkHash: hashFromBase58(networkHash)
602
+ }, () => submissionDispatched);
603
+ if (requireSuccess) assertTransactionSuccess(result.receipt, result.submission);
604
+ return result;
605
+ } finally {
606
+ executing = false;
607
+ }
608
+ }
609
+ return {
610
+ ...base,
611
+ prepareDeployment: async (deployment, options) => {
612
+ assertIdle();
613
+ return base.prepareDeployment(deployment, options);
614
+ },
615
+ async deployContractAndWait(deployment, options, controls) {
616
+ const address = deployment.address.slice();
617
+ const result = await execute(() => base.prepareDeployment(deployment, options), controls, false);
618
+ decodeDeployment(result.receipt, {
619
+ ...deployment,
620
+ address
621
+ }, defaultWalletAdapter, result.submission);
622
+ return {
623
+ ...result,
624
+ address
625
+ };
626
+ },
627
+ getCapabilities: async () => {
628
+ assertIdle();
629
+ return base.getCapabilities();
630
+ },
631
+ prepareExecution: async (input) => {
632
+ assertIdle();
633
+ return base.prepareExecution(input);
634
+ },
635
+ prepareBatchExecution: async (input) => {
636
+ assertIdle();
637
+ return base.prepareBatchExecution(input);
638
+ },
639
+ signTransaction: async (input) => {
640
+ assertIdle();
641
+ return base.signTransaction(input);
642
+ },
643
+ submitTransaction: async (input) => {
644
+ assertIdle();
645
+ return base.submitTransaction(input);
646
+ },
647
+ executeAndWait: (input, controls) => execute(() => base.prepareExecution(input), controls),
648
+ async executeCallsAndWait(input, controls) {
649
+ const calls = [...input.calls];
650
+ const result = await execute(() => calls.length === 1 ? base.prepareExecution({
651
+ ...input,
652
+ invocation: calls[0].invocation
653
+ }) : base.prepareBatchExecution({
654
+ ...input,
655
+ invocations: calls.map((call) => call.invocation)
656
+ }), controls, false);
657
+ return {
658
+ ...result,
659
+ values: decodeExecution(result.receipt, calls, defaultWalletAdapter, result.submission)
660
+ };
661
+ }
662
+ };
663
+ }
664
+ //#endregion
665
+ export { rv32Contract as a, prepareContractDeployment as i, createWalletClient as n, createPubkeyAccount as o, decodeDeployment as r, createSourceAddressAccount as s, createProviderWalletClient as t };
666
+
667
+ //# sourceMappingURL=wallet-CwWPYoHF.js.map