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

Attempt MultiSign #143

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Attempt MultiSign
  • Loading branch information
bh2smith committed Nov 6, 2024
commit 550d5db97806dedd7c3fe7ee3a6b141ff02489ee
16 changes: 8 additions & 8 deletions examples/send-eth.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import dotenv from "dotenv";
import { SEPOLIA_CHAIN_ID, setupNearEthAdapter } from "./setup";
import { setupNearEthAdapter } from "./setup";
dotenv.config();

const run = async (): Promise<void> => {
const evm = await setupNearEthAdapter();
await evm.signAndSendTransaction({
// Sending to self.
to: evm.address,
// THIS IS ONE WEI!
value: 1n,
chainId: SEPOLIA_CHAIN_ID,
});
console.log(evm.address);
// throw new Error("You foked up");
const transactions = [
{ to: evm.address, value: 1n, chainId: 97 },
{ to: evm.address, value: 1n, chainId: 1301 },
];
await evm.signAndSendTransaction(transactions);
};

run();
27 changes: 21 additions & 6 deletions src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class NearEthAdapter {
readonly mpcContract: IMpcContract;
readonly address: Address;
readonly derivationPath: string;
readonly keyVersion: number;
readonly beta: Beta;

private constructor(config: {
Expand All @@ -41,6 +42,7 @@ export class NearEthAdapter {
}) {
this.mpcContract = config.mpcContract;
this.derivationPath = config.derivationPath;
this.keyVersion = 0;
this.address = config.sender;
this.beta = new Beta(this);
}
Expand Down Expand Up @@ -103,16 +105,29 @@ export class NearEthAdapter {
* Note that the signature request is a recursive function.
*/
async signAndSendTransaction(
txData: BaseTx,
txData: BaseTx[],
nearGas?: bigint
): Promise<Hash> {
const { transaction, signArgs } = await this.createTxPayload(txData);
): Promise<Hash[]> {
// Note that chainIds must be all different or
// we have to make special consideration for the nonces.
const txArray = await Promise.all(
txData.map((tx) => this.createTxPayload(tx))
);

console.log(`Requesting signature from ${this.mpcContract.accountId()}`);
const signature = await this.mpcContract.requestSignature(
signArgs,
const signatures = await this.mpcContract.requestMulti(
txArray.map((tx) => tx.signArgs),
nearGas
);
return broadcastSignedTransaction({ transaction, signature });
const transactions = txArray.map((tx) => tx.transaction);
return Promise.all(
transactions.map((transaction, i) =>
broadcastSignedTransaction({
transaction,
signature: signatures[i]!,
})
)
);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/mpcContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ export class MpcContract implements IMpcContract {
return transformSignature(mpcSig);
};

requestMulti = async (
signArgs: SignArgs[],
gas?: bigint
): Promise<Signature[]> => {
const transaction = await this.encodeMulti(signArgs, gas);

const result = await this.connectedAccount.signAndSendTransaction({
receiverId: transaction.receiverId,
actions: transaction.actions.map((action) => {
return {
enum: action.type,
functionCall: {
methodName: action.params.methodName,
args: Buffer.from(JSON.stringify(action.params.args)),
gas: BigInt(action.params.gas),
deposit: BigInt(action.params.deposit),
},
};
}),
});

// Extract signatures from each receipt outcome in order
const mpcSigs = result.receipts_outcome.map(
(receipt) => receipt.outcome.status as MPCSignature
);
return mpcSigs.map(transformSignature);
};

async encodeSignatureRequestTx(
signArgs: SignArgs,
gas?: bigint
Expand All @@ -133,6 +161,30 @@ export class MpcContract implements IMpcContract {
],
};
}

async encodeMulti(
signArgs: SignArgs[],
gas?: bigint
): Promise<FunctionCallTransaction<{ request: SignArgs }>> {
const deposit = await this.getDeposit();
return {
signerId: this.connectedAccount.accountId,
receiverId: this.contract.contractId,
actions: signArgs.map((args) => {
return {
type: "FunctionCall",
params: {
methodName: "sign",
args: {
request: args,
},
gas: gasOrDefault(gas),
deposit,
},
};
}),
};
}
}

function gasOrDefault(gas?: bigint): string {
Expand All @@ -149,8 +201,13 @@ export interface IMpcContract {
deriveEthAddress(derivationPath: string): Promise<Address>;
getDeposit(): Promise<string>;
requestSignature(signArgs: SignArgs, gas?: bigint): Promise<Signature>;
requestMulti(signArgs: SignArgs[], gas?: bigint): Promise<Signature[]>;
encodeSignatureRequestTx(
signArgs: SignArgs,
gas?: bigint
): Promise<FunctionCallTransaction<{ request: SignArgs }>>;
encodeMulti(
signArgs: SignArgs[],
gas?: bigint
): Promise<FunctionCallTransaction<{ request: SignArgs }>>;
}
9 changes: 9 additions & 0 deletions src/utils/mock-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export class MockMpcContract implements IMpcContract {
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
);
}
requestMulti(_signArgs: SignArgs[], _gas?: bigint): Promise<Signature[]> {
throw new Error("Method not implemented.");
}
encodeMulti(
_signArgs: SignArgs[],
_gas?: bigint
): Promise<FunctionCallTransaction<{ request: SignArgs }>> {
throw new Error("Method not implemented.");
}

accountId(): string {
return "mock-mpc.offline";
Expand Down
Loading