Skip to content

Commit

Permalink
refactor(signer): remove L2VoidSigner and L1VoidSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelTxFusion committed Jan 22, 2024
1 parent b52bf85 commit c616c86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 122 deletions.
94 changes: 1 addition & 93 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,96 +183,4 @@ export class L1Signer extends AdapterL1(ethers.JsonRpcSigner) {
return this;
}
}

export class VoidSigner extends AdapterL2(ethers.VoidSigner) {
// @ts-ignore
public override provider: Provider;
// @ts-ignore
public eip712: EIP712Signer;

override _signerL2() {
return this;
}

override _providerL2() {
return this.provider;
}

static from(signer: ethers.VoidSigner & { provider: Provider }, chainId: number): VoidSigner {
const newSigner: VoidSigner = Object.setPrototypeOf(signer, VoidSigner.prototype);
newSigner.eip712 = new EIP712Signer(newSigner, chainId);
return newSigner;
}

override async sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
if (transaction.customData == null && transaction.type == null) {
// use legacy txs by default
transaction.type = 0;
}
if (transaction.customData == null && transaction.type != EIP712_TX_TYPE) {
return (await super.sendTransaction(transaction)) as TransactionResponse;
} else {
const address = await this.getAddress();
const from =
transaction.from == null
? address
: await ethers.resolveAddress(transaction.from as Address);
if (from.toLowerCase() != address.toLowerCase()) {
throw new Error("Transaction `from` address mismatch");
}
const tx: TransactionLike = {
type: transaction.type ?? EIP712_TX_TYPE,
value: transaction.value ?? 0,
data: transaction.data ?? "0x",
nonce: transaction.nonce ?? (await this.getNonce()),
gasPrice: transaction.gasPrice ?? (await this.provider.getGasPrice()),
gasLimit: transaction.gasLimit ?? (await this.provider.estimateGas(transaction)),
chainId: transaction.chainId ?? (await this.provider.getNetwork()).chainId,
to: await ethers.resolveAddress(transaction.to as Address),
customData: this._fillCustomData(transaction.customData ?? {}),
from,
};
// @ts-ignore
tx.customData.customSignature = await this.eip712.sign(tx);

const txBytes = serializeEip712(tx);
return await this.provider.broadcastTransaction(txBytes);
}
}
}

// This class is to be used on the frontend with metamask injection.
// It only contains L1 operations. For L2 operations, see VoidSigner.
// Sample usage:
// const ethProvider = new ethers.BrowserProvider(window.ethereum);
// const provider = new Provider('<rpc_url>');
// const signer = L1Signer.from(provider.getSigner(), provider);
// const tx = await signer.deposit({ ... });
export class L1VoidSigner extends AdapterL1(ethers.VoidSigner) {
// @ts-ignore
public providerL2: Provider;
override _providerL2() {
return this.providerL2;
}

override _providerL1(): ethers.Provider {
// @ts-ignore
return this.provider;
}

override _signerL1() {
return this;
}

static from(signer: ethers.VoidSigner, zksyncProvider: Provider): L1VoidSigner {
const newSigner: L1VoidSigner = Object.setPrototypeOf(signer, L1VoidSigner.prototype);
newSigner.providerL2 = zksyncProvider;
return newSigner;
}

connectToL2(provider: Provider): this {
this.providerL2 = provider;
return this;
}
}
/* c8 ignore stop */
/* c8 ignore stop */
56 changes: 27 additions & 29 deletions tests/integration/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("Wallet", () => {
expect(ethWallet.provider).to.be.equal(ethProvider);
});

it("should throw an error when L1 `Wallet` is not specified in constructor", async () => {
it("should throw an error when L1 `Provider` is not specified in constructor", async () => {
const wallet = new Wallet(PRIVATE_KEY, provider);
try {
wallet.ethWallet();
Expand Down Expand Up @@ -173,6 +173,28 @@ describe("Wallet", () => {
});

describe("#populateTransaction()", () => {
it("should return a populated transaction", async () => {
const tx = {
to: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618",
value: BigInt(7_000_000_000),
type: utils.EIP712_TX_TYPE,
from: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049",
nonce: await wallet.getNonce("pending"),
gasLimit: BigInt(154_379),
chainId: BigInt(270),
data: "0x",
customData: { gasPerPubdata: 50_000, factoryDeps: [] },
gasPrice: BigInt(250_000_000),
};

const result = await wallet.populateTransaction({
type: utils.EIP712_TX_TYPE,
to: RECEIVER,
value: 7_000_000_000,
});
expect(result).to.be.deep.equal(tx);
}).timeout(25_000);

it("should return a populated transaction with default values if are omitted", async () => {
const tx = {
to: RECEIVER,
Expand Down Expand Up @@ -290,7 +312,6 @@ describe("Wallet", () => {
value: BigInt(288_992_000_000_000),
from: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049",
to: await (await wallet.getL1BridgeContracts()).erc20.getAddress(),
data: "0xe8b99b1b00000000000000000000000036615cf349d7f6344891b1e7ca7c72883f5dc049000000000000000000000000881567b68502e6d7a7a3556ff4313b637ba47f4e0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000008e0f6000000000000000000000000000000000000000000000000000000000000032000000000000000000000000036615cf349d7f6344891b1e7ca7c72883f5dc049",
};
const result = await wallet.getDepositTx({
token: DAI_L1,
Expand Down Expand Up @@ -419,7 +440,7 @@ describe("Wallet", () => {

describe("#getFullRequiredDepositFee()", () => {
it("should return a fee for ETH token deposit", async () => {
const FEE_DATA = {
const feeData = {
baseCost: BigInt(285_096_500_000_000),
l1GasLimit: BigInt(132_711),
l2GasLimit: "0x8b351",
Expand All @@ -430,7 +451,7 @@ describe("Wallet", () => {
token: utils.ETH_ADDRESS,
to: await wallet.getAddress(),
});
expect(result).to.be.deep.equal(FEE_DATA);
expect(result).to.be.deep.equal(feeData);
});

it("should throw an error when there is not enough allowance to cover the deposit", async () => {
Expand All @@ -445,7 +466,7 @@ describe("Wallet", () => {
}).timeout(10_000);

it("should return a fee for DAI token deposit", async () => {
const FEE_DATA = {
const feeData = {
baseCost: BigInt(288_992_000_000_000),
l1GasLimit: BigInt(253_177),
l2GasLimit: "0x8d1c0",
Expand All @@ -460,7 +481,7 @@ describe("Wallet", () => {
token: DAI_L1,
to: await wallet.getAddress(),
});
expect(result).to.be.deep.equal(FEE_DATA);
expect(result).to.be.deep.equal(feeData);
}).timeout(10_000);

it("should throw an error when there is not enough balance for the deposit", async () => {
Expand Down Expand Up @@ -629,27 +650,4 @@ describe("Wallet", () => {
}
}).timeout(25_000);
});

describe("#populateTransaction()", () => {
it("should return a populated transaction", async () => {
const TX = {
to: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618",
value: BigInt(7_000_000_000),
type: utils.EIP712_TX_TYPE,
from: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049",
gasLimit: BigInt(154_379),
chainId: BigInt(270),
data: "0x",
customData: { gasPerPubdata: 50_000, factoryDeps: [] },
gasPrice: BigInt(250_000_000),
};

const result = await wallet.populateTransaction({
type: utils.EIP712_TX_TYPE,
to: RECEIVER,
value: 7_000_000_000,
});
expect(result).to.be.deepEqualExcluding(TX, ["nonce"]);
}).timeout(25_000);
});
});

0 comments on commit c616c86

Please sign in to comment.