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 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
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
58 changes: 58 additions & 0 deletions src/mpcContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { TGAS } from "./chains/near";
import { MPCSignature, FunctionCallTransaction, SignArgs } from "./types";
import { transformSignature } from "./utils/signature";
import { Action, FunctionCall } from "near-api-js/lib/transaction";

/**
* Near Contract Type for change methods.
Expand Down Expand Up @@ -113,6 +114,34 @@ export class MpcContract implements IMpcContract {
return transformSignature(mpcSig);
};

requestMulti = async (
signArgs: SignArgs[],
gas?: bigint
): Promise<Signature[]> => {
const transaction = await this.encodeMulti(signArgs, gas);
// TODO: This is a hack to prevent out of gas errors
const maxGasPerAction = 300000000000000n / BigInt(signArgs.length);
const result = await this.connectedAccount.signAndSendTransaction({
receiverId: transaction.receiverId,
actions: transaction.actions.map((action) => {
return new Action({
functionCall: new FunctionCall({
methodName: "sign",
args: Buffer.from(JSON.stringify(action.params.args)),
gas: maxGasPerAction,
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 +162,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 +202,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
42 changes: 24 additions & 18 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ describe("End To End", () => {

it.skip("signAndSendTransaction", async () => {
await expect(
realAdapter.signAndSendTransaction({
// Sending 1 WEI to self (so we never run out of funds)
to: realAdapter.address,
value: ONE_WEI,
chainId,
})
realAdapter.signAndSendTransaction([
{
// Sending 1 WEI to self (so we never run out of funds)
to: realAdapter.address,
value: ONE_WEI,
chainId,
},
])
).resolves.not.toThrow();
});

it.skip("signAndSendTransaction - Gnosis Chain", async () => {
await expect(
realAdapter.signAndSendTransaction({
// Sending 1 WEI to self (so we ~never run out of funds)
to: realAdapter.address,
value: ONE_WEI,
// Gnosis Chain!
chainId: 100,
})
realAdapter.signAndSendTransaction([
{
// Sending 1 WEI to self (so we ~never run out of funds)
to: realAdapter.address,
value: ONE_WEI,
// Gnosis Chain!
chainId: 100,
},
])
).resolves.not.toThrow();
});

Expand All @@ -54,11 +58,13 @@ describe("End To End", () => {
address: mockedAdapter.address,
});
await expect(
realAdapter.signAndSendTransaction({
to,
value: senderBalance + ONE_WEI,
chainId,
})
realAdapter.signAndSendTransaction([
{
to,
value: senderBalance + ONE_WEI,
chainId,
},
])
).rejects.toThrow();
});

Expand Down
Loading