Skip to content

Commit

Permalink
feat(avaxp-coin-sdk): add skeleton for permissionlessValidatorTx builder
Browse files Browse the repository at this point in the history
TICKET: CR-1073
  • Loading branch information
CapnMigraine committed Mar 25, 2024
1 parent 676f492 commit cb41794
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
97 changes: 86 additions & 11 deletions modules/sdk-coin-avaxp/src/lib/permissionlessValidatorTxBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import {
BaseAddress,
BaseKey,
BaseTransaction,
BaseTransactionBuilder,
BuildTransactionError,
TransactionType,
} from '@bitgo/sdk-core';
import { AvalancheNetwork, BaseCoin as CoinConfig } from '@bitgo/statics';
import { BinTools, BN } from 'avalanche';
import { AddPermissionlessValidatorTx } from 'bitgo-aaron-avalanchejs/dist/serializable/pvm';
import { Tx } from './iface';
import { Transaction } from './transaction';
import { KeyPair } from './keyPair';
import utils from './utils';
import BigNumber from 'bignumber.js';

export class PermissionlessValidatorTxBuilder {
export class PermissionlessValidatorTxBuilder extends BaseTransactionBuilder {
private _transaction: BaseTransaction;
protected _nodeID: string;
protected _blsKey: string;
protected _blsPublicKey: string;
protected _blsSignature: string;
protected _startTime: BN;
protected _endTime: BN;
Expand All @@ -17,7 +29,7 @@ export class PermissionlessValidatorTxBuilder {
* @param coinConfig
*/
constructor(coinConfig: Readonly<CoinConfig>) {
// super(coinConfig);
super(coinConfig);
const network = coinConfig.network as AvalancheNetwork;
this._stakeAmount = new BN(network.minStake);
}
Expand All @@ -43,20 +55,20 @@ export class PermissionlessValidatorTxBuilder {
*
* @param nodeID
*/
nodeID(value: string): this {
this.validateNodeID(value);
this._nodeID = value;
nodeID(nodeID: string): this {
this.validateNodeID(nodeID);
this._nodeID = nodeID;
return this;
}

/**
*
* @param blsKey
* @param blsPublicKey
*/
blsKey(blsKey: string): this {
blsPublicKey(blsPublicKey: string): this {
// TODO add
// this.validateBlsKey(blsKey);
this._blsKey = blsKey;
// this.validateBlsKey(blsPublicKey);
this._blsPublicKey = blsPublicKey;
return this;
}

Expand Down Expand Up @@ -170,4 +182,67 @@ export class PermissionlessValidatorTxBuilder {
protected buildAvaxTransaction(): void {
// TODO Implement
}

/** @inheritdoc */
// protected async buildImplementation(): Promise<Transaction> {
protected async buildImplementation(): Promise<BaseTransaction> {
// TODO Implement
return this.transaction;
}

/** @inheritdoc */
protected fromImplementation(rawTransaction: string): BaseTransaction {
// TODO Implement
return this.transaction;
}

/** @inheritdoc */
protected signImplementation({ key }: BaseKey): BaseTransaction {
// TODO Implement
return this.transaction;
}

/** @inheritdoc */
validateAddress(address: BaseAddress, addressFormat?: string): void {
if (!utils.isValidAddress(address.address)) {
throw new BuildTransactionError('Invalid address');
}
}

/** @inheritdoc */
protected get transaction(): BaseTransaction {
return this._transaction;
}

protected set transaction(transaction: BaseTransaction) {
this._transaction = transaction;
}

/** @inheritdoc */
validateKey({ key }: BaseKey): void {
if (!new KeyPair({ prv: key })) {
throw new BuildTransactionError('Invalid key');
}
}

/**
* Check the raw transaction has a valid format in the blockchain context, throw otherwise.
*
* @param rawTransaction Transaction in any format
*/
validateRawTransaction(rawTransaction: string): void {
utils.validateRawTransaction(rawTransaction);
}

/** @inheritdoc */
validateTransaction(transaction?: Transaction): void {
// throw new NotImplementedError('validateTransaction not implemented');
}

/** @inheritdoc */
validateValue(value: BigNumber): void {
if (value.isLessThan(0)) {
throw new BuildTransactionError('Value cannot be less than zero');
}
}
}
10 changes: 10 additions & 0 deletions modules/sdk-coin-avaxp/src/lib/transactionBuilderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ExportTxBuilder } from './exportTxBuilder';
import { ImportTxBuilder } from './importTxBuilder';
import { ImportInCTxBuilder } from './importInCTxBuilder';
import { ExportInCTxBuilder } from './exportInCTxBuilder';
import { PermissionlessValidatorTxBuilder } from './permissionlessValidatorTxBuilder';

export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
protected recoverSigner = false;
Expand Down Expand Up @@ -78,6 +79,15 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
return new ValidatorTxBuilder(this._coinConfig);
}

/**
* Initialize Permissionless Validator builder
*
* @returns {PermissionlessValidatorTxBuilder} the builder initialized
*/
getPermissionlessValidatorTxBuilder(): PermissionlessValidatorTxBuilder {
return new PermissionlessValidatorTxBuilder(this._coinConfig);
}

/**
* Export Cross chain transfer
*
Expand Down

0 comments on commit cb41794

Please sign in to comment.