@ocap/client 1.31.4 → 1.31.6

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/extension.js CHANGED
@@ -2,7 +2,12 @@ const { get, set, omit, camelCase } = require('./helpers/lite');
2
2
 
3
3
  const { isValid, isFromPublicKey, toTypeInfo } = require('@arcblock/did');
4
4
  const ocapProto = require('@ocap/proto/schema');
5
- const { toTxHash, parseStrictEthAddress, buildBridgeWithdrawEnvelopeExtra } = require('@ocap/mcrypto');
5
+ const {
6
+ toTxHash,
7
+ parseStrictEthAddress,
8
+ buildBridgeWithdrawEnvelopeExtra,
9
+ EIP712_ENVELOPE_DEFAULT_DOMAIN
10
+ } = require('@ocap/mcrypto');
6
11
  const { fromPublicKey } = require('@ocap/wallet');
7
12
  const { encodeTx: encodeTxCbor, decodeTx: decodeTxCbor } = require('@ocap/message/cbor');
8
13
  const { bytesToHex, toBase58, toBase64, toHex, toBuffer, fromTokenToUnit, fromUnitToToken } = require('@ocap/util');
@@ -18,8 +23,7 @@ const {
18
23
  toTokenAddress: toTokenAddressCbor,
19
24
  toTokenFactoryAddress: toTokenFactoryAddressCbor,
20
25
  toStakeAddress,
21
- toRollupAddress: toRollupAddressCbor,
22
- toBridgeReserveAddress
26
+ toRollupAddress: toRollupAddressCbor
23
27
  } = require('@arcblock/did-util/cbor');
24
28
 
25
29
  const debug = require('debug')(require('../package.json').name);
@@ -666,13 +670,16 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
666
670
  }, extra);
667
671
 
668
672
  /**
669
- * Build and sign a bridge withdraw (a `transfer_v2` into the bridge reserve, #1465), ready for `sendTransferV2Tx`.
673
+ * Build and sign a bridge withdraw, ready for `sendWithdrawTokenV2Tx`.
674
+ *
675
+ * The tx is a `withdraw_token_v2`: `{ token, to: ethRecipient, rollup: bridge }`, the reserve is derived by the chain
676
+ * (#1691). The envelope `action` is `withdraw_token_v2`.
670
677
  *
671
678
  * Signing modes (`signing`):
672
679
  * - `eip712` (default for Ethereum-type wallets): `ArcBridgeWithdraw` clear-signing envelope, shows amount, token and
673
680
  * Ethereum destination in the wallet UI;
674
681
  * - `eip191`: personal_sign over the canonical text, which carries the same three fields;
675
- * - `raw`: the plain transaction signature (what `sendTransferV2Tx` does by default). The chain only accepts it from
682
+ * - `raw`: the plain transaction signature (what `sendWithdrawTokenV2Tx` does by default). The chain only accepts it from
676
683
  * signers that do not use wallet envelopes.
677
684
  *
678
685
  * @memberof GraphQLClient
@@ -680,15 +687,17 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
680
687
  * @name GraphQLClient#buildBridgeWithdrawTx
681
688
  * @param {object} params
682
689
  * @param {WalletObject} params.wallet - the sender
683
- * @param {string} params.bridge - address of the bridge (rollup); the reserve (`itx.to`) is derived from it
690
+ * @param {string} params.bridge - address of the bridge (rollup), `itx.rollup`
684
691
  * @param {string|number|bigint} params.amount - amount in base units
685
692
  * @param {string} params.ethRecipient - Ethereum recipient, `0x` + 40 hex, lowercase or EIP-55 (checked locally)
686
693
  * @param {string} [params.token] - the bridge's OCAP token, defaults to the chain's official token
687
694
  * @param {string} [params.signing='eip712'] - `eip712` | `eip191` | `raw`
688
695
  * @param {string|number} [params.nonce] - defaults to Date.now()
689
696
  * @param {string} [params.chainId] - defaults to the connected chain
690
- * @param {object} [params.domain] - `{ name, version }` of the envelope domain, defaults to the chain's default
691
- * @returns {Promise<{ tx: object, signature: string, extra: string }>} pass as `client.sendTransferV2Tx({ tx, signature, extra })`
697
+ * @param {object} [params.domain] - `{ name, version }` of the envelope domain; only the default `ArcBlock` / `1` is
698
+ * accepted (absent or spelled out), anything else throws because the chain rejects it (#1715)
699
+ * @returns {Promise<{ tx: object, signature: string, extra: string }>} pass as
700
+ * `client.sendWithdrawTokenV2Tx({ tx, signature, extra })`
692
701
  */
