Skip to content

Commit de3da12

Browse files
Merge pull request #5514 from BitGo/WIN-4302-2
fix(tao): fix abstract-substrate implementation
2 parents 67ff47c + 1c986da commit de3da12

38 files changed

+629
-3148
lines changed

modules/abstract-substrate/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@
4646
"@polkadot/util-crypto": "13.3.1",
4747
"@substrate/txwrapper-core": "7.5.2",
4848
"@substrate/txwrapper-polkadot": "7.5.2",
49+
"@substrate/txwrapper-registry": "7.5.3",
50+
"bignumber.js": "^9.1.2",
4951
"bs58": "^4.0.1",
5052
"hi-base32": "^0.5.1",
53+
"joi": "^17.4.0",
5154
"lodash": "^4.17.15",
5255
"tweetnacl": "^1.0.3"
5356
},

modules/abstract-substrate/src/abstractSubstrateCoin.ts

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as _ from 'lodash';
21
import {
32
BaseCoin,
43
BitGoBase,
@@ -7,43 +6,17 @@ import {
76
ParsedTransaction,
87
ParseTransactionOptions,
98
SignedTransaction,
10-
SignTransactionOptions as BaseSignTransactionOptions,
119
VerifyAddressOptions,
1210
VerifyTransactionOptions,
1311
} from '@bitgo/sdk-core';
14-
import { BaseCoin as StaticsBaseCoin, CoinFamily } from '@bitgo/statics';
15-
import { Interface, KeyPair as SubstrateKeyPair, Utils } from './lib';
16-
17-
const utils = Utils.default;
18-
19-
export const DEFAULT_SCAN_FACTOR = 20; // default number of receive addresses to scan for funds
20-
21-
export interface SignTransactionOptions extends BaseSignTransactionOptions {
22-
txPrebuild: TransactionPrebuild;
23-
prv: string;
24-
}
25-
26-
export interface TransactionPrebuild {
27-
txHex: string;
28-
transaction: Interface.TxData;
29-
}
30-
31-
export interface ExplainTransactionOptions {
32-
txPrebuild: TransactionPrebuild;
33-
publicKey: string;
34-
feeInfo: {
35-
fee: string;
36-
};
37-
}
38-
39-
export interface VerifiedTransactionParameters {
40-
txHex: string;
41-
prv: string;
42-
}
12+
import { CoinFamily, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
13+
import { KeyPair as SubstrateKeyPair } from './lib';
14+
import { DEFAULT_SUBSTRATE_PREFIX } from './lib/constants';
15+
import { SignTransactionOptions, VerifiedTransactionParameters } from './lib/iface';
16+
import utils from './lib/utils';
4317

4418
export class SubstrateCoin extends BaseCoin {
4519
protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
46-
readonly MAX_VALIDITY_DURATION = 2400;
4720

4821
protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
4922
super(bitgo);
@@ -55,10 +28,6 @@ export class SubstrateCoin extends BaseCoin {
5528
this._staticsCoin = staticsCoin;
5629
}
5730

58-
static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
59-
return new SubstrateCoin(bitgo, staticsCoin);
60-
}
61-
6231
/**
6332
* Creates an instance of TransactionBuilderFactory for the coin specific sdk
6433
*/
@@ -98,7 +67,7 @@ export class SubstrateCoin extends BaseCoin {
9867

9968
/** @inheritDoc **/
10069
generateKeyPair(seed?: Buffer): KeyPair {
101-
const keyPair = seed ? utils.keyPairFromSeed(new Uint8Array(seed)) : new SubstrateKeyPair();
70+
const keyPair = seed ? new SubstrateKeyPair({ seed }) : new SubstrateKeyPair();
10271
const keys = keyPair.getKeys();
10372
if (!keys.prv) {
10473
throw new Error('Missing prv in key generation.');
@@ -120,8 +89,8 @@ export class SubstrateCoin extends BaseCoin {
12089
}
12190

12291
/** @inheritDoc **/
123-
parseTransaction(params: ParseTransactionOptions): Promise<ParsedTransaction> {
124-
throw new Error('Method not implemented');
92+
async parseTransaction(params: ParseTransactionOptions): Promise<ParsedTransaction> {
93+
return {};
12594
}
12695

12796
/** @inheritDoc **/
@@ -141,28 +110,15 @@ export class SubstrateCoin extends BaseCoin {
141110
}
142111

143112
verifySignTransactionParams(params: SignTransactionOptions): VerifiedTransactionParameters {
144-
const prv = params.prv;
145-
146-
const txHex = params.txPrebuild.txHex;
113+
const prv = params?.prv;
114+
const txHex = params?.txPrebuild?.txHex;
147115

148-
if (!txHex) {
149-
throw new Error('missing txPrebuild parameter');
116+
if (typeof txHex !== 'string') {
117+
throw new Error(`txHex must be string, got type ${typeof txHex}`);
150118
}
151119

152-
if (!_.isString(txHex)) {
153-
throw new Error(`txPrebuild must be an object, got type ${typeof txHex}`);
154-
}
155-
156-
if (!prv) {
157-
throw new Error('missing prv parameter to sign transaction');
158-
}
159-
160-
if (!_.isString(prv)) {
161-
throw new Error(`prv must be a string, got type ${typeof prv}`);
162-
}
163-
164-
if (!_.has(params, 'pubs')) {
165-
throw new Error('missing public key parameter to sign transaction');
120+
if (typeof prv !== 'string') {
121+
throw new Error(`prv must be string, got type ${typeof prv}`);
166122
}
167123

168124
return { txHex, prv };
@@ -177,7 +133,7 @@ export class SubstrateCoin extends BaseCoin {
177133
const { referenceBlock, blockNumber, transactionVersion, sender } = params.txPrebuild.transaction;
178134

179135
txBuilder
180-
.validity({ firstValid: blockNumber, maxDuration: this.MAX_VALIDITY_DURATION })
136+
.validity({ firstValid: blockNumber, maxDuration: this.getMaxValidityDurationBlocks() })
181137
.referenceBlock(referenceBlock)
182138
.version(transactionVersion)
183139
.sender({ address: sender })
@@ -189,4 +145,26 @@ export class SubstrateCoin extends BaseCoin {
189145
const signedTxHex = transaction.toBroadcastFormat();
190146
return { txHex: signedTxHex };
191147
}
148+
149+
/**
150+
* Retrieves the address format for the substrate coin.
151+
*
152+
* @returns {number} The address format as a number.
153+
*/
154+
protected getAddressFormat(): number {
155+
return DEFAULT_SUBSTRATE_PREFIX;
156+
}
157+
158+
/**
159+
* Retrieves the maximum validity duration in blocks.
160+
*
161+
* This method is intended to be overridden by subclasses to provide the specific
162+
* maximum validity duration for different types of Substrate-based coins.
163+
*
164+
* @returns {number} The maximum validity duration in blocks.
165+
* @throws {Error} If the method is not implemented by the subclass.
166+
*/
167+
protected getMaxValidityDurationBlocks(): number {
168+
throw new Error('Method not implemented.');
169+
}
192170
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_SUBSTRATE_PREFIX = 42;

0 commit comments

Comments
 (0)