Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tao): fix abstract-substrate implementation #5487

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 22 additions & 80 deletions modules/abstract-substrate/src/abstractSubstrateCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,33 @@ import * as _ from 'lodash';
import {
BaseCoin,
BitGoBase,
KeyPair,
MPCAlgorithm,
ParsedTransaction,
ParseTransactionOptions,
SignedTransaction,
SignTransactionOptions as BaseSignTransactionOptions,
VerifyAddressOptions,
VerifyTransactionOptions,
} from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin, CoinFamily } from '@bitgo/statics';
import { Interface, KeyPair as SubstrateKeyPair, Utils } from './lib';
import { BaseCoin as StaticsBaseCoin, CoinFamily, coins } from '@bitgo/statics';
import { KeyPair as SubstrateKeyPair } from './lib';
import utils from './lib/utils';
import { TransactionBuilderFactory } from './lib/transactionBuilderFactory';
import { SignTransactionOptions, VerifiedTransactionParameters } from './lib/iface';

const utils = Utils.default;

export const DEFAULT_SCAN_FACTOR = 20; // default number of receive addresses to scan for funds

export interface SignTransactionOptions extends BaseSignTransactionOptions {
txPrebuild: TransactionPrebuild;
prv: string;
}

export interface TransactionPrebuild {
txHex: string;
transaction: Interface.TxData;
}

export interface ExplainTransactionOptions {
txPrebuild: TransactionPrebuild;
publicKey: string;
feeInfo: {
fee: string;
};
}

export interface VerifiedTransactionParameters {
txHex: string;
prv: string;
}

export class SubstrateCoin extends BaseCoin {
export abstract class SubstrateCoin extends BaseCoin {
protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
readonly MAX_VALIDITY_DURATION = 2400;

protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo);

if (!staticsCoin) {
throw new Error('missing required constructor parameter staticsCoin');
}

this._staticsCoin = staticsCoin;
}

static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
return new SubstrateCoin(bitgo, staticsCoin);
}

/**
* Creates an instance of TransactionBuilderFactory for the coin specific sdk
*/
getBuilder(): any {
throw new Error('Method not implemented.');
getBuilder(): TransactionBuilderFactory {
return new TransactionBuilderFactory(coins.get(this.getChain()));
}

/** @inheritDoc **/
Expand Down Expand Up @@ -97,16 +62,8 @@ export class SubstrateCoin extends BaseCoin {
}

/** @inheritDoc **/
generateKeyPair(seed?: Buffer): KeyPair {
const keyPair = seed ? utils.keyPairFromSeed(new Uint8Array(seed)) : new SubstrateKeyPair();
const keys = keyPair.getKeys();
if (!keys.prv) {
throw new Error('Missing prv in key generation.');
}
return {
pub: keys.pub,
prv: keys.prv,
};
allowsAccountConsolidations(): boolean {
return true;
}

/** @inheritDoc **/
Expand All @@ -115,13 +72,8 @@ export class SubstrateCoin extends BaseCoin {
}

/** @inheritDoc **/
isWalletAddress(params: VerifyAddressOptions): Promise<boolean> {
throw new Error('Method not implemented');
}

/** @inheritDoc **/
parseTransaction(params: ParseTransactionOptions): Promise<ParsedTransaction> {
throw new Error('Method not implemented');
async parseTransaction(params: ParseTransactionOptions): Promise<ParsedTransaction> {
return {};
}

/** @inheritDoc **/
Expand All @@ -142,29 +94,16 @@ export class SubstrateCoin extends BaseCoin {

verifySignTransactionParams(params: SignTransactionOptions): VerifiedTransactionParameters {
const prv = params.prv;

const txHex = params.txPrebuild.txHex;

if (!txHex) {
throw new Error('missing txPrebuild parameter');
if (!txHex || typeof txHex !== 'string') {
throw new Error('invalid txHex parameter');
}

if (!_.isString(txHex)) {
throw new Error(`txPrebuild must be an object, got type ${typeof txHex}`);
if (!prv || typeof prv !== 'string') {
throw new Error('invalid prv parameter');
}

if (!prv) {
throw new Error('missing prv parameter to sign transaction');
}

if (!_.isString(prv)) {
throw new Error(`prv must be a string, got type ${typeof prv}`);
}

if (!_.has(params, 'pubs')) {
throw new Error('missing public key parameter to sign transaction');
}

return { txHex, prv };
}

Expand All @@ -174,12 +113,11 @@ export class SubstrateCoin extends BaseCoin {
const factory = this.getBuilder();
const txBuilder = factory.from(txHex);
const keyPair = new SubstrateKeyPair({ prv: prv });
const { referenceBlock, blockNumber, transactionVersion, sender } = params.txPrebuild.transaction;
const { referenceBlock, blockNumber, sender } = params.txPrebuild.transaction;

txBuilder
.validity({ firstValid: blockNumber, maxDuration: this.MAX_VALIDITY_DURATION })
.validity({ firstValid: blockNumber, maxDuration: this.getMaxValidityDurationBlocks() })
.referenceBlock(referenceBlock)
.version(transactionVersion)
.sender({ address: sender })
.sign({ key: keyPair.getKeys().prv });
const transaction = await txBuilder.build();
Expand All @@ -189,4 +127,8 @@ export class SubstrateCoin extends BaseCoin {
const signedTxHex = transaction.toBroadcastFormat();
return { txHex: signedTxHex };
}

abstract getMaxValidityDurationBlocks(): number;

abstract getSS58Format(): number;
}
Empty file.
15 changes: 15 additions & 0 deletions modules/abstract-substrate/src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BuildTransactionError } from '@bitgo/sdk-core';

export class AddressValidationError extends BuildTransactionError {
constructor(malformedAddress: string) {
super(`The address '${malformedAddress}' is not a well-formed address`);
this.name = AddressValidationError.name;
}
}

export class InvalidFeeError extends BuildTransactionError {
constructor(type?: string, expectedType?: string) {
super(`The specified type: "${type}" is not valid. Please provide the type: "${expectedType}"`);
this.name = InvalidFeeError.name;
}
}
Loading
Loading