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

From fallback #200

Merged
merged 3 commits into from
Jun 17, 2024
Merged
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
41 changes: 7 additions & 34 deletions src/signers/abstract-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
* sufficent for most developers, but this is provided to fascilitate more complex Signers.
*/
import { AddressLike, resolveAddress, validateAddress } from '../address/index.js';
import { defineProperties, getBigInt, resolveProperties, assert, assertArgument, isQiAddress } from '../utils/index.js';
import {
addressFromTransactionRequest,
copyRequest,
QiTransactionRequest,
QuaiTransactionRequest,
} from '../providers/provider.js';
import { defineProperties, getBigInt, resolveProperties, assert, assertArgument } from '../utils/index.js';
import { addressFromTransactionRequest, copyRequest, QuaiTransactionRequest } from '../providers/provider.js';

import type { TypedDataDomain, TypedDataField } from '../hash/index.js';
import type { TransactionLike } from '../transaction/index.js';

import type { BlockTag, Provider, TransactionRequest, TransactionResponse } from '../providers/provider.js';
import type { Signer } from './signer.js';
import { getTxType } from '../utils/index.js';
import { QiTransaction, QiTransactionLike, QuaiTransaction, QuaiTransactionLike } from '../transaction/index.js';
import { QuaiTransaction, QuaiTransactionLike } from '../transaction/index.js';
import { toZone, Zone } from '../constants/index.js';

function checkProvider(signer: AbstractSigner, operation: string): Provider {
Expand Down Expand Up @@ -155,19 +150,6 @@ export abstract class AbstractSigner<P extends null | Provider = null | Provider
return await resolveProperties(pop);
}

async populateQiTransaction(tx: QiTransactionRequest): Promise<QiTransactionLike> {
const pop = {
inputsUTXO: tx.inputs,
outputsUTXO: tx.outputs,
chainId: tx.chainId,
type: 2,
};

//@TOOD: Don't await all over the place; save them up for
// the end for better batching
return await resolveProperties(pop);
}

async estimateGas(tx: TransactionRequest): Promise<bigint> {
return checkProvider(this, 'estimateGas').estimateGas(await this.populateCall(tx));
}
Expand All @@ -178,22 +160,13 @@ export abstract class AbstractSigner<P extends null | Provider = null | Provider

async sendTransaction(tx: TransactionRequest): Promise<TransactionResponse> {
const provider = checkProvider(this, 'sendTransaction');
const sender = await this.getAddress();
const zone = await this.zoneFromAddress(addressFromTransactionRequest(tx));
const pop = await this.populateQuaiTransaction(tx as QuaiTransactionRequest);
const txObj = QuaiTransaction.from(pop);

let pop;
let txObj;
if (isQiAddress(sender)) {
pop = await this.populateQiTransaction(tx);
txObj = QiTransaction.from(pop);
} else {
pop = await this.populateQuaiTransaction(tx as QuaiTransactionRequest);
txObj = QuaiTransaction.from(pop);
}

const sender = await this.getAddress();
const signedTx = await this.signTransaction(txObj);

return await provider.broadcastTransaction(zone, signedTx, 'from' in tx ? tx.from : undefined);
return await provider.broadcastTransaction(zone, signedTx, sender);
}

abstract signTransaction(tx: TransactionRequest): Promise<string>;
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/quai-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export class QuaiTransaction extends AbstractTransaction<Signature> implements Q
}

if (tx.from != null) {
validateAddress(tx.from);
assertArgument(!isQiAddress(tx.from), 'from address must be a Quai address', 'tx.from', tx.from);
assertArgument(
(result.from || '').toLowerCase() === (tx.from || '').toLowerCase(),
'from mismatch',
Expand Down
19 changes: 9 additions & 10 deletions src/wallet/base-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,26 @@ export class BaseWallet extends AbstractSigner {
from: tx.from ? resolveAddress(tx.from) : undefined,
});

if (to != null) {
if (to !== undefined) {
validateAddress(to);
tx.to = to;
}
if (from != null) {
validateAddress(from);
tx.from = from;
}

if (tx.from != null) {
if (from !== undefined) {
assertArgument(
getAddress(<string>tx.from) === this.#address,
getAddress(<string>from) === this.#address,
'transaction from address mismatch',
'tx.from',
tx.from,
from,
);
} else {
// No `from` specified, use the wallet's address
tx.from = this.#address;
}

const btx = QuaiTransaction.from(<QuaiTransactionLike>tx);
const digest= keccak256(btx.unsignedSerialized)
btx.signature = this.signingKey.sign(digest)
const digest = keccak256(btx.unsignedSerialized);
btx.signature = this.signingKey.sign(digest);

return btx.serialized;
}
Expand Down
Loading