@gala-chain/gala-core-sdk 1.1.0-beta.0 → 1.1.0-beta.2

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/README.md CHANGED
@@ -22,10 +22,13 @@ next to the product SDK, once.
22
22
  ## Quick start
23
23
 
24
24
  ```typescript
25
- import { PrivateKeySigner, serialize, signNative, normalizePrivateKey } from '@gala-chain/gala-core-sdk';
25
+ import { PrivateKeySigner, serialize, signNative, normalizePrivateKey, withDtoOperation } from '@gala-chain/gala-core-sdk';
26
26
 
27
27
  const signer = new PrivateKeySigner(process.env.GALACHAIN_PRIVATE_KEY!);
28
- const signed = await signer.signObject('TransferToken', { quantity: '1', uniqueKey: 'k-1' });
28
+ // The chain refuses a signed DTO without the `dtoOperation` of the call it is sent to, and the field is
29
+ // part of what is signed: set it BEFORE signing (GatewayWrites and the product SDKs already do).
30
+ const dto = withDtoOperation('asset/token-contract', 'TransferToken', { quantity: '1', uniqueKey: 'k-1' });
31
+ const signed = await signer.signObject('TransferToken', dto);
29
32
 
30
33
  // The primitives the signer is built on, byte-identical to @gala-chain/api:
31
34
  const key = normalizePrivateKey(process.env.GALACHAIN_PRIVATE_KEY!);
@@ -38,7 +41,7 @@ key.fill(0);
38
41
  | Area | Exports |
39
42
  | --- | --- |
40
43
  | Signers | `PrivateKeySigner`, `createLazyPrivateKeySigner`, `GalaWalletSigner`, `BrowserWalletSigner`, `signPreSend`, type `GalaChainSigner` |
41
- | Signing | `serialize`, `getPayloadToSign`, `signNative`, `signPersonalMessage`, `personalSignPrefixFor`, `fixpointPrefixFor`, `signChainDto*`, `signRawObject`, `generateEIP712Types`, `buildEIP712Payload`, `hashTypedData`, `signTypedDataPayload` |
44
+ | Signing | `serialize`, `getPayloadToSign`, `signNative`, `signPersonalMessage`, `personalSignPrefixFor`, `fixpointPrefixFor`, `signChainDto*`, `signRawObject`, `generateEIP712Types`, `buildEIP712Payload`, `hashTypedData`, `signTypedDataPayload`; the signed `dtoOperation`: `withDtoOperation`, `dtoOperationOf`, `CHAIN_CONTRACT_OPERATION_PREFIXES`, type `ChainContractPath` |
42
45
  | Transport | `fetchRequestor`, `requestWithTimeout` (`kind: { write: false }` makes a read's timeout and transport failure say `write: false`), `buildRequestHeaders`, `buildQueryString`, `readRetryAfter`, `httpRequestorFromAxios`, `eip1193ProviderFromEthers`, `isFetchTransportFailure`, types `HttpRequestor`, `WireOptions`, `RequestKind` |
43
46
  | Gateway reads | `GatewayReads` (`fetchBalances`, `fetchTokenBalance`, `fetchNftBalances`, `fetchTokenClasses`, `fetchTokenClassesWithSupply`, `fetchTokenClass`, `fetchTokenDetails`, `fetchTokenClassesPage`, `fetchAllTokenClasses`, `fetchNftCollectionAuthorizationsPage`, `fetchAllNftCollectionAuthorizations`, `fetchPublicKey`), `GATEWAY_READ_METHODS`, `GATEWAY_READ_LIMITS`, types `GalaChainTokenBalance`, `GalaChainTokenBalanceSummary`, `GalaChainTokenClass`, `GalaChainTokenDetails`, `GalaChainPage`, `GalaChainPublicKey` |
44
47
  | Gateway writes | `GatewayWrites` (`transferGala`, `transferToken`, `burnTokens`, `lockTokens`, `unlockTokens`), `galaChainUserRef` (a user reference in the one spelling the gateway accepts), `GATEWAY_WRITE_METHODS`, `GATEWAY_WRITE_BATCH_LIMITS`, `GALA_TOKEN_CLASS_KEY`, types `GatewayWritesOptions`, `GatewayWriteReceipt`, `TokenNameResolver`, `TokenQuantityInput` |
@@ -96,7 +99,9 @@ bridge-out request was accepted the tokens may be locked: a later failure is
96
99
  `BRIDGE_OUT_OUTCOME_UNKNOWN` (with the step, the bridge request id and the `uniqueKey`), and a
97
100
  deposit that may have been sent is `BRIDGE_DEPOSIT_OUTCOME_UNKNOWN`; neither is ever retried by the
98
101
  SDK. `WrapService` wraps a token from its own channel to the asset channel (MUSIC to GMUSIC) and
99
- back, signed natively by a `GalaChainSigner`.
102
+ back, signed natively by a `GalaChainSigner`. The bridge-out request signs the bridge contract's
103
+ `dtoOperation` (`asset-channel_bridge-chaincode_BridgeContract:RequestTokenBridgeOut`) as a field of its
104
+ EIP-712 `GalaTransaction` type, so the signature covers it; `buildBridgeOutPayload` does this.
100
105
 
101
106
  TypeScript: the `./bridge` declarations name `@solana/web3.js` types, whose own declarations import
102
107
  Node's `http` and `https`, so a TypeScript app that imports `./bridge` has `@types/node` (with
@@ -184,6 +189,24 @@ await writes.lockTokens({ tokens: [{ tokenClassKey: 'GALA|Unit|none|none', quant
184
189
  converted.
185
190
  - The signer is handed the bare DTO; the body sent is exactly the DTO plus the signature (a signer
186
191
  that adds or changes a field is refused). A `uniqueKey` is minted per write unless you pass one.
192
+ - Every DTO carries a `dtoOperation` (`<channel>_<chaincode>_<Contract>:<Method>`, for example
193
+ `asset-channel_basic-asset_GalaChainToken:TransferToken`), set before the signer runs because it is
194
+ part of what is signed. The chain refuses a DTO without it ("DTO operation is not provided. Signed
195
+ requests must include dtoOperation.") or with one that is not the call it is sent to ("The dto was
196
+ signed to call X operation, but the current operation is Y"). The URL of a write and its operation
197
+ come from one table (`CHAIN_CONTRACT_OPERATION_PREFIXES`), so they cannot disagree. For a call the
198
+ SDK does not make, use `withDtoOperation(contractPath, method, dto)` (the contract path is the
199
+ route's `<channel>/<contract>`, such as `asset/token-contract`) or `signPreSend(signer,
200
+ contractPath, method, dto)`, which sets it and signs; a DTO that already names a different
201
+ operation is refused as `SIGNING_FAILED`. Never add it after signing: the signature would not
202
+ cover it. Inside a launchpad bundle the operation is the bundler's `BatchSubmit`
203
+ (`asset-channel_basic-asset_Launchpad:BatchSubmit`), not the inner DTO's own method: launchpad-sdk sets it.
204
+ - Typed data (`BrowserWalletSigner` with `{ scheme: 'eip712' }`, and the `./bridge` bridge-out) is signed under
205
+ an EIP-712 domain whose NAME the deployment must accept: the stage chain refuses anything but its own
206
+ ("EIP-712 domain name must be GalaChainStage for this deployment."). Pass
207
+ `GALA_CHAIN_ENVIRONMENTS[env].eip712DomainName` (`GalaChainStage` on stage; absent on prod, where a signer
208
+ keeps the name it always signed: `ethereum`, and `GalaConnect` for the bridge) as the signer's `eip712DomainName`;
209
+ `BridgeService` takes the environment's by itself. The chain id stays the wallet's live one.
187
210
  - GalaChain's `Status` is checked before anything else is trusted; a timeout or broken connection is
188
211
  `GATEWAY_WRITE_OUTCOME_UNKNOWN`: the write may have been applied, so check its `uniqueKey` and
189
212
  never resend it under a fresh key.
@@ -222,7 +245,10 @@ const classes = await reads.fetchAllTokenClasses({ collection: 'BONKTactics' },
222
245
  The signing code is pinned byte-for-byte to `@gala-chain/api` and to the Gala Wallet's and the
223
246
  gSwap web app's signing code by the golden vectors in `tests/fixtures/wallet-signing-golden.json`
224
247
  and `tests/fixtures/frontend-signing-golden.json` (`tests/signing-golden.test.ts`). They are never
225
- regenerated to make a test pass.
248
+ regenerated to make a test pass. Those two pin the signing primitives and the web apps' bytes, which
249
+ carry no `dtoOperation`; the SDK's own write and bridge recordings
250
+ (`tests/fixtures/writes-signing-golden.json`, `bridge-out-recorded.json`) pin the signed bodies
251
+ including it.
226
252
 
227
253
  ## React Native
228
254
 
@@ -257,7 +283,7 @@ gzipped, before `ethers` and `@solana/*`) are loaded only by an app that imports
257
283
 
258
284
  ## Status
259
285
 
260
- <!-- docs:var version -->1.1.0-beta.0<!-- /docs:var --> (rc.7: `eth|0x<hex>` is refused as `INVALID_ETH_USER_REF_PREFIX`, not an address form; rc.6: `fetchPublicKey`, MetaMask and Trust Wallet detection, silent `restore()`;
286
+ <!-- docs:var version -->1.1.0-beta.2<!-- /docs:var --> (rc.7: `eth|0x<hex>` is refused as `INVALID_ETH_USER_REF_PREFIX`, not an address form; rc.6: `fetchPublicKey`, MetaMask and Trust Wallet detection, silent `restore()`;
261
287
  rc.5: every Ethereum account a read, write or GalaConnect client sends is
262
288
  `eth|<EIP-55>`, the only spelling the gateway accepts, and `galaChainUserRef` is exported; rc.4:
263
289
  `bridgeableTokenService` is required). In: signing, signers, transport and the error model (rc.0, from gswap-sdk); the
package/dist/bridge.cjs CHANGED
@@ -67,6 +67,17 @@ const GALA_BRIDGE_TYPED_DATA_DOMAIN = {
67
67
  chainId: 1
68
68
  };
69
69
  /**
70
+ * The bridge-out typed data domain for a deployment: {@link GALA_BRIDGE_TYPED_DATA_DOMAIN}, or the
71
+ * same domain under the name the deployment's chain requires
72
+ * (`GALA_CHAIN_ENVIRONMENTS[env].eip712DomainName`, `GalaChainStage` on stage).
73
+ */
74
+ function galaBridgeTypedDataDomain(name) {
75
+ return name === void 0 ? GALA_BRIDGE_TYPED_DATA_DOMAIN : {
76
+ name,
77
+ chainId: 1
78
+ };
79
+ }
80
+ /**
70
81
  * Common GalaTransaction fields (identical in both schemas)
71
82
  */
