Skip to content

Commit 2b994a4

Browse files
authored
Bun Update (Near API JS) (#181)
Standard update, with some modifications related to near-api-js deprecations.
1 parent 69895eb commit 2b994a4

6 files changed

Lines changed: 97 additions & 131 deletions

File tree

bun.lock

Lines changed: 60 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
"node": ">=20.0.0"
3737
},
3838
"dependencies": {
39-
"@reown/walletkit": "^1.2.4",
39+
"@reown/walletkit": "^1.2.10",
4040
"elliptic": "^6.6.1",
4141
"js-sha3": "^0.9.3",
42-
"near-api-js": "^6.2.4",
43-
"viem": "^2.29.3"
42+
"near-api-js": "^6.2.6",
43+
"viem": "^2.36.0"
4444
},
4545
"devDependencies": {
4646
"@types/elliptic": "^6.4.18",
4747
"@types/jest": "^30.0.0",
48-
"@types/node": "^24.2.0",
49-
"@typescript-eslint/eslint-plugin": "^8.21.0",
50-
"@typescript-eslint/parser": "^8.21.0",
48+
"@types/node": "^24.3.0",
49+
"@typescript-eslint/eslint-plugin": "^8.41.0",
50+
"@typescript-eslint/parser": "^8.41.0",
5151
"dotenv": "^17.2.1",
52-
"eslint": "^9.19.0",
53-
"ethers": "^6.13.3",
54-
"opensea-js": "^7.1.12",
55-
"prettier": "^3.5.3",
56-
"ts-jest": "^29.2.3",
57-
"tsx": "^4.16.2",
58-
"typescript": "^5.7.3"
52+
"eslint": "^9.34.0",
53+
"ethers": "^6.15.0",
54+
"opensea-js": "^7.2.1",
55+
"prettier": "^3.6.2",
56+
"ts-jest": "^29.4.1",
57+
"tsx": "^4.20.5",
58+
"typescript": "^5.9.2"
5959
}
6060
}

