@chipi-stack/backend 14.6.0 → 14.8.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.mjs CHANGED
@@ -400,21 +400,52 @@ async function buildEd25519Envelope(params) {
400
400
  );
401
401
  }
402
402
  const ed = await import('@noble/ed25519');
403
- const garaga = await ensureGaragaInit();
404
403
  const publicKey = await ed.getPublicKeyAsync(params.privateKey);
405
404
  const signedBytes = ed25519SignedBytes(params.messageHash);
406
405
  const signature = await ed.signAsync(signedBytes, params.privateKey);
407
- const R = signature.slice(0, 32);
408
- const s = signature.slice(32, 64);
409
- const payload = garaga.eddsaCalldataBuilder(
410
- bytesToLeBigInt(R),
411
- bytesToLeBigInt(s),
412
- bytesToLeBigInt(publicKey),
406
+ return assembleEd25519Envelope({
407
+ R: signature.slice(0, 32),
408
+ s: signature.slice(32, 64),
409
+ publicKey,
413
410
  signedBytes,
411
+ ownerIndex
412
+ });
413
+ }
414
+ async function buildEd25519EnvelopeFromSignature(params) {
415
+ const ownerIndex = params.ownerIndex ?? 0n;
416
+ if (ownerIndex < 0n || ownerIndex >= 0x100000000n) {
417
+ throw new Error(
418
+ `buildEd25519EnvelopeFromSignature: ownerIndex ${ownerIndex} is out of u32 range [0, 2^32). owner_id is the u32 INDEX into the wallet's owner_set (0 for primary); do NOT pass an owner_commitment or pubkey here.`
419
+ );
420
+ }
421
+ if (params.signature.length !== 64) {
422
+ throw new Error(
423
+ `Ed25519 signature must be exactly 64 bytes (R\u2016s); got ${params.signature.length}.`
424
+ );
425
+ }
426
+ if (params.publicKey.length !== 32) {
427
+ throw new Error(`Ed25519 public key must be exactly 32 bytes; got ${params.publicKey.length}.`);
428
+ }
429
+ const signedBytes = ed25519SignedBytes(params.messageHash);
430
+ return assembleEd25519Envelope({
431
+ R: params.signature.slice(0, 32),
432
+ s: params.signature.slice(32, 64),
433
+ publicKey: params.publicKey,
434
+ signedBytes,
435
+ ownerIndex
436
+ });
437
+ }
438
+ async function assembleEd25519Envelope(parts) {
439
+ const garaga = await ensureGaragaInit();
440
+ const payload = garaga.eddsaCalldataBuilder(
441
+ bytesToLeBigInt(parts.R),
442
+ bytesToLeBigInt(parts.s),
443
+ bytesToLeBigInt(parts.publicKey),
444
+ parts.signedBytes,
414
445
  false
415
446
  );
416
447
  return wrapV2Snip12Envelope({
417
- ownerId: ownerIndex,
448
+ ownerId: parts.ownerIndex,
418
449
  kindTag: ED25519_KIND_TAG,
419
450
  payload
420
451
  });
@@ -1429,12 +1460,17 @@ var ChipiWallets = class {
1429
1460
  }
1430
1461
  const isShhh = walletType === "SHHH";
1431
1462
  const effectiveSignerKind = isShhh ? params.signerKind ?? "STARK" : void 0;
1432
- const SDK_SHIPPED_SHHH_KINDS = ["STARK", "ED25519"];
1463
+ const SDK_SHIPPED_SHHH_KINDS = [
1464
+ "STARK",
1465
+ "ED25519",
1466
+ "WEBAUTHN_P256",
1467
+ "EIP191_SECP256K1"
1468
+ ];
1433
1469
  if (isShhh && !SDK_SHIPPED_SHHH_KINDS.includes(
1434
1470
  effectiveSignerKind
1435
1471
  )) {
1436
1472
  throw new Error(
1437
- `SHHH signerKind=${effectiveSignerKind} is not yet shipped from the SDK (Wave B ships STARK + ED25519). Use signerKind="STARK" or "ED25519", or wait for the follow-up PRs that add external-rooted (MetaMask, Phantom, passkey, Apple) deploy support.`
1473
+ `SHHH signerKind=${effectiveSignerKind} is not yet shipped from the SDK (supports STARK, ED25519, WEBAUTHN_P256, EIP191_SECP256K1). JWT_ES256_APPLE_SUB and others remain deferred to their follow-up PRs (OAuth / Apple-Developer context).`
1438
1474
  );
1439
1475
  }
1440
1476
  const rpcUrl = walletType === "READY" ? WALLET_RPC_ENDPOINTS.READY : WALLET_RPC_ENDPOINTS.CHIPI;
@@ -1469,6 +1505,14 @@ var ChipiWallets = class {
1469
1505
  pubkeyFelts = ed25519PubkeyFelts(edPubKey);
1470
1506
  shhhPrivateKeyHex = Buffer.from(edPrivKey).toString("hex");
1471
1507
  }
1508
+ } else if (effectiveSignerKind === "WEBAUTHN_P256" || effectiveSignerKind === "EIP191_SECP256K1") {
1509
+ if (!params.ownerPubkeyFelts || params.ownerPubkeyFelts.length !== 4) {
1510
+ throw new Error(
1511
+ `SHHH ${effectiveSignerKind} ownerPubkeyFelts must be [x_low, x_high, y_low, y_high] (4 felts); got ${params.ownerPubkeyFelts?.length ?? 0}.`
1512
+ );
1513
+ }
1514
+ pubkeyFelts = params.ownerPubkeyFelts.map((s) => BigInt(s));
1515
+ shhhPrivateKeyHex = "";
1472
1516
  } else {
1473
1517
  throw new Error(
1474
1518
  `SHHH signerKind=${effectiveSignerKind} reached the address-derivation block without an implementation.`
@@ -1579,12 +1623,18 @@ var ChipiWallets = class {
1579
1623
  userSignature: userSignaturePayload,
1580
1624
  typedData: typedData2,
1581
1625
  ...encryptedPrivateKey !== void 0 && { encryptedPrivateKey },
1582
- // SHHH: write the signer kind to the new wallet row. P0.3's
1583
- // STARK-only path uses a Chipi-held STARK key, so
1584
- // ownerPublicKey stays unset (encryptedPrivateKey carries
1585
- // the same key the verifier checks against). External-rooted
1586
- // wallets will pass ownerPublicKey instead in a follow-up PR.
1626
+ // SHHH: write the signer kind to the new wallet row.
1587
1627
  ...isShhh && { signerKind: effectiveSignerKind },
1628
+ // External-rooted SHHH (passkey / MetaMask / Phantom): no
1629
+ // Chipi-held key, so chipi-back needs the pubkey felts (address
1630
+ // derivation) + `ownerPublicKey` as the credential source it
1631
+ // stores (encryptedPrivateKey stays null). Canonical
1632
+ // ownerPublicKey = comma-joined felts. STARK / Chipi-held kinds
1633
+ // are untouched — encryptedPrivateKey is their credential source.
1634
+ ...isShhh && encryptedPrivateKey === void 0 && shhhOwnerPubkeyFelts && {
1635
+ ownerPubkeyFelts: shhhOwnerPubkeyFelts,
1636
+ ownerPublicKey: shhhOwnerPubkeyFelts.join(",")
1637
+ },
1588
1638
  ...encryptedPrivateKeyBackup && { encryptedPrivateKeyBackup },
1589
1639
  ...params.authMethod && { authMethod: params.authMethod },
1590
1640
  ...params.credentialId && { credentialId: params.credentialId },
@@ -2771,6 +2821,79 @@ var ChipiSessions = class {
2771
2821
  }
2772
2822
  }
2773
2823
  };
2824
+ var ChipiTreasury = class {
2825
+ constructor(client, apiSecretKey) {
2826
+ this.client = client;
2827
+ this.apiSecretKey = apiSecretKey;
2828
+ }
2829
+ base(treasuryId) {
2830
+ return `${API_ENDPOINTS.TREASURIES}/${treasuryId}/transactions`;
2831
+ }
2832
+ /** Resolve a `sk_` for the secret-gated reads/execute, or fail loudly. */
2833
+ secret(bearerToken) {
2834
+ const token = bearerToken ?? this.apiSecretKey;
2835
+ if (!token) {
2836
+ throw new Error(
2837
+ "A secret key (sk_) is required for treasury execute / list / allowance. Pass `bearerToken` to forTreasury(...) or configure the SDK with apiSecretKey. Call these from a server, never the browser."
2838
+ );
2839
+ }
2840
+ return token;
2841
+ }
2842
+ /**
2843
+ * Build a {@link TreasuryTransport} for one treasury + owner. Wire the
2844
+ * returned object directly into chipi-react's `useProposeAction` /
2845
+ * `useTreasuryProposals` `transport` prop, or call its methods yourself.
2846
+ */
2847
+ forTreasury(config) {
2848
+ const { treasuryId, ownerPubkey } = config;
2849
+ const base = this.base(treasuryId);
2850
+ return {
2851
+ // sk_ — read the proposal list. chipi-back returns
2852
+ // { items, total, page, limit, hasMore }; the hook only reads `items`.
2853
+ list: async () => {
2854
+ const res = await this.client.get({
2855
+ endpoint: base,
2856
+ bearerToken: this.secret(config.bearerToken)
2857
+ });
2858
+ return { ...res, items: res.items ?? [] };
2859
+ },
2860
+ // pk_ + owner sig — no secret. `ownerPubkey` lets the backend verify the
2861
+ // proposer envelope. `meta` (if present) is stripped server-side.
2862
+ propose: async (body) => {
2863
+ return this.client.post({
2864
+ endpoint: base,
2865
+ // Empty string → no Authorization header; the pk_ travels in
2866
+ // x-api-key, which ApiKeyGuard accepts for propose.
2867
+ bearerToken: "",
2868
+ body: { ...body, ownerPubkey }
2869
+ });
2870
+ },
2871
+ // pk_ + owner sig — approve an existing proposal.
2872
+ sign: async (proposalId, body) => {
2873
+ return this.client.post({
2874
+ endpoint: `${base}/${proposalId}/sign`,
2875
+ bearerToken: "",
2876
+ body: { ...body, ownerPubkey }
2877
+ });
2878
+ },
2879
+ // sk_ — relay the assembled threshold calldata through the paymaster.
2880
+ execute: async (proposalId, calldata) => {
2881
+ return this.client.post({
2882
+ endpoint: `${base}/${proposalId}/execute`,
2883
+ bearerToken: this.secret(config.bearerToken),
2884
+ body: { calldata }
2885
+ });
2886
+ },
2887
+ // sk_ — the free-execute meter (org-scoped).
2888
+ getMultisigAllowance: async () => {
2889
+ return this.client.get({
2890
+ endpoint: `${API_ENDPOINTS.TREASURIES}/${treasuryId}/multisig-allowance`,
2891
+ bearerToken: this.secret(config.bearerToken)
2892
+ });
2893
+ }
2894
+ };
2895
+ }
2896
+ };
2774
2897
  var InMemoryNonceStore = class {
2775
2898
  constructor() {
2776
2899
  this.used = /* @__PURE__ */ new Set();
@@ -3050,6 +3173,7 @@ var ChipiSDK = class {
3050
3173
  this.skus = new ChipiSkus(this.client);
3051
3174
  this.sessions = new ChipiSessions(this.client);
3052
3175
  this.exchanges = new ChipiExchanges(this.client);
3176
+ this.treasury = new ChipiTreasury(this.client, config.apiSecretKey);
3053
3177
  this.executeTransaction = this.executeTransaction.bind(this);
3054
3178
  this.executeTransactionWithSession = this.executeTransactionWithSession.bind(this);
3055
3179
  this.transfer = this.transfer.bind(this);
@@ -3185,9 +3309,7 @@ var ChipiSDK = class {
3185
3309
  * });
3186
3310
  * ```
3187
3311
  */
3188
- async signTypedData({
3189
- params
3190
- }) {
3312
+ async signTypedData({ params }) {
3191
3313
  return signTypedData({ params });
3192
3314
  }
3193
3315
  /**
@@ -4489,6 +4611,6 @@ function secondsUntilExecutable(validAfterSecs, nowSecs) {
4489
4611
  init_threshold();
4490
4612
  init_execute_paymaster_raw();
4491
4613
 
4492
- export { ANY_CALLER_FELT, APPLE_ISSUER, BOOTSTRAP_DOMAIN_SEPARATOR, ChipiBrowserSDK, ChipiClient, ChipiPaymasterAdapter, ChipiSDK, ChipiServerSDK, ChipiSessions, ChipiSkuPurchases, ChipiSkus, ChipiTransactions, ChipiWallets, DEFAULT_OP_EXPIRY_SECONDS, ISIGNER_CANONICAL_LABEL, ISIGNER_ID, ISRC9_V2_ID, MAX_OE_VALIDITY_WINDOW_SECONDS, MIN_THRESHOLD_OWNERS, OE_DOMAIN_NAME, OE_DOMAIN_REVISION, OE_DOMAIN_VERSION, OP_ADD_OWNER, OP_REMOVE_OWNER, OP_ROTATE_OWNER, OP_SET_THRESHOLD, ROLE_GUARDIAN, ROLE_OWNER, SIG_VERSION_V1_HEX_ASCII, SIG_VERSION_V2_SNIP12, SIG_VERSION_V2_THRESHOLD, STARKNET_MESSAGE_PREFIX, TIMELOCK_SECONDS, VIEW_SELECTORS, X402Facilitator, X402ShhhClient, assertValidV2Snip12Envelope, buildBootstrapSignedCall, buildCancelPendingOpCall, buildCancelRecoveryCall, buildEd25519Envelope, buildEip191EnvelopeFromPrivateKey, buildEip191EnvelopeFromSignature, buildExecuteAddOwnerCall, buildExecuteRemoveOwnerCall, buildExecuteRotateOwnerCall, buildExecuteSetThresholdCall, buildFinalizeRecoveryCall, buildInitiateRecoveryCall, buildJwtAppleSubEnvelopeFromTestKey, buildJwtAppleSubEnvelopeFromTokenResponse, buildMigrationCalls, buildOutsideExecution, buildProposeAddOwnerCall, buildProposeRemoveOwnerCall, buildProposeRotateOwnerCall, buildProposeSetThresholdCall, buildStarkEnvelope, buildThresholdEnvelope, buildWebAuthnEnvelopeFromAssertion, buildWebAuthnEnvelopeFromPrivateKey, byteArrayToFelts, computeAddressSalt, computeBootstrapMessage, computeISignerId, computeOeHash, computeShhhAddress, computeSnip12Hash, decryptPrivateKey, ed25519OwnerId, ed25519PubkeyFelts, ed25519SignedBytes, eip191OwnerId, eip191PubkeyFelts, encryptPrivateKey, estimateThresholdGasOverhead, executeShhhSponsoredRaw, hashCall, hashCalls, hashOutsideExecutionStruct, hashStarknetDomain, isReadyToExecute, jwtAppleSubHash, jwtAppleSubOwnerId, jwtAppleSubPubkeyFelts, labelToFelt, locateSubClaim, messageHashToNonceB64url, migrateChipiWalletToShhh, ownerCommitment, parseAppleIdToken, parseDerEcdsaSignature, parseEip191Signature, parseThresholdEnvelope, resolveShhhChainId, secondsUntilExecutable, serializeExecuteFromOutsideV2Calldata, signBootstrapMessage, signTypedData, snip12HashToHexAsciiBytes, starkOwnerId, starkPubkeyFelts, webauthnOwnerId, webauthnPubkeyFelts, wrapV2Snip12Envelope, wrapV2ThresholdEnvelope, x402Middleware };
4614
+ export { ANY_CALLER_FELT, APPLE_ISSUER, BOOTSTRAP_DOMAIN_SEPARATOR, ChipiBrowserSDK, ChipiClient, ChipiPaymasterAdapter, ChipiSDK, ChipiServerSDK, ChipiSessions, ChipiSkuPurchases, ChipiSkus, ChipiTransactions, ChipiTreasury, ChipiWallets, DEFAULT_OP_EXPIRY_SECONDS, ISIGNER_CANONICAL_LABEL, ISIGNER_ID, ISRC9_V2_ID, MAX_OE_VALIDITY_WINDOW_SECONDS, MIN_THRESHOLD_OWNERS, OE_DOMAIN_NAME, OE_DOMAIN_REVISION, OE_DOMAIN_VERSION, OP_ADD_OWNER, OP_REMOVE_OWNER, OP_ROTATE_OWNER, OP_SET_THRESHOLD, ROLE_GUARDIAN, ROLE_OWNER, SIG_VERSION_V1_HEX_ASCII, SIG_VERSION_V2_SNIP12, SIG_VERSION_V2_THRESHOLD, STARKNET_MESSAGE_PREFIX, TIMELOCK_SECONDS, VIEW_SELECTORS, X402Facilitator, X402ShhhClient, assertValidV2Snip12Envelope, buildBootstrapSignedCall, buildCancelPendingOpCall, buildCancelRecoveryCall, buildEd25519Envelope, buildEd25519EnvelopeFromSignature, buildEip191EnvelopeFromPrivateKey, buildEip191EnvelopeFromSignature, buildExecuteAddOwnerCall, buildExecuteRemoveOwnerCall, buildExecuteRotateOwnerCall, buildExecuteSetThresholdCall, buildFinalizeRecoveryCall, buildInitiateRecoveryCall, buildJwtAppleSubEnvelopeFromTestKey, buildJwtAppleSubEnvelopeFromTokenResponse, buildMigrationCalls, buildOutsideExecution, buildProposeAddOwnerCall, buildProposeRemoveOwnerCall, buildProposeRotateOwnerCall, buildProposeSetThresholdCall, buildStarkEnvelope, buildThresholdEnvelope, buildWebAuthnEnvelopeFromAssertion, buildWebAuthnEnvelopeFromPrivateKey, byteArrayToFelts, computeAddressSalt, computeBootstrapMessage, computeISignerId, computeOeHash, computeShhhAddress, computeSnip12Hash, decryptPrivateKey, ed25519OwnerId, ed25519PubkeyFelts, ed25519SignedBytes, eip191OwnerId, eip191PubkeyFelts, encryptPrivateKey, estimateThresholdGasOverhead, executeShhhSponsoredRaw, hashCall, hashCalls, hashOutsideExecutionStruct, hashStarknetDomain, isReadyToExecute, jwtAppleSubHash, jwtAppleSubOwnerId, jwtAppleSubPubkeyFelts, labelToFelt, locateSubClaim, messageHashToNonceB64url, migrateChipiWalletToShhh, ownerCommitment, parseAppleIdToken, parseDerEcdsaSignature, parseEip191Signature, parseThresholdEnvelope, resolveShhhChainId, secondsUntilExecutable, serializeExecuteFromOutsideV2Calldata, signBootstrapMessage, signTypedData, snip12HashToHexAsciiBytes, starkOwnerId, starkPubkeyFelts, webauthnOwnerId, webauthnPubkeyFelts, wrapV2Snip12Envelope, wrapV2ThresholdEnvelope, x402Middleware };
4493
4615
  //# sourceMappingURL=index.mjs.map
4494
4616
  //# sourceMappingURL=index.mjs.map