@toon-protocol/client 0.20.3 → 0.21.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.
package/dist/index.js CHANGED
@@ -25,6 +25,11 @@ import {
25
25
  selectLatestAddressable,
26
26
  verifyRendererTrust
27
27
  } from "./chunk-QEMD5EAI.js";
28
+ import {
29
+ deployMinaChannelZkApp,
30
+ ensureOwnedMinaZkApp,
31
+ openMinaChannelOnChain
32
+ } from "./chunk-OM3MHJHC.js";
28
33
 
29
34
  // src/ToonClient.ts
30
35
  import { generateSecretKey as generateSecretKey3, getPublicKey as getPublicKey2, finalizeEvent } from "nostr-tools/pure";
@@ -2186,181 +2191,6 @@ async function depositSolanaChannel(params) {
2186
2191
  return { depositTxSignature };
2187
2192
  }
2188
2193
 
2189
- // src/channel/mina-channel-open.ts
2190
- import { hexToMinaBase58PrivateKey as hexToMinaBase58PrivateKey2 } from "@toon-protocol/core";
2191
- var cachedO1js = null;
2192
- var cachedPaymentChannel = null;
2193
- var compiledContract = null;
2194
- var runtimeOverride = null;
2195
- async function loadMinaRuntime() {
2196
- if (cachedO1js && cachedPaymentChannel) {
2197
- return { o1js: cachedO1js, PaymentChannel: cachedPaymentChannel };
2198
- }
2199
- if (runtimeOverride) {
2200
- const injected = await runtimeOverride();
2201
- cachedO1js = injected.o1js;
2202
- cachedPaymentChannel = injected.PaymentChannel;
2203
- return injected;
2204
- }
2205
- const { createRequire } = await import("module");
2206
- const nodePath = await import("path");
2207
- const requireHere = createRequire(import.meta.url);
2208
- const mzkPkgPath = requireHere.resolve(
2209
- "@toon-protocol/mina-zkapp/package.json"
2210
- );
2211
- const requireFromMzk = createRequire(mzkPkgPath);
2212
- const o1js = requireFromMzk("o1js");
2213
- const mzkPkgJson = requireFromMzk(mzkPkgPath);
2214
- const mzkDir = nodePath.dirname(mzkPkgPath);
2215
- const mzkEntry = nodePath.join(mzkDir, mzkPkgJson.main ?? "dist/index.js");
2216
- const mzk = requireFromMzk(mzkEntry);
2217
- const PaymentChannel = mzk.PaymentChannel ?? mzk.default?.PaymentChannel;
2218
- if (!PaymentChannel) {
2219
- throw new Error(
2220
- "@toon-protocol/mina-zkapp does not export PaymentChannel \u2014 cannot open a Mina channel"
2221
- );
2222
- }
2223
- cachedO1js = o1js;
2224
- cachedPaymentChannel = PaymentChannel;
2225
- return { o1js, PaymentChannel };
2226
- }
2227
- async function getO1js() {
2228
- return (await loadMinaRuntime()).o1js;
2229
- }
2230
- async function getCompiledPaymentChannel() {
2231
- const { PaymentChannel } = await loadMinaRuntime();
2232
- if (!compiledContract) {
2233
- await PaymentChannel.compile();
2234
- compiledContract = PaymentChannel;
2235
- }
2236
- return compiledContract;
2237
- }
2238
- var MINA_CHANNEL_STATE_OPEN = 1n;
2239
- var MINA_CHANNEL_STATE_UNINITIALIZED = 0n;
2240
- async function openMinaChannelOnChain(params) {
2241
- const { Mina, PrivateKey, PublicKey, Field, AccountUpdate, fetchAccount } = await getO1js();
2242
- const network = Mina.Network(params.graphqlUrl);
2243
- Mina.setActiveInstance(network);
2244
- const txFee = params.feeNanomina ?? 100000000n;
2245
- const payerKeyBase58 = hexToMinaBase58PrivateKey2(params.payerPrivateKey);
2246
- const payerPrivateKey = PrivateKey.fromBase58(payerKeyBase58);
2247
- const payerPublicKey = payerPrivateKey.toPublicKey();
2248
- const zkAppPublicKey = PublicKey.fromBase58(params.zkAppAddress);
2249
- const readChannelState = async () => {
2250
- const res = await fetchAccount({ publicKey: zkAppPublicKey });
2251
- if (res.error || !res.account) {
2252
- throw new Error(
2253
- `Mina zkApp account ${params.zkAppAddress} not found on-chain (${String(
2254
- res.error
2255
- )}) \u2014 deploy the PaymentChannel zkApp before opening a channel`
2256
- );
2257
- }
2258
- const appState = res.account.zkapp?.appState;
2259
- const raw = appState?.[3]?.toString() ?? "0";
2260
- return BigInt(raw);
2261
- };
2262
- const readDepositTotal = async () => {
2263
- const res = await fetchAccount({ publicKey: zkAppPublicKey });
2264
- if (res.error || !res.account) {
2265
- throw new Error(
2266
- `Mina zkApp account ${params.zkAppAddress} not found on-chain (${String(
2267
- res.error
2268
- )}) \u2014 deploy the PaymentChannel zkApp before opening a channel`
2269
- );
2270
- }
2271
- const appState = res.account.zkapp?.appState;
2272
- const raw = appState?.[4]?.toString() ?? "0";
2273
- return BigInt(raw);
2274
- };
2275
- const currentState = await readChannelState();
2276
- await fetchAccount({ publicKey: payerPublicKey });
2277
- let opened = false;
2278
- let initTxHash;
2279
- let zkApp;
2280
- const getZkApp = async () => {
2281
- if (!zkApp) {
2282
- const PaymentChannel = await getCompiledPaymentChannel();
2283
- zkApp = new PaymentChannel(zkAppPublicKey);
2284
- }
2285
- return zkApp;
2286
- };
2287
- if (currentState === MINA_CHANNEL_STATE_UNINITIALIZED) {
2288
- const channel = await getZkApp();
2289
- const participantA = payerPublicKey;
2290
- const participantB = params.peerPublicKey ? PublicKey.fromBase58(params.peerPublicKey) : payerPublicKey;
2291
- const nonce = Field(0);
2292
- const timeoutField = Field((params.timeout ?? 86400n).toString());
2293
- const tokenIdField = Field(params.tokenId ?? "1");
2294
- await fetchAccount({ publicKey: zkAppPublicKey });
2295
- await fetchAccount({ publicKey: payerPublicKey });
2296
- const initTx = await Mina.transaction(
2297
- { sender: payerPublicKey, fee: Number(txFee) },
2298
- async () => {
2299
- await channel.initializeChannel(
2300
- participantA,
2301
- participantB,
2302
- nonce,
2303
- timeoutField,
2304
- tokenIdField
2305
- );
2306
- }
2307
- );
2308
- await initTx.prove();
2309
- const sentInit = await initTx.sign([payerPrivateKey]).send();
2310
- initTxHash = sentInit.hash ?? void 0;
2311
- opened = true;
2312
- await sentInit.wait();
2313
- await fetchAccount({ publicKey: zkAppPublicKey });
2314
- await fetchAccount({ publicKey: payerPublicKey });
2315
- } else if (currentState !== MINA_CHANNEL_STATE_OPEN) {
2316
- throw new Error(
2317
- `Mina channel ${params.zkAppAddress} is in state ${currentState} (not UNINITIALIZED/OPEN) \u2014 cannot open`
2318
- );
2319
- }
2320
- let depositTxHash;
2321
- if (params.deposit && params.deposit.amount > 0n) {
2322
- const channel = await getZkApp();
2323
- await fetchAccount({ publicKey: zkAppPublicKey });
2324
- const amountField = Field(params.deposit.amount.toString());
2325
- const depositTx = await Mina.transaction(
2326
- { sender: payerPublicKey, fee: Number(txFee) },
2327
- async () => {
2328
- await channel.deposit(amountField, payerPublicKey);
2329
- }
2330
- );
2331
- await depositTx.prove();
2332
- const sentDeposit = await depositTx.sign([payerPrivateKey]).send();
2333
- depositTxHash = sentDeposit.hash ?? void 0;
2334
- await sentDeposit.wait();
2335
- await fetchAccount({ publicKey: zkAppPublicKey });
2336
- await fetchAccount({ publicKey: payerPublicKey });
2337
- }
2338
- let finalState;
2339
- try {
2340
- finalState = Number(await readChannelState());
2341
- } catch {
2342
- finalState = opened ? Number(MINA_CHANNEL_STATE_OPEN) : Number(currentState);
2343
- }
2344
- if (opened && finalState === Number(MINA_CHANNEL_STATE_UNINITIALIZED)) {
2345
- finalState = Number(MINA_CHANNEL_STATE_OPEN);
2346
- }
2347
- void AccountUpdate;
2348
- let depositTotal;
2349
- try {
2350
- depositTotal = await readDepositTotal();
2351
- } catch {
2352
- depositTotal = 0n;
2353
- }
2354
- return {
2355
- zkAppAddress: params.zkAppAddress,
2356
- opened,
2357
- initTxHash,
2358
- depositTxHash,
2359
- channelState: finalState,
2360
- depositTotal
2361
- };
2362
- }
2363
-
2364
2194
  // src/channel/OnChainChannelClient.ts