72
83
  const GALA_TRANSACTION_TYPE = [
@@ -2224,10 +2235,10 @@ const BRIDGE_OUT_METHOD = "RequestTokenBridgeOut";
2224
2235
  * `SIGNING_FAILED` (`user_rejected` or `failed`, the original as `cause`), and so is a signature
2225
2236
  * that is not a string: nothing was sent, so a retry is safe.
2226
2237
  */
2227
- async function signBridgeOut(wallet, types, message) {
2238
+ async function signBridgeOut(wallet, domain, types, message) {
2228
2239
  let signature;
2229
2240
  try {
2230
- signature = await wallet.signTypedData(GALA_BRIDGE_TYPED_DATA_DOMAIN, types, message);
2241
+ signature = await wallet.signTypedData(domain, types, message);
2231
2242
  } catch (cause) {
2232
2243
  if ((0, _gala_chain_gala_core_sdk.isGalaError)(cause)) throw cause;
2233
2244
  throw new _gala_chain_gala_core_sdk.GalaSDKError("SIGNING_FAILED", {
@@ -2285,9 +2296,10 @@ async function buildBridgeOutPayload(dto, config) {
2285
2296
  uniqueKey
2286
2297
  };
2287
2298
  const typedDataTypes = getGalaBridgeTypedDataTypes(hasCrossRate);
2288
- const signature = await signBridgeOut(wallet, typedDataTypes, message);
2299
+ const domain = galaBridgeTypedDataDomain(config.eip712DomainName);
2300
+ const signature = await signBridgeOut(wallet, domain, typedDataTypes, message);
2289
2301
  const prefix = `\u0019Ethereum Signed Message:\n${(0, _gala_chain_gala_core_sdk.serialize)({
2290
- domain: GALA_BRIDGE_TYPED_DATA_DOMAIN,
2302
+ domain,
2291
2303
  message,
2292
2304
  primaryType: "GalaTransaction",
2293
2305
  types: typedDataTypes
@@ -2297,7 +2309,7 @@ async function buildBridgeOutPayload(dto, config) {
2297
2309
  signature,
2298
2310
  prefix,
2299
2311
  types: typedDataTypes,
2300
- domain: GALA_BRIDGE_TYPED_DATA_DOMAIN
2312
+ domain
2301
2313
  };
2302
2314
  }
2303
2315
 
@@ -2362,7 +2374,7 @@ async function completeBridgeOut(galaConnectClient, requestResponse, uniqueKey,
2362
2374
  */
2363
2375
  async function executeBridgeOutFlow(params, config) {
2364
2376
  const { amount, recipientAddress, tokenSymbol: resolvedSymbol } = params;
2365
- const { galaConnectClient, tokenMetadataResolver, ethereumWallet, destinationChainId, destinationChain, validateRecipientAddress } = config;
2377
+ const { galaConnectClient, tokenMetadataResolver, ethereumWallet, eip712DomainName, destinationChainId, destinationChain, validateRecipientAddress } = config;
2366
2378
  const tokenSymbol = assertTokenSymbolResolved(resolvedSymbol);
2367
2379
  validateBridgeAmount(amount);
2368
2380
  validateRecipientAddress(recipientAddress, "recipient");
@@ -2378,7 +2390,10 @@ async function executeBridgeOutFlow(params, config) {
2378
2390
  recipientAddress,
2379
2391
  tokenDescriptor: metadata.descriptor
2380
2392
  });
2381
- const signedPayload = await buildBridgeOutPayload(dtoPayload, { wallet: ethereumWallet });
2393
+ const signedPayload = await buildBridgeOutPayload(dtoPayload, {
2394
+ wallet: ethereumWallet,
2395
+ eip712DomainName
2396
+ });
2382
2397
  const requestResponse = await galaConnectClient.requestBridgeOut(signedPayload);
2383
2398
  assertRequestAccepted("RequestTokenBridgeOut", requestResponse, signedPayload.uniqueKey);
2384
2399
  const transactionHash = await completeBridgeOut(galaConnectClient, requestResponse, signedPayload.uniqueKey, "asset", extractBridgeOutTransactionHash);
@@ -2845,6 +2860,7 @@ var EthereumBridgeStrategy = class EthereumBridgeStrategy extends ConfiguredBrid
2845
2860
  ethereumBridgeContract;
2846
2861
  ethereumWalletAddress;
2847
2862
  tokenMetadataResolver;
2863
+ eip712DomainName;
2848
2864
  /**
2849
2865
  * Creates a new Ethereum bridge strategy.
2850
2866
  *
@@ -2863,6 +2879,7 @@ var EthereumBridgeStrategy = class EthereumBridgeStrategy extends ConfiguredBrid
2863
2879
  const rpcUrl = assertHttpUrlOption(config.ethereumRpcUrl ?? "https://ethereum.publicnode.com", "ethereumRpcUrl");
2864
2880
  if (config.ethereumWalletAddress !== void 0) assertEthereumAddressOption(config.ethereumWalletAddress, "ethereumWalletAddress");
2865
2881
  this.ethereumProvider = new ethers.JsonRpcProvider(rpcUrl);
2882
+ this.eip712DomainName = config.eip712DomainName;
2866
2883
  this.ethereumWallet = new ethers.Wallet(config.ethereumPrivateKey, this.ethereumProvider);
2867
2884
  this.ethereumWalletAddress = config.ethereumWalletAddress ?? this.ethereumWallet.address;
2868
2885
  this.ethereumBridgeContract = ethereumBridgeContract;
@@ -2888,6 +2905,7 @@ var EthereumBridgeStrategy = class EthereumBridgeStrategy extends ConfiguredBrid
2888
2905
  galaConnectClient: this.galaConnectClient,
2889
2906
  tokenMetadataResolver: this.tokenMetadataResolver,
2890
2907
  ethereumWallet: this.ethereumWallet,
2908
+ eip712DomainName: this.eip712DomainName,
2891
2909
  destinationChainId: CHAIN_IDS.ETHEREUM,
2892
2910
  destinationChain: "Ethereum",
2893
2911
  validateRecipientAddress: validateEthereumAddressOrThrow
@@ -3249,6 +3267,7 @@ var SolanaBridgeStrategy = class extends ConfiguredBridgeStrategy {
3249
3267
  network = "Solana";
3250
3268
  galaChainWalletAddress;
3251
3269
  ethereumWallet;
3270
+ eip712DomainName;
3252
3271
  solanaConnection;
3253
3272
  solanaKeypair;
3254
3273
  solanaBridgeProgramId;
@@ -3272,6 +3291,7 @@ var SolanaBridgeStrategy = class extends ConfiguredBridgeStrategy {
3272
3291
  this.galaChainWalletAddress = config.galaChainWalletAddress;
3273
3292
  validateEthereumPrivateKeyFormat(config.ethereumPrivateKey);
3274
3293
  this.ethereumWallet = new ethers.Wallet(config.ethereumPrivateKey);
3294
+ this.eip712DomainName = config.eip712DomainName;
3275
3295
  const rpcUrl = assertHttpUrlOption(config.solanaRpcUrl ?? "https://api.mainnet-beta.solana.com", "solanaRpcUrl");
3276
3296
  this.solanaConnection = config.solanaConnection ?? new _solana_web3_js.Connection(rpcUrl, "confirmed");
3277
3297
  this.solanaKeypair = keypairFromBase58(config.solanaPrivateKeyBase58);
@@ -3301,6 +3321,7 @@ var SolanaBridgeStrategy = class extends ConfiguredBridgeStrategy {
3301
3321
  galaConnectClient: this.galaConnectClient,
3302
3322
  tokenMetadataResolver: this.tokenMetadataResolver,
3303
3323
  ethereumWallet: this.ethereumWallet,
3324
+ eip712DomainName: this.eip712DomainName,
3304
3325
  destinationChainId: CHAIN_IDS.SOLANA,
3305
3326
  destinationChain: "Solana",
3306
3327
  validateRecipientAddress: validateSolanaAddressOrThrow
@@ -3850,6 +3871,7 @@ var BridgeService = class {
3850
3871
  });
3851
3872
  this.bridgeableTokenService = config.bridgeableTokenService;
3852
3873
  const sleep = config.sleep !== void 0 ? { sleep: config.sleep } : {};
3874
+ const eip712DomainName = config.eip712DomainName ?? urls.eip712DomainName;
3853
3875
  this.strategies = /* @__PURE__ */ new Map([["Ethereum", new EthereumBridgeStrategy({
3854
3876
  galaConnectClient: this.galaConnectClient,
3855
3877
  galaChainWalletAddress,
@@ -3857,6 +3879,7 @@ var BridgeService = class {
3857
3879
  ethereumRpcUrl: config.ethereumRpcUrl ?? rpcUrls.ethereum,
3858
3880
  ethereumBridgeContract: config.ethereumBridgeContract ?? getEthereumBridgeContractByEnvironment(this.environment),
3859
3881
  tokenConfigs: getEthereumTokensByEnvironment(this.environment),
3882
+ eip712DomainName,
3860
3883
  ...sleep
3861
3884
  })]]);
3862
3885
  if (config.solanaPrivateKey !== void 0 && config.solanaPrivateKey !== "") this.strategies.set("Solana", new SolanaBridgeStrategy({
@@ -3867,6 +3890,7 @@ var BridgeService = class {
3867
3890
  solanaRpcUrl: config.solanaRpcUrl ?? rpcUrls.solana,
3868
3891
  solanaBridgeProgram: config.solanaBridgeProgram ?? "AaE4dTnL75XqgUJpdxBKg6vS9sTJgBPJwBQRVhD29WwS",
3869
3892
  tokenConfigs: DEFAULT_SOLANA_TOKENS,
3893
+ eip712DomainName,
3870
3894
  ...config.solanaConnection !== void 0 && { solanaConnection: config.solanaConnection },
3871
3895
  ...sleep
3872
3896
  }));
@@ -4892,6 +4916,7 @@ exports.createSolanaWallet = createSolanaWallet;
4892
4916
  exports.formatTokenAmount = formatTokenAmount;
4893
4917
  exports.formatTokenDescriptor = formatTokenDescriptor;
4894
4918
  exports.fromBaseUnits = fromBaseUnits;
4919
+ exports.galaBridgeTypedDataDomain = galaBridgeTypedDataDomain;
4895
4920
  exports.getEthereumBridgeContractByEnvironment = getEthereumBridgeContractByEnvironment;
4896
4921
  exports.getEthereumTokenConfig = getEthereumTokenConfig;
4897
4922
  exports.getEthereumTokensByEnvironment = getEthereumTokensByEnvironment;