From cb41794f6444c6cdaae0ba969c8ef87f45edf7f5 Mon Sep 17 00:00:00 2001 From: Luis Rojas Date: Mon, 25 Mar 2024 12:44:31 -0600 Subject: [PATCH] feat(avaxp-coin-sdk): add skeleton for permissionlessValidatorTx builder TICKET: CR-1073 --- .../lib/permissionlessValidatorTxBuilder.ts | 97 ++++++++++++++++--- .../src/lib/transactionBuilderFactory.ts | 10 ++ 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/modules/sdk-coin-avaxp/src/lib/permissionlessValidatorTxBuilder.ts b/modules/sdk-coin-avaxp/src/lib/permissionlessValidatorTxBuilder.ts index 77637b68a7..742c539ddf 100644 --- a/modules/sdk-coin-avaxp/src/lib/permissionlessValidatorTxBuilder.ts +++ b/modules/sdk-coin-avaxp/src/lib/permissionlessValidatorTxBuilder.ts @@ -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; @@ -17,7 +29,7 @@ export class PermissionlessValidatorTxBuilder { * @param coinConfig */ constructor(coinConfig: Readonly) { - // super(coinConfig); + super(coinConfig); const network = coinConfig.network as AvalancheNetwork; this._stakeAmount = new BN(network.minStake); } @@ -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; } @@ -170,4 +182,67 @@ export class PermissionlessValidatorTxBuilder { protected buildAvaxTransaction(): void { // TODO Implement } + + /** @inheritdoc */ + // protected async buildImplementation(): Promise { + protected async buildImplementation(): Promise { + // 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'); + } + } } diff --git a/modules/sdk-coin-avaxp/src/lib/transactionBuilderFactory.ts b/modules/sdk-coin-avaxp/src/lib/transactionBuilderFactory.ts index d1e7c72c6f..a39bb14aa1 100644 --- a/modules/sdk-coin-avaxp/src/lib/transactionBuilderFactory.ts +++ b/modules/sdk-coin-avaxp/src/lib/transactionBuilderFactory.ts @@ -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; @@ -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 *