1
- import * as _ from 'lodash' ;
2
1
import {
3
2
BaseCoin ,
4
3
BitGoBase ,
@@ -7,43 +6,17 @@ import {
7
6
ParsedTransaction ,
8
7
ParseTransactionOptions ,
9
8
SignedTransaction ,
10
- SignTransactionOptions as BaseSignTransactionOptions ,
11
9
VerifyAddressOptions ,
12
10
VerifyTransactionOptions ,
13
11
} 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' ;
43
17
44
18
export class SubstrateCoin extends BaseCoin {
45
19
protected readonly _staticsCoin : Readonly < StaticsBaseCoin > ;
46
- readonly MAX_VALIDITY_DURATION = 2400 ;
47
20
48
21
protected constructor ( bitgo : BitGoBase , staticsCoin ?: Readonly < StaticsBaseCoin > ) {
49
22
super ( bitgo ) ;
@@ -55,10 +28,6 @@ export class SubstrateCoin extends BaseCoin {
55
28
this . _staticsCoin = staticsCoin ;
56
29
}
57
30
58
- static createInstance ( bitgo : BitGoBase , staticsCoin ?: Readonly < StaticsBaseCoin > ) : BaseCoin {
59
- return new SubstrateCoin ( bitgo , staticsCoin ) ;
60
- }
61
-
62
31
/**
63
32
* Creates an instance of TransactionBuilderFactory for the coin specific sdk
64
33
*/
@@ -98,7 +67,7 @@ export class SubstrateCoin extends BaseCoin {
98
67
99
68
/** @inheritDoc **/
100
69
generateKeyPair ( seed ?: Buffer ) : KeyPair {
101
- const keyPair = seed ? utils . keyPairFromSeed ( new Uint8Array ( seed ) ) : new SubstrateKeyPair ( ) ;
70
+ const keyPair = seed ? new SubstrateKeyPair ( { seed } ) : new SubstrateKeyPair ( ) ;
102
71
const keys = keyPair . getKeys ( ) ;
103
72
if ( ! keys . prv ) {
104
73
throw new Error ( 'Missing prv in key generation.' ) ;
@@ -120,8 +89,8 @@ export class SubstrateCoin extends BaseCoin {
120
89
}
121
90
122
91
/** @inheritDoc **/
123
- parseTransaction ( params : ParseTransactionOptions ) : Promise < ParsedTransaction > {
124
- throw new Error ( 'Method not implemented' ) ;
92
+ async parseTransaction ( params : ParseTransactionOptions ) : Promise < ParsedTransaction > {
93
+ return { } ;
125
94
}
126
95
127
96
/** @inheritDoc **/
@@ -141,28 +110,15 @@ export class SubstrateCoin extends BaseCoin {
141
110
}
142
111
143
112
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 ;
147
115
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 } ` ) ;
150
118
}
151
119
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 } ` ) ;
166
122
}
167
123
168
124
return { txHex, prv } ;
@@ -177,7 +133,7 @@ export class SubstrateCoin extends BaseCoin {
177
133
const { referenceBlock, blockNumber, transactionVersion, sender } = params . txPrebuild . transaction ;
178
134
179
135
txBuilder
180
- . validity ( { firstValid : blockNumber , maxDuration : this . MAX_VALIDITY_DURATION } )
136
+ . validity ( { firstValid : blockNumber , maxDuration : this . getMaxValidityDurationBlocks ( ) } )
181
137
. referenceBlock ( referenceBlock )
182
138
. version ( transactionVersion )
183
139
. sender ( { address : sender } )
@@ -189,4 +145,26 @@ export class SubstrateCoin extends BaseCoin {
189
145
const signedTxHex = transaction . toBroadcastFormat ( ) ;
190
146
return { txHex : signedTxHex } ;
191
147
}
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
+ }
192
170
}
0 commit comments