Skip to content

Commit

Permalink
near encode Signature Request (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Sep 13, 2024
1 parent 8a69b89 commit 3d46518
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
10 changes: 7 additions & 3 deletions examples/send-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { loadArgs } from "./cli";
import { TransactionManager } from "../src";
import { nearAccountFromKeyPair } from "near-ca";
import { KeyPair } from "near-api-js";
import { KeyPairString } from "near-api-js/lib/utils";

dotenv.config();

async function main(): Promise<void> {
const options = await loadArgs();
const nearAccount = await nearAccountFromKeyPair({
accountId: process.env.NEAR_ACCOUNT_ID!,
keyPair: KeyPair.fromString(process.env.NEAR_ACCOUNT_PRIVATE_KEY!),
keyPair: KeyPair.fromString(
process.env.NEAR_ACCOUNT_PRIVATE_KEY! as KeyPairString
),
network: {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
Expand Down Expand Up @@ -40,11 +43,12 @@ async function main(): Promise<void> {
transactions.push(recoveryTx);
}

const { unsignedUserOp, safeOpHash } = await txManager.buildTransaction({
const unsignedUserOp = await txManager.buildTransaction({
transactions,
options,
usePaymaster: options.usePaymaster,
});
console.log("Unsigned UserOp", unsignedUserOp);
const safeOpHash = await txManager.opHash(unsignedUserOp);
console.log("Safe Op Hash", safeOpHash);

// TODO: Evaluate gas cost (in ETH)
Expand Down
13 changes: 4 additions & 9 deletions src/lib/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
getSafeModuleSetupDeployment,
} from "@safe-global/safe-modules-deployments";
import { PLACEHOLDER_SIG, packGas, packPaymasterData } from "../util";
import {
GasPrice,
PaymasterData,
UnsignedUserOperation,
UserOperation,
} from "../types";
import { GasPrice, UnsignedUserOperation, UserOperation } from "../types";
import { MetaTransaction } from "ethers-multisend";

/**
Expand Down Expand Up @@ -121,8 +116,8 @@ export class ContractSuite {
}

async getOpHash(
unsignedUserOp: UserOperation,
paymasterData: PaymasterData
unsignedUserOp: UserOperation
// paymasterData: PaymasterData
): Promise<string> {
return this.m4337.getOperationHash({
...unsignedUserOp,
Expand All @@ -140,7 +135,7 @@ export class ContractSuite {
unsignedUserOp.maxPriorityFeePerGas,
unsignedUserOp.maxFeePerGas
),
paymasterAndData: packPaymasterData(paymasterData),
paymasterAndData: packPaymasterData(unsignedUserOp),
signature: PLACEHOLDER_SIG,
});
}
Expand Down
40 changes: 26 additions & 14 deletions src/tx-manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ethers } from "ethers";
import { NearEthAdapter, MpcContract } from "near-ca";
import { NearEthAdapter, MpcContract, NearEthTxData } from "near-ca";
import { Erc4337Bundler } from "./lib/bundler";
import { packSignature } from "./util";
import { getNearSignature } from "./lib/near";
import { UserOperation, UserOperationReceipt, UserOptions } from "./types";
import { UserOperation, UserOperationReceipt } from "./types";
import { MetaTransaction, encodeMulti } from "ethers-multisend";
import { ContractSuite } from "./lib/safe";
import { Account } from "near-api-js";
Expand Down Expand Up @@ -92,9 +92,9 @@ export class TransactionManager {

async buildTransaction(args: {
transactions: MetaTransaction[];
options: UserOptions;
}): Promise<{ safeOpHash: string; unsignedUserOp: UserOperation }> {
const { transactions, options } = args;
usePaymaster: boolean;
}): Promise<UserOperation> {
const { transactions, usePaymaster } = args;
const gasFees = (await this.bundler.getGasPrice()).fast;
// const gasFees = await this.provider.getFeeData();
// Build Singular MetaTransaction for Multisend from transaction list.
Expand All @@ -114,27 +114,39 @@ export class TransactionManager {

const paymasterData = await this.bundler.getPaymasterData(
rawUserOp,
options.usePaymaster,
usePaymaster,
this.safeNotDeployed
);

const unsignedUserOp = { ...rawUserOp, ...paymasterData };
const safeOpHash = await this.safePack.getOpHash(
unsignedUserOp,
paymasterData
);

return {
safeOpHash,
unsignedUserOp,
};
return unsignedUserOp;
}

async signTransaction(safeOpHash: string): Promise<string> {
const signature = await getNearSignature(this.nearAdapter, safeOpHash);
return packSignature(signature);
}

async opHash(userOp: UserOperation): Promise<string> {
return this.safePack.getOpHash(userOp);
}
async encodeSignatureRequest(
unsignedUserOp: UserOperation
): Promise<NearEthTxData> {
const safeOpHash = (await this.opHash(unsignedUserOp)) as `0x${string}`;
const txData = this.nearAdapter.encodeSignRequest({
method: "hash",
chainId: 0,
params: safeOpHash as `0x${string}`,
});

return {
...txData,
evmMessage: unsignedUserOp,
};
}

async executeTransaction(
userOp: UserOperation
): Promise<UserOperationReceipt> {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2289,14 +2289,14 @@ near-api-js@^5.0.0:
node-fetch "2.6.7"

near-ca@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/near-ca/-/near-ca-0.5.2.tgz#02ab0e7f0c86ab3e37b54ef0f72e2d4b5c76ae56"
integrity sha512-MbZo5IsG7YY6v3WVtestqjLz/THzc0UdOpx1KLUbmPO9CuNAMvMN5HhMIr2ZlPq+VuUui3tRVlOyilWt/x6vRg==
version "0.5.4"
resolved "https://registry.yarnpkg.com/near-ca/-/near-ca-0.5.4.tgz#04d0d6f50005e312c3ac0aec3ccf85c93e940bfb"
integrity sha512-Q5hY6uY3/2qYOqiXT23an3hyKeqPKG1RJRGIn/v55I+8FdlIvJYLd/gllB4crDn2psYwjUoevwTUbRLMP33bmA==
dependencies:
"@walletconnect/web3wallet" "^1.13.0"
elliptic "^6.5.6"
near-api-js "^5.0.0"
viem "^2.17.5"
viem "^2.21.6"

node-addon-api@^5.0.0:
version "5.1.0"
Expand Down Expand Up @@ -2860,7 +2860,7 @@ util-deprecate@^1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

viem@^2.17.5:
viem@^2.21.6:
version "2.21.6"
resolved "https://registry.yarnpkg.com/viem/-/viem-2.21.6.tgz#741f9e579e069335d1540051dde19aea272ee1d7"
integrity sha512-YX48IVl6nZ4FRsY4ypv2RrxtQVWysIY146/lBW53tma8u32h8EsiA7vecw9ZbrueNUy/asHR4Egu68Z6FOvDzQ==
Expand Down

0 comments on commit 3d46518

Please sign in to comment.