Skip to content

Commit

Permalink
util: extract static constructors from WithdrawalRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 14, 2024
1 parent 242e801 commit 0f77391
Showing 1 changed file with 29 additions and 42 deletions.
71 changes: 29 additions & 42 deletions packages/util/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,6 @@ export class DepositRequest extends CLRequest<CLRequestType.Deposit> {
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<CLRequestType.Withdrawal> {
Expand All @@ -152,20 +139,6 @@ export class WithdrawalRequest extends CLRequest<CLRequestType.Withdrawal> {
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)

Expand All @@ -182,19 +155,6 @@ export class WithdrawalRequest extends CLRequest<CLRequestType.Withdrawal> {
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<CLRequestType.Consolidation> {
Expand Down Expand Up @@ -253,9 +213,9 @@ export class CLRequestFactory {
public static fromSerializedRequest(bytes: Uint8Array): CLRequest<CLRequestType> {
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:
Expand Down Expand Up @@ -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),
})
}

0 comments on commit 0f77391

Please sign in to comment.