From 0f77391a25ee1d9960774ef075c693096ca3ca29 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 12:10:19 -0600 Subject: [PATCH] util: extract static constructors from WithdrawalRequest --- packages/util/src/request.ts | 71 +++++++++++++++--------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index 99832b3154f..a809485a1de 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -128,19 +128,6 @@ export class DepositRequest extends CLRequest { index: bigIntToHex(this.index), } } - - public static deserialize(bytes: Uint8Array): DepositRequest { - const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode( - bytes.slice(1), - ) as [Uint8Array, Uint8Array, Uint8Array, Uint8Array, Uint8Array] - return createDepositRequest({ - pubkey, - withdrawalCredentials, - amount: bytesToBigInt(amount), - signature, - index: bytesToBigInt(index), - }) - } } export class WithdrawalRequest extends CLRequest { @@ -152,20 +139,6 @@ export class WithdrawalRequest extends CLRequest { super(CLRequestType.Withdrawal) } - public static fromRequestData(withdrawalData: WithdrawalRequestData): WithdrawalRequest { - const { sourceAddress, validatorPubkey, amount } = withdrawalData - return new WithdrawalRequest(sourceAddress, validatorPubkey, amount) - } - - public static fromJSON(jsonData: WithdrawalRequestV1): WithdrawalRequest { - const { sourceAddress, validatorPubkey, amount } = jsonData - return this.fromRequestData({ - sourceAddress: hexToBytes(sourceAddress), - validatorPubkey: hexToBytes(validatorPubkey), - amount: hexToBigInt(amount), - }) - } - serialize() { const amountBytes = this.amount === BIGINT_0 ? new Uint8Array() : bigIntToBytes(this.amount) @@ -182,19 +155,6 @@ export class WithdrawalRequest extends CLRequest { amount: bigIntToHex(this.amount), } } - - public static deserialize(bytes: Uint8Array): WithdrawalRequest { - const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes.slice(1)) as [ - Uint8Array, - Uint8Array, - Uint8Array, - ] - return this.fromRequestData({ - sourceAddress, - validatorPubkey, - amount: bytesToBigInt(amount), - }) - } } export class ConsolidationRequest extends CLRequest { @@ -253,9 +213,9 @@ export class CLRequestFactory { public static fromSerializedRequest(bytes: Uint8Array): CLRequest { switch (bytes[0]) { case CLRequestType.Deposit: - return DepositRequest.deserialize(bytes) + return createDepositRequestFromBytes(bytes) case CLRequestType.Withdrawal: - return WithdrawalRequest.deserialize(bytes) + return createWithDrawalRequestFromBytes(bytes) case CLRequestType.Consolidation: return ConsolidationRequest.deserialize(bytes) default: @@ -296,3 +256,30 @@ export function createDepositRequestFromBytes(bytes: Uint8Array): DepositRequest index: bytesToBigInt(index), }) } + +export function createWithdrawalRequest(withdrawalData: WithdrawalRequestData): WithdrawalRequest { + const { sourceAddress, validatorPubkey, amount } = withdrawalData + return new WithdrawalRequest(sourceAddress, validatorPubkey, amount) +} + +export function createWithdrawalRequestFromJSON(jsonData: WithdrawalRequestV1): WithdrawalRequest { + const { sourceAddress, validatorPubkey, amount } = jsonData + return createWithdrawalRequest({ + sourceAddress: hexToBytes(sourceAddress), + validatorPubkey: hexToBytes(validatorPubkey), + amount: hexToBigInt(amount), + }) +} + +export function createWithDrawalRequestFromBytes(bytes: Uint8Array): WithdrawalRequest { + const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes.slice(1)) as [ + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createWithdrawalRequest({ + sourceAddress, + validatorPubkey, + amount: bytesToBigInt(amount), + }) +}