Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Near Adapter for Signature #1

Merged
merged 12 commits into from
Jun 14, 2024
7 changes: 7 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NEAR_MULTICHAIN_CONTRACT=v2.multichain-mpc.testnet

NEAR_ACCOUNT_ID=
NEAR_ACCOUNT_PRIVATE_KEY=

SAFE_SALT_NONCE=
ERC4337_BUNDLER_URL=
29 changes: 22 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
} from "@safe-global/safe-modules-deployments";
import dotenv from "dotenv";
import { ethers } from "ethers";
import { NearEthAdapter, MultichainContract } from "near-ca";

dotenv.config();
const { NEAR_EVM_ADDRESS, SAFE_SALT_NONCE, ERC4337_BUNDLER_URL } = process.env;
const { SAFE_SALT_NONCE, ERC4337_BUNDLER_URL } = process.env;

type DeploymentFunction = (filter?: {
version: string;
Expand All @@ -40,10 +41,19 @@ async function getDeployment(
);
}

async function getNearSignature(hash: ethers.BytesLike) {
// TODO(bh2smith): do your thing
//assert(ethers.recoverAddress(hash, "0x...") === NEAR_EVM_ADDRESS);
return ethers.solidityPacked(["uint256", "uint256", "uint8"], [1, 2, 27]);
async function getNearSignature(
adapter: NearEthAdapter,
hash: ethers.BytesLike,
): Promise<`0x${string}`> {
const viemHash = typeof hash === "string" ? (hash as `0x${string}`) : hash;
// MPC Contract produces two possible signatures.
const signatures = await adapter.sign(viemHash);
for (const sig of signatures) {
if (ethers.recoverAddress(hash, sig) === adapter.address) {
return sig;
}
}
throw new Error("Invalid signature!");
}

async function sendUserOperation(userOp: unknown, entryPoint: string) {
Expand Down Expand Up @@ -89,10 +99,14 @@ async function main() {
),
};

const nearAdapter = await NearEthAdapter.fromConfig({
mpcContract: await MultichainContract.fromEnv(),
});

const setup = await contracts.singleton.interface.encodeFunctionData(
"setup",
[
[NEAR_EVM_ADDRESS],
[nearAdapter.address],
1,
contracts.moduleSetup.target,
contracts.moduleSetup.interface.encodeFunctionData("enableModules", [
Expand All @@ -110,6 +124,7 @@ async function main() {
setup,
SAFE_SALT_NONCE,
);
console.log("Safe Address:", safeAddress);

const safeNotDeployed = (await provider.getCode(safeAddress)) === "0x";
const { maxPriorityFeePerGas, maxFeePerGas } = await provider.getFeeData();
Expand Down Expand Up @@ -170,7 +185,7 @@ async function main() {
});
console.log(safeOpHash);

const signature = await getNearSignature(safeOpHash);
const signature = await getNearSignature(nearAdapter, safeOpHash);
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
console.log(
await sendUserOperation(
{ ...unsignedUserOp, signature },
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"near-ca": "^0.0.9"
},
"devDependencies": {
"@types/node": "^20.14.2",
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
"prettier": "^3.3.2",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.13.tgz#f64277c341150c979e42b00e4ac289290c9df469"
integrity sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==

"@types/node@^20.14.2":
version "20.14.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.14.2.tgz#a5f4d2bcb4b6a87bffcaa717718c5a0f208f4a18"
integrity sha512-xyu6WAMVwv6AKFLB+e/7ySZVr/0zLCzOa7rSpq6jNwpqOrUbcACDWC+53d4n2QHOnDou0fbIsg8wZu/sxrnI4Q==
dependencies:
undici-types "~5.26.4"

"@walletconnect/[email protected]":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@walletconnect/auth-client/-/auth-client-2.1.2.tgz#cee304fb0cdca76f6bf4aafac96ef9301862a7e8"
Expand Down Expand Up @@ -1946,6 +1953,11 @@ uncrypto@^0.1.3:
resolved "https://registry.yarnpkg.com/uncrypto/-/uncrypto-0.1.3.tgz#e1288d609226f2d02d8d69ee861fa20d8348ef2b"
integrity sha512-Ql87qFHB3s/De2ClA9e0gsnS6zXG27SkTiSJwjCc9MebbfapQfuPzumMIUMi38ezPZVNFcHI9sUIepeQfw8J8Q==

undici-types@~5.26.4:
version "5.26.5"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

unenv@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/unenv/-/unenv-1.9.0.tgz#469502ae85be1bd3a6aa60f810972b1a904ca312"
Expand Down