Browse Source

clients/js: fixes hardcoded Near deployer implicit account id. (#3248)

* clients/js: fixes hardcoded Near deployer implicit account id.

* clients/js: remove near deployerAccount

---------

Co-authored-by: Evan Gray <battledingo@gmail.com>
scnale 1 năm trước cách đây
mục cha
commit
9634d59035

+ 1 - 0
clients/js/Makefile

@@ -25,6 +25,7 @@ test: build
 # This first invocation will set up the initial config, so that the warning
 # doesn't show up in the tests
 	npm run check
+	npm test
 	node build/main.js --version > /dev/null
 	./run_parse_tests
 

+ 12 - 0
clients/js/jest.config.json

@@ -0,0 +1,12 @@
+{
+  "moduleNameMapper": {
+    "^@certusone/(.*)/lib/esm/(.*)$": "@certusone/$1/lib/cjs/$2"
+  },
+  "transform": {
+    "^.+\\.(t|j)sx?$": "ts-jest"
+  },
+  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
+  "testPathIgnorePatterns": ["__tests__/utils"],
+  "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
+  "testTimeout": 60000
+}

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 879 - 207
clients/js/package-lock.json


+ 4 - 1
clients/js/package.json

@@ -19,7 +19,7 @@
     "check": "tsc --noEmit",
     "docs": "npx tsx src/doc.ts",
     "prepublishOnly": "npm run check",
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "jest"
   },
   "author": "Wormhole Contributors",
   "license": "Apache-2.0",
@@ -64,9 +64,12 @@
     "@truffle/hdwallet-provider": "^2.0.15",
     "@types/bn.js": "^5.1.0",
     "@types/bs58": "^4.0.1",
+    "@types/jest": "^29.5.12",
     "@types/node-fetch": "^2.6.3",
     "@types/yargs": "^17.0.24",
     "copy-dir": "^1.3.0",
+    "jest": "^29.7.0",
+    "ts-jest": "^29.1.2",
     "typescript": "^4.6"
   }
 }

+ 0 - 4
clients/js/src/consts/networks.ts

@@ -92,8 +92,6 @@ const MAINNET = {
     rpc: "https://rpc.mainnet.near.org",
     key: getEnvVar("NEAR_KEY"),
     networkId: "mainnet",
-    deployerAccount:
-      "85957f38de1768d6db9eab29bee9dd2a01462aff9c8d83daefb9bcd2506c32d2",
   },
   injective: {
     rpc: "http://sentry0.injective.network:26657",
@@ -332,7 +330,6 @@ const TESTNET = {
     rpc: "https://rpc.testnet.near.org",
     key: getEnvVar("NEAR_KEY_TESTNET"),
     networkId: "testnet",
-    deployerAccount: "wormhole.testnet",
   },
   injective: {
     rpc: "https://k8s.testnet.tm.injective.network:443",
@@ -561,7 +558,6 @@ const DEVNET = {
     rpc: undefined,
     key: undefined,
     networkId: "sandbox",
-    deployerAccount: "test.near",
   },
   injective: {
     rpc: undefined,

+ 20 - 0
clients/js/src/near.test.ts

@@ -0,0 +1,20 @@
+import { parseSeedPhrase } from "near-seed-phrase";
+import { test } from "@jest/globals";
+import { KeyPair } from "near-api-js";
+import { keyPairToImplicitAccount } from "./near";
+import base58 from "bs58";
+
+test("keyPairToImplicitAccount", () => {
+  // seed phrase from /near/devnet_deploy.ts
+  const parsed = parseSeedPhrase(
+    "weather opinion slam purpose access artefact word orbit matter rice poem badge"
+  );
+  const expectedImplicitAccount = base58
+    .decode(parsed.publicKey.split(":")[1])
+    .toString("hex");
+
+  const keyPair = KeyPair.fromString(parsed.secretKey);
+  const implicitAccount = keyPairToImplicitAccount(keyPair);
+  expect(implicitAccount.length).toEqual(64);
+  expect(implicitAccount).toEqual(expectedImplicitAccount);
+});

+ 12 - 4
clients/js/src/near.ts

@@ -14,12 +14,16 @@ import {
 } from "@certusone/wormhole-sdk/lib/esm/token_bridge/transfer";
 import { tryNativeToUint8Array } from "@certusone/wormhole-sdk/lib/esm/utils";
 
+export function keyPairToImplicitAccount(keyPair: KeyPair): string {
+  return Buffer.from(keyPair.getPublicKey().data).toString("hex");
+}
+
 export const execute_near = async (
   payload: Payload,
   vaa: string,
   network: Network
 ): Promise<void> => {
-  const { rpc, key, networkId, deployerAccount } = NETWORKS[network].near;
+  const { rpc, key, networkId } = NETWORKS[network].near;
   if (!key) {
     throw Error(`No ${network} key defined for NEAR`);
   }
@@ -113,8 +117,10 @@ export const execute_near = async (
       impossible(payload);
   }
 
+  const keyPair = KeyPair.fromString(key);
+  const deployerAccount = keyPairToImplicitAccount(keyPair);
   const keyStore = new InMemoryKeyStore();
-  keyStore.setKey(networkId, deployerAccount, KeyPair.fromString(key));
+  keyStore.setKey(networkId, deployerAccount, keyPair);
   const near = await connect({
     keyStore,
     networkId,
@@ -159,7 +165,7 @@ export async function transferNear(
   network: Network,
   rpc: string
 ) {
-  const { key, networkId, deployerAccount } = NETWORKS[network].near;
+  const { key, networkId } = NETWORKS[network].near;
   if (!key) {
     throw Error(`No ${network} key defined for NEAR`);
   }
@@ -170,8 +176,10 @@ export async function transferNear(
   if (token_bridge === undefined) {
     throw Error(`Unknown token bridge contract on ${network} for NEAR`);
   }
+  const keyPair = KeyPair.fromString(key);
+  const deployerAccount = keyPairToImplicitAccount(keyPair);
   const keyStore = new InMemoryKeyStore();
-  keyStore.setKey(networkId, deployerAccount, KeyPair.fromString(key));
+  keyStore.setKey(networkId, deployerAccount, keyPair);
   const near = await connect({
     keyStore,
     networkId,

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác