-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3feabc
commit 06c3ad4
Showing
6 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Address } from './address.js' | ||
import { bigIntToHex, bytesToHex, toBytes } from './bytes.js' | ||
import { BIGINT_0 } from './constants.js' | ||
import { TypeOutput, toType } from './types.js' | ||
|
||
import type { AddressLike, BigIntLike, BytesLike, PrefixedHexString } from './types.js' | ||
|
||
/** | ||
* Flexible input data type for EIP-4895 withdrawal data with amount in Gwei to | ||
* match CL representation and for eventual ssz withdrawalsRoot | ||
*/ | ||
export type ExitData = { | ||
address: Uint8Array | ||
validatorPubkey: Uint8Array | ||
} | ||
|
||
/** | ||
* JSON RPC interface for EIP-7002 exit data | ||
*/ | ||
export interface JsonRpcExit { | ||
address: PrefixedHexString // 20 bytes | ||
validatorPubkey: PrefixedHexString // 48 bytes | ||
} | ||
|
||
export type ExitBytes = [Uint8Array, Uint8Array] | ||
|
||
/** | ||
* Representation of EIP-7002 exit data | ||
*/ | ||
export class Exit { | ||
/** | ||
* This constructor assigns and validates the values. | ||
* Use the static factory methods to assist in creating a Withdrawal object from varying data types. | ||
* Its amount is in Gwei to match CL representation and for eventual ssz withdrawalsRoot | ||
*/ | ||
constructor(public readonly address: Uint8Array, public readonly validatorPubkey: Uint8Array) {} | ||
|
||
public static fromExitData(exitData: ExitData) { | ||
const { address, validatorPubkey } = exitData | ||
|
||
const vPubKey = toType(validatorPubkey, TypeOutput.Uint8Array) | ||
const addr = toType(address, TypeOutput.Uint8Array) | ||
|
||
return new Exit(addr, vPubKey) | ||
} | ||
|
||
public static fromValuesArray(exitArray: ExitBytes) { | ||
if (exitArray.length !== 2) { | ||
throw Error(`Invalid exitArray length expected=2 actual=${exitArray.length}`) | ||
} | ||
const [address, validatorPubkey] = exitArray | ||
return Exit.fromExitData({ validatorPubkey, address }) | ||
} | ||
|
||
/** | ||
* Convert a withdrawal to a buffer array | ||
* @param withdrawal the withdrawal to convert | ||
* @returns buffer array of the withdrawal | ||
*/ | ||
public static toBytesArray(exit: Exit | ExitData): ExitBytes { | ||
const { validatorPubkey, address } = exit | ||
|
||
return [address, validatorPubkey] | ||
} | ||
|
||
raw() { | ||
return Exit.toBytesArray(this) | ||
} | ||
|
||
toValue() { | ||
return { | ||
address: this.address, | ||
validatorPubkey: this.validatorPubkey, | ||
} | ||
} | ||
|
||
toJSON() { | ||
return { | ||
address: this.address.toString(), | ||
validatorPubkey: bytesToHex(this.validatorPubkey), | ||
} | ||
} | ||
} |
Oops, something went wrong.