2365
2195
  var TOKEN_NETWORK_ABI = [
2366
2196
  {
@@ -2870,15 +2700,28 @@ var OnChainChannelClient = class {
2870
2700
  "Mina channel config not provided \u2014 cannot open Mina channel"
2871
2701
  );
2872
2702
  }
2873
- const zkAppAddress = this.minaConfig.zkAppAddress;
2874
- if (!zkAppAddress) {
2703
+ if (!params.peerAddress) {
2875
2704
  throw new Error(
2876
- "Mina channel requires a deployed zkAppAddress (minaConfig.zkAppAddress)"
2705
+ "Mina channel requires peerAddress (apex Mina settlement B62) so the on-chain channel is opened two-party \u2014 the participant-form claim cannot settle against a single-party channel"
2877
2706
  );
2878
2707
  }
2879
- if (!params.peerAddress) {
2708
+ let zkAppAddress = this.minaConfig.zkAppAddress;
2709
+ if (this.minaConfig.autoDeploy) {
2710
+ const { ensureOwnedMinaZkApp: ensureOwnedMinaZkApp2 } = await import("./mina-channel-deploy-CO2LMPPB.js");
2711
+ const ensured = await ensureOwnedMinaZkApp2({
2712
+ graphqlUrl: this.minaConfig.graphqlUrl,
2713
+ payerPrivateKey: this.minaConfig.privateKey,
2714
+ peerPublicKey: params.peerAddress,
2715
+ ...this.minaConfig.autoDeploy.deployed ? { deployed: this.minaConfig.autoDeploy.deployed } : {},
2716
+ ...zkAppAddress ? { candidateZkAppAddress: zkAppAddress } : {},
2717
+ ...this.minaConfig.autoDeploy.onDeployed ? { onDeployed: this.minaConfig.autoDeploy.onDeployed } : {},
2718
+ ...this.minaConfig.autoDeploy.onProgress ? { onProgress: this.minaConfig.autoDeploy.onProgress } : {}
2719
+ });
2720
+ zkAppAddress = ensured.zkAppAddress;
2721
+ }
2722
+ if (!zkAppAddress) {
2880
2723
  throw new Error(
2881
- "Mina channel requires peerAddress (apex Mina settlement B62) so the on-chain channel is opened two-party \u2014 the participant-form claim cannot settle against a single-party channel"
2724
+ "Mina channel requires a deployed zkAppAddress (minaConfig.zkAppAddress)"
2882
2725
  );
2883
2726
  }
2884
2727
  const timeout = BigInt(
@@ -3327,7 +3170,7 @@ var SolanaSigner = class {
3327
3170
  };
3328
3171
 
3329
3172
  // src/signing/mina-signer.ts
3330
- import { hexToMinaBase58PrivateKey as hexToMinaBase58PrivateKey3 } from "@toon-protocol/core";
3173
+ import { hexToMinaBase58PrivateKey as hexToMinaBase58PrivateKey2 } from "@toon-protocol/core";
3331
3174
  import { sha256 as sha2563 } from "@noble/hashes/sha2.js";
3332
3175
  import { bytesToHex } from "@noble/hashes/utils.js";
3333
3176
 
@@ -3574,7 +3417,7 @@ var MinaSigner = class {
3574
3417
  "MinaSigner requires a zkAppAddress (channel id) to sign a balance proof"
3575
3418
  );
3576
3419
  }
3577
- const minaPrivateKey = hexToMinaBase58PrivateKey3(this.privateKey);
3420
+ const minaPrivateKey = hexToMinaBase58PrivateKey2(this.privateKey);
3578
3421
  const tokenId = params.metadata.tokenId ?? DEFAULT_MINA_TOKEN_ID;
3579
3422
  const salt = deriveMinaSalt(zkAppAddress, params.nonce);
3580
3423
  const clientPubKey = this.publicKeyBase58 ?? await this.deriveOwnPublicKey(minaPrivateKey);
@@ -4861,7 +4704,7 @@ async function submitEvmSettlement(bundle, params) {
4861
4704
  }
4862
4705
 
4863
4706
  // src/swap/mina-settlement.ts
4864
- import { hexToMinaBase58PrivateKey as hexToMinaBase58PrivateKey4 } from "@toon-protocol/core";
4707
+ import { hexToMinaBase58PrivateKey as hexToMinaBase58PrivateKey3 } from "@toon-protocol/core";
4865
4708
  var MinaSettlementError = class extends Error {
4866
4709
  code;
4867
4710
  constructor(code, message) {
@@ -4930,7 +4773,7 @@ async function buildMinaCoSignedClaim(inputs) {
4930
4773
  balanceB,
4931
4774
  salt
4932
4775
  ).toString();
4933
- const recipientPrivateKeyBase58 = hexToMinaBase58PrivateKey4(
4776
+ const recipientPrivateKeyBase58 = hexToMinaBase58PrivateKey3(
4934
4777
  inputs.recipientPrivateKey
4935
4778
  );
4936
4779
  const built = await buildMinaPaymentChannelProof({
@@ -5057,7 +4900,7 @@ function createO1jsMinaClaimSubmitter() {
5057
4900
  const PaymentChannel = zkAppMod.PaymentChannel;
5058
4901
  Mina.setActiveInstance(Mina.Network(args.graphqlUrl));
5059
4902
  const feePayerKey = PrivateKey.fromBase58(
5060
- hexToMinaBase58PrivateKey4(args.feePayerPrivateKey)
4903
+ hexToMinaBase58PrivateKey3(args.feePayerPrivateKey)
5061
4904
  );
5062
4905
  const feePayerPub = feePayerKey.toPublicKey();
5063
4906
  await PaymentChannel.compile();
@@ -5350,7 +5193,8 @@ var ToonClient = class {
5350
5193
  if (this.config.minaChannel && this.minaPrivateKey) {
5351
5194
  initialization.onChainChannelClient.setMinaConfig({
5352
5195
  graphqlUrl: this.config.minaChannel.graphqlUrl,
5353
- zkAppAddress: this.config.minaChannel.zkAppAddress,
5196
+ ...this.config.minaChannel.zkAppAddress !== void 0 ? { zkAppAddress: this.config.minaChannel.zkAppAddress } : {},
5197
+ ...this.config.minaChannel.autoDeploy !== void 0 ? { autoDeploy: this.config.minaChannel.autoDeploy } : {},
5354
5198
  privateKey: this.minaPrivateKey,
5355
5199
  ...this.config.minaChannel.challengeDuration !== void 0 ? { challengeDuration: this.config.minaChannel.challengeDuration } : {},
5356
5200
  ...this.config.minaChannel.tokenId !== void 0 ? { tokenId: this.config.minaChannel.tokenId } : {},
@@ -8093,11 +7937,13 @@ export {
8093
7937
  decodeEvmSettlementTx,
8094
7938
  decryptMnemonic2 as decryptMnemonic,
8095
7939
  defaultFaucetTimeout,
7940
+ deployMinaChannelZkApp,
8096
7941
  deriveFromNsec,
8097
7942
  deriveFullIdentity,
8098
7943
  deriveNostrKeyFromMnemonic,
8099
7944
  deterministicGenerator,
8100
7945
  encryptMnemonic2 as encryptMnemonic,
7946
+ ensureOwnedMinaZkApp,
8101
7947
  entryToAccumulatedClaim,
8102
7948
  evmClaimDigest,
8103
7949
  evmCooperativeCloseDigest,