@decentstake/beaconchain-utils
v1.0.7
Published
Typescript Ethereum BeaconChain Utils
Downloads
6
Maintainers
Readme
BeaconChain Utils
This package contains typescript utilities for working with the Eth2 Beacon Chain, wrapping around @Lodestar and @Chainsafe packages.
Installation
npm install @decentstake/beaconchain-utils
Usage
- Sign and create a DepositData object with a BIP39 mnemonic:
import {
generateDepositData,
generatePackedDepositData,
parseAddressToBLS,
Validator,
SecretProvider
} from '@decentstake/beaconchain-utils';
import type {
IDepositData,
IDepositDataSignature,
IPackedDepositData
} from '@decentstake/beaconchain-utils/interfaces';
import { Chains } from '@decentstake/beaconchain-utils/chainParams';
const mnemonic: string = 'sister protect...'
const withdrawal_credential: Uint8Array = parseAddressToBLS('0x8FB4...');
const validatorIndex: number = 0;
// Create a new mnemonic.
const secretProvider = new SecretProvider();
// Or use an existing mnemonic.
const secretProvider = new SecretProvider(mnemonic);
const secretKey = secretProvider.deriveValidator(validatorIndex).secretKey;
const validator = new Validator(secretKey, validatorIndex, Chains.mainnet);
// Signs data for generating a deposit data object.
const depositDataSignature: IDepositDataSignature = await validator.signDepositData(
withdrawal_credential,
);
// Generates a deposit data object.
const depositData: IDepositData[] = generateDepositData([depositDataSignature]);
// `packed` version of `generateDepositData`.
const packedDepositData: IPackedDepositData = generatePackedDepositData([depositDataSignature]);
- Generate EIP 2335 keystores for a list of validator indices:
Generating 100 keystores takes about 90 seconds. May vary depending on your machine.
import { SecretProvider } from '@decentstake/beaconchain-utils';
import type { IKeystore, IKeystoreObject } from '@decentstake/beaconchain-utils/interfaces';
const password: string = "somePassword";
const mnemonic: string = 'sister protect...';
const startIndex = 0;
const numberOfValidators = 100; //ValidatorIndexes 0-99
// Create a new mnemonic.
const secretProvider = new SecretProvider();
// Or use an existing mnemonic.
const secretProvider = new SecretProvider(mnemonic);
const keystoreObject: IKeystoreObject = await secretProvider.generateKeystores(
startIndex,
numberOfValidators,
password
);
const keystores: Array<IKeystore> = keystoreObject.keystores;
const psw: string = keystoreObject.password;
//OR export keystore from a single validator
//...
const validator = new Validator(secretKey, validatorIndex, Chains.mainnet);
const keystore: IKeystore = await validator.generateKeystore(password);
- Derive a validator public and secret key from a BIP39 mnemonic:
import { SecretProvider, hexStringToBytes } from '@decentstake/beaconchain-utils';
import type { IValidatorKeyPair } from '@decentstake/beaconchain-utils/interfaces';
const mnemonic: string = 'sister protect...';
const startIndex = 0;
const numberOfValidators = 100; //ValidatorIndexes 0-99
// Create a new mnemonic.
const secretProvider = new SecretProvider();
// Or use an existing mnemonic.
const secretProvider = new SecretProvider(mnemonic);
//Derive a single validator.
const validator0KeyPair: IValidatorKeyPair = secretProvider.deriveValidator(0);
const validator0secretKey = validator0KeyPair.secretKey;
const validator0pubkey = validator0KeyPair.pubkey;
// OR Derive multiple validators.
// The nth validator has a secretKey secretKey[n] and a public key pubkey[n]
// Values are returned as hexstrings.
const { pubkeys, secretKeys } = secretProvider.exportValidators(startIndex, numberOfValidators);
const validator0secretKey = hexStringToBytes(secretKeys[0]);
const validator0pubkey = hexStringToBytes(pubkeys[0]);