Skip to content

Commit

Permalink
[EVM] ImportTx MultiSig
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed Apr 8, 2023
1 parent 365a0ad commit 061a083
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
13 changes: 10 additions & 3 deletions src/apis/evm/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import BinTools from "../../utils/bintools"
import { EVMConstants } from "./constants"
import { SelectCredentialClass } from "./credentials"
import { KeyChain, KeyPair } from "./keychain"
import { Credential } from "../../common/credentials"
import { EVMStandardTx, EVMStandardUnsignedTx } from "../../common/evmtx"
import {
Credential,
EVMStandardTx,
EVMStandardUnsignedTx,
MultisigKeyChain
} from "../../common"
import createHash from "create-hash"
import { EVMBaseTx } from "./basetx"
import { ImportTx } from "./importtx"
Expand Down Expand Up @@ -81,7 +85,10 @@ export class UnsignedTx extends EVMStandardUnsignedTx<
const msg: Buffer = Buffer.from(
createHash("sha256").update(txbuff).digest()
)
const creds: Credential[] = this.transaction.sign(msg, kc)
const creds: Credential[] =
kc instanceof MultisigKeyChain
? kc.getCredentials()
: this.transaction.sign(msg, kc)
return new Tx(this, creds)
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/apis/evm/utxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "./outputs"
import { EVMConstants } from "./constants"
import { EVMInput, SECPTransferInput, TransferableInput } from "./inputs"
import { Output } from "../../common/output"
import { Output, OutputOwners } from "../../common/output"
import { UnixNow } from "../../utils/helperfunctions"
import { StandardUTXO, StandardUTXOSet } from "../../common/utxos"
import { DefaultPlatformChainID } from "../../utils/constants"
Expand Down Expand Up @@ -339,6 +339,7 @@ export class UTXOSet extends StandardUTXOSet<UTXO> {
let ins: TransferableInput[] = []
let outs: EVMOutput[] = []
let feepaid: BN = new BN(0)
let owners: OutputOwners[] = []

if (typeof fee === "undefined") {
fee = zero.clone()
Expand Down Expand Up @@ -378,6 +379,7 @@ export class UTXOSet extends StandardUTXOSet<UTXO> {
)
const from: Buffer[] = output.getAddresses()
const spenders: Buffer[] = output.getSpenders(from)

spenders.forEach((spender: Buffer): void => {
const idx: number = output.getAddressIdx(spender)
if (idx === -1) {
Expand All @@ -389,6 +391,13 @@ export class UTXOSet extends StandardUTXOSet<UTXO> {
xferin.getInput().addSignatureIdx(idx, spender)
})
ins.push(xferin)
owners.push(
new OutputOwners(
output.getAddresses(),
output.getLocktime(),
output.getThreshold()
)
)

if (map.has(assetID)) {
infeeamount = infeeamount.add(new BN(map.get(assetID)))
Expand Down Expand Up @@ -418,6 +427,7 @@ export class UTXOSet extends StandardUTXOSet<UTXO> {
outs,
fee
)
importTx.setOutputOwners(owners)
return new UnsignedTx(importTx)
}

Expand Down
38 changes: 30 additions & 8 deletions src/common/evmtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { Buffer } from "buffer/"
import BinTools from "../utils/bintools"
import { Credential } from "./credentials"
import BN from "bn.js"
import { StandardKeyChain, StandardKeyPair } from "./keychain"
import { SignerKeyChain, SignerKeyPair } from "./keychain"
import { StandardAmountInput, StandardTransferableInput } from "./input"
import { StandardAmountOutput, StandardTransferableOutput } from "./output"
import {
OutputOwners,
StandardAmountOutput,
StandardTransferableOutput
} from "./output"
import { DefaultNetworkID } from "../utils/constants"
import {
Serializable,
Expand All @@ -27,11 +31,12 @@ const serializer: Serialization = Serialization.getInstance()
* Class representing a base for all transactions.
*/
export abstract class EVMStandardBaseTx<
KPClass extends StandardKeyPair,
KCClass extends StandardKeyChain<KPClass>
KPClass extends SignerKeyPair,
KCClass extends SignerKeyChain
> extends Serializable {
protected _typeName = "EVMStandardBaseTx"
protected _typeID = undefined
protected _outputOwners: OutputOwners[] = undefined

serialize(encoding: SerializedEncoding = "hex"): object {
let fields: object = super.serialize(encoding)
Expand Down Expand Up @@ -78,6 +83,23 @@ export abstract class EVMStandardBaseTx<
*/
abstract getTxType(): number

/**
* @returns The outputOwners of inputs, one per input
*/
getOutputOwners(): OutputOwners[] {
if (this._outputOwners) {
return [...this._outputOwners]
}
return []
}

/**
* @params The outputOwners of inputs, one per input
*/
setOutputOwners(owners: OutputOwners[]) {
this._outputOwners = [...owners]
}

/**
* Returns the NetworkID as a number
*/
Expand Down Expand Up @@ -137,8 +159,8 @@ export abstract class EVMStandardBaseTx<
* Class representing an unsigned transaction.
*/
export abstract class EVMStandardUnsignedTx<
KPClass extends StandardKeyPair,
KCClass extends StandardKeyChain<KPClass>,
KPClass extends SignerKeyPair,
KCClass extends SignerKeyChain,
SBTx extends EVMStandardBaseTx<KPClass, KCClass>
> extends Serializable {
protected _typeName = "StandardUnsignedTx"
Expand Down Expand Up @@ -281,8 +303,8 @@ export abstract class EVMStandardUnsignedTx<
* Class representing a signed transaction.
*/
export abstract class EVMStandardTx<
KPClass extends StandardKeyPair,
KCClass extends StandardKeyChain<KPClass>,
KPClass extends SignerKeyPair,
KCClass extends SignerKeyChain,
SUBTx extends EVMStandardUnsignedTx<
KPClass,
KCClass,
Expand Down

0 comments on commit 061a083

Please sign in to comment.