693
702
  client.buildBridgeWithdrawTx = async ({
694
703
  wallet,
@@ -702,6 +711,11 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
702
711
  domain = {}
703
712
  }) => {
704
713
  if (!bridge) throw new Error('buildBridgeWithdrawTx requires the bridge address');
714
+ // #1715: the chain only accepts the default domain on a bridge withdraw envelope, refuse anything else up front
715
+ const { name: domainName, version: domainVersion } = domain || {};
716
+ if (domainName !== undefined && domainName !== EIP712_ENVELOPE_DEFAULT_DOMAIN.name || domainVersion !== undefined && domainVersion !== EIP712_ENVELOPE_DEFAULT_DOMAIN.version) {
717
+ throw new Error(`Bridge withdraw envelope domain must be ${EIP712_ENVELOPE_DEFAULT_DOMAIN.name} / ${EIP712_ENVELOPE_DEFAULT_DOMAIN.version}`);
718
+ }
705
719
  const recipient = parseStrictEthAddress(ethRecipient);
706
720
  if (!recipient.ok) throw new Error(`Invalid Ethereum recipient: ${recipient.reason}`);
707
721
  const amountStr = String(amount);
@@ -710,16 +724,10 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
710
724
  const tokenAddress = token || get(context, 'token.address');
711
725
  if (!tokenAddress) throw new Error('token is required when the chain has no official token');
712
726
 
713
- const tx = {
714
- itx: {
715
- to: toBridgeReserveAddress(bridge),
716
- tokens: [{ address: tokenAddress, value: amountStr }],
717
- data: { typeUrl: 'fg:x:address', value: ethRecipient }
718
- }
719
- };
727
+ const tx = { itx: { token: { address: tokenAddress, value: amountStr }, to: ethRecipient, rollup: bridge } };
720
728
  if (nonce !== undefined) tx.nonce = nonce;
721
729
  if (chainId) tx.chainId = chainId;
722
- const { object, buffer } = await client.encodeTransferV2Tx({ tx, wallet });
730
+ const { object, buffer } = await client.encodeWithdrawTokenV2Tx({ tx, wallet });
723
731
 
724
732
  if (signing === 'raw') {
725
733
  return { tx: object, signature: await wallet.sign(bytesToHex(buffer)), extra: '' };
@@ -734,8 +742,9 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
734
742
  amount: amountStr,
735
743
  token: tokenAddress,
736
744
  ethRecipient,
737
- domainName: domain.name,
738
- domainVersion: domain.version
745
+ action: 'withdraw_token_v2',
746
+ domainName,
747
+ domainVersion
739
748
  });
740
749
  const signed = await wallet.signEnvelope(buffer, envelopeExtra);
741
750
  return { tx: object, signature: signed.signature, extra: signed.extra };
@@ -753,7 +762,7 @@ const createExtensionMethods = (client, deps = defaultDeps) => {
753
762
  */
754
763
  client.bridgeWithdraw = async (params, extra) => {
755
764
  const { tx, signature, extra: txExtra } = await client.buildBridgeWithdrawTx(omit(params, ['commit']));
756
- return client.sendTransferV2Tx({ tx, signature, commit: !!params.commit, extra: txExtra }, extra);
765
+ return client.sendWithdrawTokenV2Tx({ tx, signature, commit: !!params.commit, extra: txExtra }, extra);
757
766
  };
758
767
 
759
768
  /**