Skip to content

Commit

Permalink
[EVM] MultiSig compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed Apr 9, 2023
1 parent 061a083 commit 195dcff
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
23 changes: 14 additions & 9 deletions src/apis/evm/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/apis/evm/basetx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +20,10 @@ const bintools: BinTools = BinTools.getInstance()
/**
* Class representing a base for all transactions.
*/
export class EVMBaseTx extends EVMStandardBaseTx<KeyPair, KeyChain> {
export class EVMBaseTx extends EVMStandardBaseTx<
SignerKeyPair,
SignerKeyChain
> {
protected _typeName = "BaseTx"
protected _typeID = undefined

Expand Down Expand Up @@ -63,7 +66,7 @@ export class EVMBaseTx extends EVMStandardBaseTx<KeyPair, KeyChain> {
* @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
}
Expand Down
8 changes: 4 additions & 4 deletions src/apis/evm/exporttx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions src/apis/evm/importtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -182,15 +183,15 @@ 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(
importIn.getInput().getCredentialID()
)
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)
Expand Down
16 changes: 15 additions & 1 deletion src/apis/evm/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
*/
Expand Down
17 changes: 11 additions & 6 deletions src/apis/evm/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -93,7 +94,11 @@ export class UnsignedTx extends EVMStandardUnsignedTx<
}
}

export class Tx extends EVMStandardTx<KeyPair, KeyChain, UnsignedTx> {
export class Tx extends EVMStandardTx<
SignerKeyPair,
SignerKeyChain,
UnsignedTx
> {
protected _typeName = "Tx"
protected _typeID = undefined

Expand Down

0 comments on commit 195dcff

Please sign in to comment.