@rabby-wallet/gnosis-sdk 1.3.7 → 1.3.9-alpha.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/api.js CHANGED
@@ -58,6 +58,10 @@ const HOST_MAP = {
58
58
  * linea
59
59
  */
60
60
  "59144": "https://safe-transaction-linea.safe.global/api/v1",
61
+ /**
62
+ * X Layer
63
+ */
64
+ "196": "https://safe-transaction-xlayer.safe.global/api/v1",
61
65
  };
62
66
  export default class RequestProvider {
63
67
  constructor(networkId, adapter) {
package/dist/index.d.ts CHANGED
@@ -4,8 +4,11 @@ import { providers } from "ethers";
4
4
  import { SafeTransactionDataPartial, SafeSignature } from "@gnosis.pm/safe-core-sdk-types";
5
5
  import { TransactionResult, TransactionOptions } from "@gnosis.pm/safe-core-sdk/dist/src/utils/transactions/types";
6
6
  import SafeTransaction from "@gnosis.pm/safe-core-sdk/dist/src/utils/transactions/SafeTransaction";
7
+ import SafeMessage from "@safe-global/protocol-kit/dist/src/utils/messages/SafeMessage";
7
8
  import RequestProvider, { SafeInfo } from "./api";
8
9
  import { AxiosAdapter } from "axios";
10
+ import SafeApiKit from "@safe-global/api-kit";
11
+ import { SafeClientResult } from "@safe-global/sdk-starter-kit";
9
12
  declare class Safe {
10
13
  contract: Contract;
11
14
  safeAddress: string;
@@ -15,6 +18,7 @@ declare class Safe {
15
18
  safeInfo: SafeInfo | null;
16
19
  request: RequestProvider;
17
20
  network: string;
21
+ apiKit: SafeApiKit;
18
22
  static adapter: AxiosAdapter;
19
23
  constructor(safeAddress: string, version: string, provider: providers.Web3Provider, network?: string);
20
24
  /**
@@ -54,6 +58,25 @@ declare class Safe {
54
58
  confirmTransaction(safeTransaction: SafeTransaction): Promise<void>;
55
59
  getBalance(): Promise<BigNumber>;
56
60
  executeTransaction(safeTransaction: SafeTransaction, options?: TransactionOptions): Promise<TransactionResult>;
61
+ /**
62
+ * Call the CompatibilityFallbackHandler getMessageHash method
63
+ *
64
+ * @param messageHash The hash of the message
65
+ * @returns Returns the Safe message hash to be signed
66
+ * @link https://github.com/safe-global/safe-contracts/blob/8ffae95faa815acf86ec8b50021ebe9f96abde10/contracts/handler/CompatibilityFallbackHandler.sol#L26-L28
67
+ */
68
+ getSafeMessageHash: (messageHash: string) => Promise<string>;
69
+ /**
70
+ * Add a new off-chain message using the Transaction service
71
+ * - If the threshold > 1, remember to confirmMessage() after sendMessage()
72
+ * - If the threshold = 1, then the message is confirmed and valid immediately
73
+ *
74
+ * @param {SafeMessage} safeMessage The message
75
+ * @returns {Promise<SafeClientResult>} The SafeClientResult
76
+ */
77
+ addMessage({ safeMessage }: {
78
+ safeMessage: SafeMessage;
79
+ }): Promise<SafeClientResult>;
57
80
  }
58
81
  export default Safe;
59
82
  export type BasicSafeInfo = Awaited<ReturnType<Safe["getBasicSafeInfo"]>>;
package/dist/index.js CHANGED
@@ -4,8 +4,15 @@ import BN from "bignumber.js";
4
4
  import { getSafeSingletonDeployment } from "@safe-global/safe-deployments";
5
5
  import { toChecksumAddress } from "web3-utils";
6
6
  import SafeTransaction from "@gnosis.pm/safe-core-sdk/dist/src/utils/transactions/SafeTransaction";
7
+ import { hashSafeMessage, } from "@safe-global/protocol-kit";
7
8
  import RequestProvider from "./api";
8
9
  import { standardizeSafeTransactionData, sameString, generateSignature, generatePreValidatedSignature, estimateGasForTransactionExecution, } from "./utils";
10
+ import { calculateSafeMessageHash, } from "@safe-global/protocol-kit/dist/src/utils";
11
+ import SafeApiKit from "@safe-global/api-kit";
12
+ import { SafeClientTxStatus } from "@safe-global/sdk-starter-kit/dist/src/constants";
13
+ import { createSafeClientResult } from "@safe-global/sdk-starter-kit/dist/src/utils";
14
+ const EQ_OR_GT_1_4_1 = ">=1.4.1";
15
+ const EQ_OR_GT_1_3_0 = ">=1.3.0";
9
16
  class Safe {
10
17
  constructor(safeAddress, version, provider, network = "1") {
11
18
  this.owners = [];
@@ -24,6 +31,22 @@ class Safe {
24
31
  owners,
25
32
  };
26
33
  };
34
+ /**
35
+ * Call the CompatibilityFallbackHandler getMessageHash method
36
+ *
37
+ * @param messageHash The hash of the message
38
+ * @returns Returns the Safe message hash to be signed
39
+ * @link https://github.com/safe-global/safe-contracts/blob/8ffae95faa815acf86ec8b50021ebe9f96abde10/contracts/handler/CompatibilityFallbackHandler.sol#L26-L28
40
+ */
41
+ this.getSafeMessageHash = async (messageHash) => {
42
+ if (this.contract.getMessageHash) {
43
+ return this.contract.getMessageHash(messageHash);
44
+ }
45
+ const safeAddress = this.safeAddress;
46
+ const safeVersion = this.version;
47
+ const chainId = BigInt(this.network);
48
+ return calculateSafeMessageHash(toChecksumAddress(safeAddress), messageHash, safeVersion, chainId);
49
+ };
27
50
  const contract = getSafeSingletonDeployment({
28
51
  version,
29
52
  network,
@@ -37,6 +60,9 @@ class Safe {
37
60
  this.safeAddress = safeAddress;
38
61
  this.network = network;
39
62
  this.request = new RequestProvider(network, Safe.adapter);
63
+ this.apiKit = new SafeApiKit({
64
+ chainId: BigInt(network),
65
+ });
40
66
  // this.init();
41
67
  }
42
68
  /**
@@ -204,5 +230,35 @@ class Safe {
204
230
  const txResponse = await contract.execTransaction(safeTransaction.data.to, safeTransaction.data.value, safeTransaction.data.data, safeTransaction.data.operation, safeTransaction.data.safeTxGas, safeTransaction.data.baseGas, safeTransaction.data.gasPrice, safeTransaction.data.gasToken, safeTransaction.data.refundReceiver, safeTransaction.encodedSignatures(), executionOptions);
205
231
  return txResponse;
206
232
  }
233
+ /**
234
+ * Add a new off-chain message using the Transaction service
235
+ * - If the threshold > 1, remember to confirmMessage() after sendMessage()
236
+ * - If the threshold = 1, then the message is confirmed and valid immediately
237
+ *
238
+ * @param {SafeMessage} safeMessage The message
239
+ * @returns {Promise<SafeClientResult>} The SafeClientResult
240
+ */
241
+ async addMessage({ safeMessage }) {
242
+ const safeAddress = this.safeAddress;
243
+ const threshold = await this.getThreshold();
244
+ const messageHash = await this.getSafeMessageHash(hashSafeMessage(safeMessage.data));
245
+ try {
246
+ await this.apiKit.addMessage(safeAddress, {
247
+ message: safeMessage.data,
248
+ signature: safeMessage.encodedSignatures(),
249
+ });
250
+ }
251
+ catch (error) {
252
+ throw new Error("Could not add a new off-chain message to the Safe account");
253
+ }
254
+ const message = await this.apiKit.getMessage(messageHash);
255
+ return createSafeClientResult({
256
+ safeAddress: this.safeAddress,
257
+ status: message.confirmations.length === threshold
258
+ ? SafeClientTxStatus.MESSAGE_CONFIRMED
259
+ : SafeClientTxStatus.MESSAGE_PENDING_SIGNATURES,
260
+ messageHash,
261
+ });
262
+ }
207
263
  }
208
264
  export default Safe;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/gnosis-sdk",
3
- "version": "1.3.7",
3
+ "version": "1.3.9-alpha.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -16,13 +16,17 @@
16
16
  "@ethersproject/solidity": "^5.5.0",
17
17
  "@gnosis.pm/safe-core-sdk": "^1.0.0",
18
18
  "@gnosis.pm/safe-core-sdk-types": "^0.1.1",
19
+ "@safe-global/api-kit": "^2.5.2",
20
+ "@safe-global/protocol-kit": "^5.0.2",
19
21
  "@safe-global/safe-deployments": "^1.37.2",
22
+ "@safe-global/sdk-starter-kit": "^1.0.2",
23
+ "@safe-global/types-kit": "^1.0.0",
20
24
  "axios": "^0.24.0",
21
25
  "bignumber.js": "^9.0.2",
22
26
  "ethereumjs-util": "^7.1.3",
23
27
  "ethers": "^5.5.1",
24
28
  "semver": "^7.5.4",
25
- "typescript": "^4.5.0",
29
+ "typescript": "5.6.2",
26
30
  "web3-core": "^1.6.0"
27
31
  },
28
32
  "devDependencies": {
package/src/api.ts CHANGED
@@ -104,6 +104,10 @@ const HOST_MAP = {
104
104
  * linea
105
105
  */
106
106
  "59144": "https://safe-transaction-linea.safe.global/api/v1",
107
+ /**
108
+ * X Layer
109
+ */
110
+ "196": "https://safe-transaction-xlayer.safe.global/api/v1",
107
111
  };
108
112
 
109
113
  export default class RequestProvider {
package/src/index.ts CHANGED
@@ -13,6 +13,16 @@ import {
13
13
  TransactionOptions,
14
14
  } from "@gnosis.pm/safe-core-sdk/dist/src/utils/transactions/types";
15
15
  import SafeTransaction from "@gnosis.pm/safe-core-sdk/dist/src/utils/transactions/SafeTransaction";
16
+ import SafeTransactionNew from "@safe-global/protocol-kit/dist/src/utils/transactions/SafeTransaction";
17
+ import SafeMessage from "@safe-global/protocol-kit/dist/src/utils/messages/SafeMessage";
18
+ import SafeProtocolKit, {
19
+ generateEIP712Signature,
20
+ hashSafeMessage,
21
+ preimageSafeMessageHash,
22
+ SafeProvider,
23
+ SigningMethod,
24
+ SigningMethodType,
25
+ } from "@safe-global/protocol-kit";
16
26
  import RequestProvider, { SafeInfo } from "./api";
17
27
  import {
18
28
  standardizeSafeTransactionData,
@@ -22,6 +32,31 @@ import {
22
32
  estimateGasForTransactionExecution,
23
33
  } from "./utils";
24
34
  import { AxiosAdapter } from "axios";
35
+ import semverSatisfies from "semver/functions/satisfies";
36
+ import {
37
+ EIP712TypedData,
38
+ SafeEIP712Args,
39
+ SafeSignature as SafeSignatureNew,
40
+ } from "@safe-global/types-kit";
41
+ import {
42
+ calculateSafeMessageHash,
43
+ EthSafeSignature,
44
+ generateSignature as generateSignatureNew,
45
+ hasSafeFeature,
46
+ SAFE_FEATURES,
47
+ } from "@safe-global/protocol-kit/dist/src/utils";
48
+ import SafeApiKit, {
49
+ EIP712TypedData as ApiKitEIP712TypedData,
50
+ } from "@safe-global/api-kit";
51
+ import { SafeClientTxStatus } from "@safe-global/sdk-starter-kit/dist/src/constants";
52
+ import { createSafeClientResult } from "@safe-global/sdk-starter-kit/dist/src/utils";
53
+ import {
54
+ SafeClientResult,
55
+ SendOffChainMessageProps,
56
+ } from "@safe-global/sdk-starter-kit";
57
+
58
+ const EQ_OR_GT_1_4_1 = ">=1.4.1";
59
+ const EQ_OR_GT_1_3_0 = ">=1.3.0";
25
60
 
26
61
  class Safe {
27
62
  contract: Contract;
@@ -32,6 +67,7 @@ class Safe {
32
67
  safeInfo: SafeInfo | null = null;
33
68
  request: RequestProvider;
34
69
  network: string;
70
+ apiKit: SafeApiKit;
35
71
 
36
72
  static adapter: AxiosAdapter;
37
73
 
@@ -54,6 +90,10 @@ class Safe {
54
90
  this.safeAddress = safeAddress;
55
91
  this.network = network;
56
92
  this.request = new RequestProvider(network, Safe.adapter);
93
+ this.apiKit = new SafeApiKit({
94
+ chainId: BigInt(network),
95
+ });
96
+
57
97
  // this.init();
58
98
  }
59
99
 
@@ -332,6 +372,68 @@ class Safe {
332
372
 
333
373
  return txResponse;
334
374
  }
375
+
376
+ /**
377
+ * Call the CompatibilityFallbackHandler getMessageHash method
378
+ *
379
+ * @param messageHash The hash of the message
380
+ * @returns Returns the Safe message hash to be signed
381
+ * @link https://github.com/safe-global/safe-contracts/blob/8ffae95faa815acf86ec8b50021ebe9f96abde10/contracts/handler/CompatibilityFallbackHandler.sol#L26-L28
382
+ */
383
+ getSafeMessageHash = async (messageHash: string): Promise<string> => {
384
+ if (this.contract.getMessageHash) {
385
+ return this.contract.getMessageHash(messageHash);
386
+ }
387
+
388
+ const safeAddress = this.safeAddress;
389
+ const safeVersion = this.version;
390
+ const chainId = BigInt(this.network);
391
+
392
+ return calculateSafeMessageHash(
393
+ toChecksumAddress(safeAddress),
394
+ messageHash,
395
+ safeVersion,
396
+ chainId
397
+ );
398
+ };
399
+
400
+ /**
401
+ * Add a new off-chain message using the Transaction service
402
+ * - If the threshold > 1, remember to confirmMessage() after sendMessage()
403
+ * - If the threshold = 1, then the message is confirmed and valid immediately
404
+ *
405
+ * @param {SafeMessage} safeMessage The message
406
+ * @returns {Promise<SafeClientResult>} The SafeClientResult
407
+ */
408
+ async addMessage({ safeMessage }: { safeMessage: SafeMessage }) {
409
+ const safeAddress = this.safeAddress;
410
+ const threshold = await this.getThreshold();
411
+ const messageHash = await this.getSafeMessageHash(
412
+ hashSafeMessage(safeMessage.data)
413
+ );
414
+
415
+ try {
416
+ await this.apiKit.addMessage(safeAddress, {
417
+ message: safeMessage.data as string | ApiKitEIP712TypedData,
418
+ signature: safeMessage.encodedSignatures(),
419
+ });
420
+ } catch (error) {
421
+ throw new Error(
422
+ "Could not add a new off-chain message to the Safe account"
423
+ );
424
+ }
425
+
426
+ const message = await this.apiKit.getMessage(messageHash);
427
+
428
+ return createSafeClientResult({
429
+ safeAddress: this.safeAddress,
430
+ status:
431
+ message.confirmations.length === threshold
432
+ ? SafeClientTxStatus.MESSAGE_CONFIRMED
433
+ : SafeClientTxStatus.MESSAGE_PENDING_SIGNATURES,
434
+ messageHash,
435
+ });
436
+ }
335
437
  }
336
438
 
337
439
  export default Safe;
package/tsconfig.json CHANGED
@@ -11,9 +11,12 @@
11
11
  "esModuleInterop": true,
12
12
  "allowSyntheticDefaultImports": true,
13
13
  "allowJs": true,
14
- "plugins": [{ "transform": "typescript-transform-paths", "afterDeclarations": true }],
14
+ "plugins": [
15
+ { "transform": "typescript-transform-paths", "afterDeclarations": true }
16
+ ],
15
17
  "outDir": "./dist",
16
- "declaration": true
18
+ "declaration": true,
19
+ "skipLibCheck": true
17
20
  },
18
21
  "exclude": ["./node_modules"],
19
22
  "include": ["src", "__tests__"]