src/mpcContract.ts

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Contract, Account, transactions } from "near-api-js";
1+
import { Account, transactions } from "near-api-js";
22
import { Address, Signature } from "viem";
33
import {
44
deriveChildPublicKey,
@@ -7,7 +7,7 @@ import {
77
uncompressedHexPointToEvmAddress,
88
} from "./utils";
99
import { TGAS } from "./chains";
10-
import { MPCSignature, FunctionCallTransaction, SignArgs } from "./types";
10+
import { FunctionCallTransaction, SignArgs } from "./types";
1111
import { FinalExecutionOutcome } from "near-api-js/lib/providers";
1212

1313
/**
@@ -26,25 +26,14 @@ export interface ChangeMethodArgs<T> {
2626
amount: string;
2727
}
2828

29-
/** Interface extending the base NEAR Contract with MPC-specific methods */
30-
interface MpcContractInterface extends Contract {
31-
/** Returns the public key */
32-
public_key: () => Promise<string>;
33-
/** Returns required deposit based on current request queue */
34-
experimental_signature_deposit: () => Promise<number>;
35-
/** Signs a request using the MPC contract */
36-
sign: (
37-
args: ChangeMethodArgs<{ request: SignArgs }>
38-
) => Promise<MPCSignature>;
39-
}
40-
4129
/**
4230
* High-level interface for the Near MPC-Recovery Contract
4331
* located in: https://github.com/near/mpc-recovery
4432
*/
4533
export class MpcContract implements IMpcContract {
4634
rootPublicKey: string | undefined;
47-
contract: MpcContractInterface;
35+
contractId: string;
36+
// contract: MpcContractInterface;
4837
connectedAccount: Account;
4938

5039
/**
@@ -57,12 +46,7 @@ export class MpcContract implements IMpcContract {
5746
constructor(account: Account, contractId: string, rootPublicKey?: string) {
5847
this.connectedAccount = account;
5948
this.rootPublicKey = rootPublicKey;
60-
61-
this.contract = new Contract(account.getConnection(), contractId, {
62-
changeMethods: ["sign"],
63-
viewMethods: ["public_key", "experimental_signature_deposit"],
64-
useLocalViewExecution: false,
65-
}) as MpcContractInterface;
49+
this.contractId = contractId;
6650
}
6751

6852
/**
@@ -71,7 +55,7 @@ export class MpcContract implements IMpcContract {
7155
* @returns The contract ID
7256
*/
7357
accountId(): string {
74-
return this.contract.contractId;
58+
return this.contractId;
7559
}
7660

7761
/**
@@ -82,36 +66,22 @@ export class MpcContract implements IMpcContract {
8266
*/
8367
deriveEthAddress = async (derivationPath: string): Promise<Address> => {
8468
if (!this.rootPublicKey) {
85-
this.rootPublicKey = await this.contract.public_key();
69+
this.rootPublicKey = await this.connectedAccount.provider.callFunction(
70+
this.contractId,
71+
"public_key",
72+
{}
73+
);
8674
}
8775

8876
const publicKey = deriveChildPublicKey(
89-
najPublicKeyStrToUncompressedHexPoint(this.rootPublicKey),
77+
najPublicKeyStrToUncompressedHexPoint(this.rootPublicKey!),
9078
this.connectedAccount.accountId,
9179
derivationPath
9280
);
9381

9482
return uncompressedHexPointToEvmAddress(publicKey);
9583
};
9684

97-
/**
98-
* Gets the required deposit for the signature
99-
*
100-
* @returns The required deposit amount as a string
101-
*/
102-
getDeposit = async (): Promise<string> => {
103-
try {
104-
const deposit = await this.contract.experimental_signature_deposit();
105-
return BigInt(
106-
deposit.toLocaleString("fullwide", { useGrouping: false })
107-
).toString();
108-
} catch {
109-
// They are phasing out experimental_signature_deposit.
110-
// required deposit is 1 yocto (see v1.signer-prod.testnet).
111-
return "1";
112-
}
113-
};
114-
11585
/**
11686
* Requests a signature from the MPC contract
11787
*
@@ -141,15 +111,15 @@ export class MpcContract implements IMpcContract {
141111
): Promise<FunctionCallTransaction<{ request: SignArgs }>> {
142112
return {
143113
signerId: this.connectedAccount.accountId,
144-
receiverId: this.contract.contractId,
114+
receiverId: this.contractId,
145115
actions: [
146116
{
147117
type: "FunctionCall",
148118
params: {
149119
methodName: "sign",
150120
args: { request: signArgs },
151121
gas: gasOrDefault(gas),
152-
deposit: await this.getDeposit(),
122+
deposit: "1",
153123
},
154124
},
155125
],
@@ -168,7 +138,7 @@ export class MpcContract implements IMpcContract {
168138
): Promise<FinalExecutionOutcome> {
169139
const account = this.connectedAccount;
170140
const signedTx = await account.createSignedTransaction(
171-
this.contract.contractId,
141+
this.contractId,
172142
transaction.actions.map(({ params: { args, gas, deposit } }) =>
173143
transactions.functionCall("sign", args, BigInt(gas), BigInt(deposit))
174144
)
@@ -195,7 +165,6 @@ export interface IMpcContract {
195165
connectedAccount: Account;
196166
accountId(): string;
197167
deriveEthAddress(derivationPath: string): Promise<Address>;
198-
getDeposit(): Promise<string>;
199168
requestSignature(signArgs: SignArgs, gas?: bigint): Promise<Signature>;
200169
encodeSignatureRequestTx(
201170
signArgs: SignArgs,

src/utils/mock-sign.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ export class MockMpcContract implements IMpcContract {
6767
return this.ethAccount.address;
6868
};
6969

70-
/**
71-
* Returns a mock deposit amount
72-
*
73-
* @returns A constant deposit value of "1"
74-
*/
75-
getDeposit = async (): Promise<string> => {
76-
return "1";
77-
};
78-
7970
/**
8071
* Signs a message using the mock private key
8172
*
@@ -110,7 +101,7 @@ export class MockMpcContract implements IMpcContract {
110101
methodName: "sign",
111102
args: { request: signArgs },
112103
gas: gas ? gas.toString() : "1",
113-
deposit: await this.getDeposit(),
104+
deposit: "1",
114105
},
115106
},
116107
],

src/utils/transaction.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,9 @@ export async function relaySignedTransaction(
129129
const tx = parseTransaction(serializedTransaction);
130130
const network = Network.fromChainId(tx.chainId!);
131131
if (wait) {
132-
return network.client.sendRawTransaction({
133-
serializedTransaction,
134-
});
132+
return network.client.sendRawTransaction({ serializedTransaction });
135133
} else {
136-
network.client.sendRawTransaction({
137-
serializedTransaction,
138-
});
134+
network.client.sendRawTransaction({ serializedTransaction });
139135
return keccak256(serializedTransaction);
140136
}
141137
}

tests/e2e.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
recoverMessageAddress,
88
zeroAddress,
99
} from "viem";
10-
dotenv.config();
10+
dotenv.config({
11+
override: true,
12+
quiet: true,
13+
});
1114

1215
describe("End To End", () => {
1316
let mockedAdapter: NearEthAdapter;
@@ -32,13 +35,12 @@ describe("End To End", () => {
3235
it.skip("signAndSendTransaction", async () => {
3336
await expect(
3437
realAdapter.signAndSendTransaction({
35-
// Sending 1 WEI to self (so we never run out of funds)
3638
to: realAdapter.address,
3739
value: ONE_WEI,
3840
chainId,
3941
})
40-
).resolves.not.toThrow();
41-
});
42+
).resolves.toMatch(/^0x[a-fA-F0-9]{64}$/); // crude match for tx hash
43+
}, 20000);
4244

4345
it.skip("signAndSendTransaction - Gnosis Chain", async () => {
4446
await expect(

0 commit comments

Comments
 (0)