diff --git a/src/apis/evm/api.ts b/src/apis/evm/api.ts index ffb7a58a4..c242436a2 100644 --- a/src/apis/evm/api.ts +++ b/src/apis/evm/api.ts @@ -573,7 +573,7 @@ export class EVMAPI extends JRPCAPI { * This helper exists because the endpoint API should be the primary point of entry for most functionality. */ buildImportTx = async ( - utxoset: UTXOSet, + utxoset: UTXOSet | undefined, toAddress: string, ownerAddresses: string[], sourceChain: Buffer | string, @@ -595,13 +595,18 @@ export class EVMAPI extends JRPCAPI { "Error - EVMAPI.buildImportTx: sourceChain is undefined or invalid sourceChain type." ) } - const utxoResponse: UTXOResponse = await this.getUTXOs( - ownerAddresses, - srcChain, - 0, - undefined - ) - const atomicUTXOs: UTXOSet = utxoResponse.utxos + + var atomicUTXOs = utxoset + if (!utxoset) { + const utxoResponse: UTXOResponse = await this.getUTXOs( + ownerAddresses, + srcChain, + 0, + undefined + ) + atomicUTXOs = utxoResponse.utxos + } + const networkID: number = this.core.getNetworkID() const avaxAssetID: string = this.core.getNetwork().X.avaxAssetID const avaxAssetIDBuf: Buffer = bintools.cb58Decode(avaxAssetID) @@ -613,7 +618,7 @@ export class EVMAPI extends JRPCAPI { ) } - const builtUnsignedTx: UnsignedTx = utxoset.buildImportTx( + const builtUnsignedTx: UnsignedTx = atomicUTXOs.buildImportTx( networkID, bintools.cb58Decode(this.blockchainID), toAddress, diff --git a/src/apis/evm/basetx.ts b/src/apis/evm/basetx.ts index 82f4c8877..c3e3504cd 100644 --- a/src/apis/evm/basetx.ts +++ b/src/apis/evm/basetx.ts @@ -5,9 +5,9 @@ import { Buffer } from "buffer/" import BinTools from "../../utils/bintools" -import { KeyChain, KeyPair } from "./keychain" import { EVMStandardBaseTx } from "../../common/evmtx" import { Credential } from "../../common/credentials" +import { SignerKeyChain, SignerKeyPair } from "../../common/keychain" import { DefaultNetworkID } from "../../utils/constants" import { SelectTxClass } from "./tx" import { SerializedEncoding } from "../../utils/serialization" @@ -20,7 +20,10 @@ const bintools: BinTools = BinTools.getInstance() /** * Class representing a base for all transactions. */ -export class EVMBaseTx extends EVMStandardBaseTx { +export class EVMBaseTx extends EVMStandardBaseTx< + SignerKeyPair, + SignerKeyChain +> { protected _typeName = "BaseTx" protected _typeID = undefined @@ -63,7 +66,7 @@ export class EVMBaseTx extends EVMStandardBaseTx { * @returns An array of [[Credential]]s */ // eslint-disable-next-line @typescript-eslint/no-unused-vars - sign(msg: Buffer, kc: KeyChain): Credential[] { + sign(msg: Buffer, kc: SignerKeyChain): Credential[] { const creds: Credential[] = [] return creds } diff --git a/src/apis/evm/exporttx.ts b/src/apis/evm/exporttx.ts index 1419a6088..08cc4f333 100644 --- a/src/apis/evm/exporttx.ts +++ b/src/apis/evm/exporttx.ts @@ -6,10 +6,10 @@ import { Buffer } from "buffer/" import BinTools from "../../utils/bintools" import { EVMConstants } from "./constants" -import { KeyChain, KeyPair } from "./keychain" import { EVMBaseTx } from "./basetx" import { SelectCredentialClass } from "./credentials" -import { Signature, SigIdx, Credential } from "../../common/credentials" +import { Credential, Signature, SigIdx } from "../../common/credentials" +import { SignerKeyChain, SignerKeyPair } from "../../common/keychain" import { EVMInput } from "./inputs" import { Serialization, SerializedEncoding } from "../../utils/serialization" import { TransferableOutput } from "./outputs" @@ -161,13 +161,13 @@ export class ExportTx extends EVMBaseTx { * * @returns An array of [[Credential]]s */ - sign(msg: Buffer, kc: KeyChain): Credential[] { + sign(msg: Buffer, kc: SignerKeyChain): Credential[] { const creds: Credential[] = super.sign(msg, kc) this.inputs.forEach((input: EVMInput) => { const cred: Credential = SelectCredentialClass(input.getCredentialID()) const sigidxs: SigIdx[] = input.getSigIdxs() sigidxs.forEach((sigidx: SigIdx) => { - const keypair: KeyPair = kc.getKey(sigidx.getSource()) + const keypair: SignerKeyPair = kc.getKey(sigidx.getSource()) const signval: Buffer = keypair.sign(msg) const sig: Signature = new Signature() sig.fromBuffer(signval) diff --git a/src/apis/evm/importtx.ts b/src/apis/evm/importtx.ts index be154f2e4..25771c396 100644 --- a/src/apis/evm/importtx.ts +++ b/src/apis/evm/importtx.ts @@ -11,9 +11,9 @@ import { EVMOutput } from "./outputs" import { TransferableInput } from "./inputs" import { EVMBaseTx } from "./basetx" import { SelectCredentialClass } from "./credentials" -import { Signature, SigIdx, Credential } from "../../common/credentials" +import { Credential, Signature, SigIdx } from "../../common/credentials" +import { SignerKeyChain, SignerKeyPair } from "../../common/keychain" import { StandardAmountInput } from "../../common/input" -import { KeyChain, KeyPair } from "./keychain" import { DefaultNetworkID } from "../../utils/constants" import { ChainIdError, @@ -47,7 +47,8 @@ export class ImportTx extends EVMBaseTx { "Buffer", "cb58" ), - importIns: this.importIns.map((i) => i.serialize(encoding)) + importIns: this.importIns.map((i) => i.serialize(encoding)), + outs: this.outs.map((o) => o.serialize(encoding)) } } deserialize(fields: object, encoding: SerializedEncoding = "hex") { @@ -182,7 +183,7 @@ export class ImportTx extends EVMBaseTx { * * @returns An array of [[Credential]]s */ - sign(msg: Buffer, kc: KeyChain): Credential[] { + sign(msg: Buffer, kc: SignerKeyChain): Credential[] { const creds: Credential[] = super.sign(msg, kc) this.importIns.forEach((importIn: TransferableInput) => { const cred: Credential = SelectCredentialClass( @@ -190,7 +191,7 @@ export class ImportTx extends EVMBaseTx { ) const sigidxs: SigIdx[] = importIn.getInput().getSigIdxs() sigidxs.forEach((sigidx: SigIdx) => { - const keypair: KeyPair = kc.getKey(sigidx.getSource()) + const keypair: SignerKeyPair = kc.getKey(sigidx.getSource()) const signval: Buffer = keypair.sign(msg) const sig: Signature = new Signature() sig.fromBuffer(signval) diff --git a/src/apis/evm/outputs.ts b/src/apis/evm/outputs.ts index da13f3d64..f28410139 100644 --- a/src/apis/evm/outputs.ts +++ b/src/apis/evm/outputs.ts @@ -11,11 +11,12 @@ import { StandardAmountOutput, StandardTransferableOutput } from "../../common/output" -import { SerializedEncoding } from "../../utils/serialization" +import { Serialization, SerializedEncoding } from "../../utils/serialization" import { EVMInput } from "./inputs" import { OutputIdError } from "../../utils/errors" const bintools: BinTools = BinTools.getInstance() +const serializer = Serialization.getInstance() /** * Takes a buffer representing the output and returns the proper Output instance. @@ -111,6 +112,19 @@ export class EVMOutput { protected amountValue: BN = new BN(0) protected assetID: Buffer = Buffer.alloc(32) + serialize(encoding: SerializedEncoding = "hex"): object { + return { + address: serializer.encoder(this.address, encoding, "Buffer", "hex"), + amount: serializer.encoder( + this.amount, + encoding, + "Buffer", + "decimalString" + ), + assetID: serializer.encoder(this.assetID, encoding, "Buffer", "cb58") + } + } + /** * Returns a function used to sort an array of [[EVMOutput]]s */ diff --git a/src/apis/evm/tx.ts b/src/apis/evm/tx.ts index 7d50dbc1b..938a337a5 100644 --- a/src/apis/evm/tx.ts +++ b/src/apis/evm/tx.ts @@ -7,12 +7,13 @@ import { Buffer } from "buffer/" import BinTools from "../../utils/bintools" import { EVMConstants } from "./constants" import { SelectCredentialClass } from "./credentials" -import { KeyChain, KeyPair } from "./keychain" import { Credential, EVMStandardTx, EVMStandardUnsignedTx, - MultisigKeyChain + MultisigKeyChain, + SignerKeyChain, + SignerKeyPair } from "../../common" import createHash from "create-hash" import { EVMBaseTx } from "./basetx" @@ -43,8 +44,8 @@ export const SelectTxClass = (txTypeID: number, ...args: any[]): EVMBaseTx => { } export class UnsignedTx extends EVMStandardUnsignedTx< - KeyPair, - KeyChain, + SignerKeyPair, + SignerKeyChain, EVMBaseTx > { protected _typeName = "UnsignedTx" @@ -80,7 +81,7 @@ export class UnsignedTx extends EVMStandardUnsignedTx< * * @returns A signed [[StandardTx]] */ - sign(kc: KeyChain): Tx { + sign(kc: SignerKeyChain): Tx { const txbuff: Buffer = this.toBuffer() const msg: Buffer = Buffer.from( createHash("sha256").update(txbuff).digest() @@ -93,7 +94,11 @@ export class UnsignedTx extends EVMStandardUnsignedTx< } } -export class Tx extends EVMStandardTx { +export class Tx extends EVMStandardTx< + SignerKeyPair, + SignerKeyChain, + UnsignedTx +> { protected _typeName = "Tx" protected _typeID = undefined