Skip to content

Commit

Permalink
zk fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Jan 28, 2022
1 parent 22fcca3 commit 30249c4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion sdk/zksync.js/src/abstract-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { Transaction, ETHOperation } from './operations';
export abstract class AbstractWallet {
public provider: SyncProvider;

protected constructor(public cachedAddress: Address, public accountId?: number) { }
protected constructor(public cachedAddress: Address, public accountId?: number) {}

connect(provider: SyncProvider) {
this.provider = provider;
Expand Down
49 changes: 26 additions & 23 deletions sdk/zksync.js/src/remote-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class RemoteWallet extends AbstractWallet {
private web3Provider: ethers.providers.Web3Provider,
private _ethMessageSigner: EthMessageSigner,
cachedAddress: Address,
accountId?: number,
accountId?: number
) {
super(cachedAddress, accountId);
this.web3Signer = web3Provider.getSigner();
Expand All @@ -48,17 +48,21 @@ export class RemoteWallet extends AbstractWallet {
// user connects.
const ethSignerType: EthSignerType = {
verificationMethod: 'ERC-1271',
isSignedMsgPrefixed: true,
isSignedMsgPrefixed: true
};

const ethMessageSigner = new EthMessageSigner(web3Provider.getSigner(), ethSignerType);
const wallet = new RemoteWallet(web3Provider, ethMessageSigner, await web3Provider.getSigner().getAddress(), accountId);
const wallet = new RemoteWallet(
web3Provider,
ethMessageSigner,
await web3Provider.getSigner().getAddress(),
accountId
);
wallet.connect(provider);
await wallet.verifyNetworks();
return wallet;
}


// ****************
// Abstract getters
//
Expand Down Expand Up @@ -90,7 +94,7 @@ export class RemoteWallet extends AbstractWallet {
): Promise<{ txs: SignedTransaction[]; signature?: TxEthSignature }> {
let nonce: number = await this.getNonce(startNonce);
// Collect transaction bodies and set nonces in it.
const txsToSign = txs.map(tx => {
const txsToSign = txs.map((tx) => {
tx.tx.nonce = nonce;
nonce += 1;
return tx.tx;
Expand Down Expand Up @@ -225,7 +229,7 @@ export class RemoteWallet extends AbstractWallet {
validFrom?: number;
validUntil?: number;
}): Promise<Order> {
throw Error("Not implemented");
throw Error('Not implemented');
}

override async getOrder(order: {
Expand All @@ -238,11 +242,11 @@ export class RemoteWallet extends AbstractWallet {
validFrom?: number;
validUntil?: number;
}): Promise<Order> {
throw Error("Not implemented");
throw Error('Not implemented');
}

override async signOrder(order: Order): Promise<Order> {
throw Error("Not implemented");
throw Error('Not implemented');
}

override async signSyncSwap(swap: {
Expand Down Expand Up @@ -387,26 +391,26 @@ export class RemoteWallet extends AbstractWallet {
/**
* Performs an RPC call to the custom `zkSync_signBatch` method.
* This method is specified here: https://github.com/argentlabs/argent-contracts-l2/discussions/4
*
*
* Basically, it's an addition to the WalletConnect server that accepts intentionally incomplete
* transactions (e.g. with no account IDs resolved), and returns transactions with both L1 and L2
* signatures.
*
*
* @param txs A list of transactions to be signed.
*
*
* @returns A list of singed transactions.
*/
protected async callExtSignZkSyncBatch(txs: any[]): Promise<SignedTransaction[]> {
try {
// Response must be an array of signed transactions.
// Transactions are flattened (ethereum signatures are on the same level as L2 signatures),
// so we need to "unflat" each one.
const response: any[] = await this.web3Provider.send("zkSync_signerPubKeyHash", null);
const response: any[] = await this.web3Provider.send('zkSync_signerPubKeyHash', null);

const transactions = response.map((tx) => {
const ethereumSignature = tx["ethereumSignature"];
const ethereumSignature = tx['ethereumSignature'];
// Remove the L1 signature from the transaction data.
delete tx["ethereumSignature"];
delete tx['ethereumSignature'];
return {
tx,
ethereumSignature
Expand All @@ -416,26 +420,25 @@ export class RemoteWallet extends AbstractWallet {
return transactions;
} catch (e) {
console.error(`Received an error performing 'zkSync_signBatch' request: ${e.toString()}`);
throw new Error("Wallet server returned a malformed response to the sign batch request");
throw new Error('Wallet server returned a malformed response to the sign batch request');
}
}

/**
* Performs an RPC call to the custom `zkSync_signerPubKeyHash` method.
*
*
* This method should return a public key hash associated with the wallet
*/
protected async callExtSignerPubKeyHash(): Promise<PubKeyHash> {
try {
const response = await this.web3Provider.send("zkSync_signerPubKeyHash", null);
if (!response["pubKeyHash"]) {
throw new Error("Wallet server returned a malformed response to the PubKeyHash request");
const response = await this.web3Provider.send('zkSync_signerPubKeyHash', null);
if (!response['pubKeyHash']) {
throw new Error('Wallet server returned a malformed response to the PubKeyHash request');
}
return response["pubKeyHash"];
}
catch (e) {
return response['pubKeyHash'];
} catch (e) {
console.error(`Received an error performing 'zkSync_signerPubKeyHash' request: ${e.toString()}`);
throw new Error("Wallet server returned a malformed response to the PubKeyHash request");
throw new Error('Wallet server returned a malformed response to the PubKeyHash request');
}
}
}
84 changes: 42 additions & 42 deletions sdk/zksync.js/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignTransfer({
stringAmount,
stringFee,
stringToken,
to: transfer.to,
nonce: transfer.nonce,
accountId: this.accountId
});
stringAmount,
stringFee,
stringToken,
to: transfer.to,
nonce: transfer.nonce,
accountId: this.accountId
});
return {
tx: signedTransferTransaction,
ethereumSignature
Expand Down Expand Up @@ -418,13 +418,13 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignWithdraw({
stringAmount,
stringFee,
stringToken,
ethAddress: withdraw.ethAddress,
nonce: withdraw.nonce,
accountId: this.accountId
});
stringAmount,
stringFee,
stringToken,
ethAddress: withdraw.ethAddress,
nonce: withdraw.nonce,
accountId: this.accountId
});

return {
tx: signedWithdrawTransaction,
Expand Down Expand Up @@ -475,11 +475,11 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignForcedExit({
stringToken,
stringFee,
target: forcedExit.target,
nonce: forcedExit.nonce
});
stringToken,
stringFee,
target: forcedExit.target,
nonce: forcedExit.nonce
});

return {
tx: signedForcedExitTransaction,
Expand Down Expand Up @@ -580,13 +580,13 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignOrder({
amount: stringAmount,
tokenSell: stringTokenSell,
tokenBuy: stringTokenBuy,
nonce: order.nonce,
recipient: order.recipient,
ratio: order.ratio
});
amount: stringAmount,
tokenSell: stringTokenSell,
tokenBuy: stringTokenBuy,
nonce: order.nonce,
recipient: order.recipient,
ratio: order.ratio
});
order.ethSignature = ethereumSignature;
return order;
}
Expand All @@ -606,10 +606,10 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignSwap({
fee: stringFee,
feeToken: stringToken,
nonce: swap.nonce
});
fee: stringFee,
feeToken: stringToken,
nonce: swap.nonce
});

return {
tx: signedSwapTransaction,
Expand Down Expand Up @@ -666,12 +666,12 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignMintNFT({
stringFeeToken,
stringFee,
recipient: mintNFT.recipient,
contentHash: mintNFT.contentHash,
nonce: mintNFT.nonce
});
stringFeeToken,
stringFee,
recipient: mintNFT.recipient,
contentHash: mintNFT.contentHash,
nonce: mintNFT.nonce
});

return {
tx: signedMintNFTTransaction,
Expand Down Expand Up @@ -721,12 +721,12 @@ export class Wallet extends AbstractWallet {
const ethereumSignature = unableToSign(this.ethSigner())
? null
: await this.ethMessageSigner().ethSignWithdrawNFT({
token: withdrawNFT.token,
to: withdrawNFT.to,
stringFee,
stringFeeToken,
nonce: withdrawNFT.nonce
});
token: withdrawNFT.token,
to: withdrawNFT.to,
stringFee,
stringFeeToken,
nonce: withdrawNFT.nonce
});

return {
tx: signedWithdrawNFTTransaction,
Expand Down

0 comments on commit 30249c4

Please sign in to comment.