@@ -19,7 +19,7 @@ import { TRANSACTION_TYPE_NUMBER } from './prepareTx';
1919const { base58Encode, blake2b, verifySignature } = libs . crypto ;
2020
2121export class Signable {
22-
22+
2323 public readonly type : SIGN_TYPE ;
2424 private readonly _forSign : TSignData ;
2525 private readonly _adapter : Adapter ;
@@ -28,75 +28,79 @@ export class Signable {
2828 private _signPromise : Promise < string > | undefined ;
2929 private _preparedData : any ;
3030 private _proofs : Array < string > = [ ] ;
31-
32-
31+
32+
3333 constructor ( forSign : TSignData , adapter : Adapter ) {
3434 const networkCode = adapter . getNetworkByte ( ) ;
3535 this . _forSign = { ...forSign } ;
3636 this . type = forSign . type ;
3737 this . _adapter = adapter ;
3838 const prepareMap = getValidateSchema ( networkCode ) [ forSign . type ] ;
39-
39+
4040 if ( ! prepareMap ) {
4141 throw new SignError ( `Can't find prepare api for tx type "${ forSign . type } "!` , ERRORS . UNKNOWN_SIGN_TYPE ) ;
4242 }
43-
43+
4444 this . _forSign . data . timestamp = new Date ( this . _forSign . data . timestamp || Date . now ( ) ) . getTime ( ) ;
45-
45+
4646 if ( this . _forSign . data . proofs ) {
4747 this . _proofs = this . _forSign . data . proofs . slice ( ) ;
4848 }
49-
49+
5050 const availableVersions = adapter . getSignVersions ( ) [ forSign . type ] ;
51-
51+
5252 if ( availableVersions . length === 0 ) {
5353 throw new SignError ( `Can\'t sign data with type ${ this . type } ` , ERRORS . NO_SUPPORTED_VERSIONS ) ;
5454 }
55-
55+
5656 if ( isEmpty ( this . _forSign . data . version ) ) {
5757 this . _forSign . data . version = last ( availableVersions ) ;
5858 }
59-
59+
6060 const version = this . _forSign . data . version ;
61-
61+
6262 if ( ! availableVersions . includes ( version ) ) {
6363 throw new SignError ( `Can\'t sign data with type "${ this . type } " and version "${ version } "` , ERRORS . VERSION_IS_NOT_SUPPORTED ) ;
6464 }
65-
65+
6666 if ( ! SIGN_TYPES [ forSign . type as SIGN_TYPE ] . getBytes [ version ] ) {
6767 throw new SignError ( `Can't find prepare api for tx type "${ forSign . type } " with version ${ version } !` , ERRORS . VERSION_IS_NOT_SUPPORTED ) ;
6868 }
69-
69+
7070 this . _signMethod = SIGN_TYPES [ forSign . type ] . adapter ;
71-
71+
7272 try {
7373 this . _preparedData = prepare . signSchema ( prepareMap ) ( this . _forSign . data , true ) ;
7474 } catch ( e ) {
7575 throw new SignError ( e . message , ERRORS . VALIDATION_FAILED ) ;
7676 }
77-
78- this . _bytePromise = this . getSignData ( )
77+
78+ const bytePromise = this . getSignData ( )
7979 . then ( signData => SIGN_TYPES [ forSign . type ] . getBytes [ version ] ( signData ) ) ;
80+
81+ bytePromise . catch ( ( ) => null ) ;
82+
83+ this . _bytePromise = bytePromise ;
8084 }
81-
85+
8286 public async getOrderFee ( config : IFeeConfig , minOrderFee : BigNumber , hasMatcherScript : boolean , smartAssetIdList ?: Array < string > ) {
8387 if ( this . _forSign . type === SIGN_TYPE . CREATE_ORDER ) {
8488 const currentFee = currentCreateOrderFactory ( config , minOrderFee ) ;
8589 return currentFee ( await this . getDataForApi ( ) , hasMatcherScript , smartAssetIdList )
8690 }
8791 }
88-
92+
8993 public async getFee ( config : IFeeConfig , hasScript : boolean , smartAssetIdList ?: Array < string > ) {
9094 const currentFee = currentFeeFactory ( config ) ;
9195 const txData = await this . getSignData ( ) ;
9296 const bytes = await this . getBytes ( ) ;
9397 return currentFee ( txData , bytes , hasScript , smartAssetIdList ) ;
9498 }
95-
99+
96100 public getTxData ( ) : TSignData [ 'data' ] {
97101 return { ...this . _forSign . data } ;
98102 }
99-
103+
100104 public async getSignData ( ) {
101105 const senderPublicKey = await this . _adapter . getPublicKey ( ) ;
102106 const sender = await this . _adapter . getAddress ( ) ;
@@ -112,13 +116,13 @@ export class Signable {
112116
113117 return signData || dataForBytes ;
114118 }
115-
119+
116120 public async getAssetIds ( ) : Promise < Array < string > > {
117121 const transaction = await this . getSignData ( ) ;
118122 const hash = Object . create ( null ) ;
119123 hash [ WAVES_ID ] = true ;
120124 hash [ normalizeAssetId ( transaction . feeAssetId ) ] = true ;
121-
125+
122126 switch ( transaction . type ) {
123127 case SIGN_TYPE . CREATE_ORDER :
124128 hash [ normalizeAssetId ( transaction . matcherFeeAssetId ) ] = true ;
@@ -146,10 +150,10 @@ export class Signable {
146150 }
147151 return Object . keys ( hash ) ;
148152 }
149-
153+
150154 public sign2fa ( options : ISign2faOptions ) : Promise < Signable > {
151155 const code = options . code ;
152-
156+
153157 return this . _adapter . getAddress ( )
154158 . then ( address => {
155159 return options . request ( {
@@ -160,49 +164,49 @@ export class Signable {
160164 } )
161165 . then ( signature => {
162166 this . _proofs . push ( signature ) ;
163-
167+
164168 return this ;
165169 } ) ;
166170 }
167-
171+
168172 public addProof ( signature : string ) : Signable {
169173 if ( this . _proofs . indexOf ( signature ) !== - 1 ) {
170174 this . _proofs . push ( signature ) ;
171175 }
172-
176+
173177 return this ;
174178 }
175-
179+
176180 public getHash ( ) {
177181 return this . _bytePromise . then ( bytes => base58Encode ( blake2b ( bytes ) ) ) ;
178182 }
179-
183+
180184 public getId ( ) : Promise < string > {
181185 return this . _bytePromise . then ( bytes => {
182186 const byteArr = Array . from ( bytes ) ;
183-
187+
184188 if ( bytes [ 0 ] === 10 ) {
185189 bytes = new Uint8Array ( [ byteArr [ 0 ] , ...byteArr . slice ( 36 , - 16 ) ] )
186190 }
187-
191+
188192 return base58Encode ( blake2b ( bytes ) )
189193 } ) ;
190194 }
191-
195+
192196 public sign ( ) : Promise < Signable > {
193197 this . _makeSignPromise ( ) ;
194198 return ( this . _signPromise as Promise < string > ) . then ( ( ) => this ) ;
195199 }
196-
200+
197201 public getSignature ( ) : Promise < string > {
198202 this . _makeSignPromise ( ) ;
199203 return ( this . _signPromise as Promise < string > ) ;
200204 }
201-
205+
202206 public getBytes ( ) {
203207 return this . _bytePromise ;
204208 }
205-
209+
206210 public getMyProofs ( ) : Promise < Array < string > > {
207211 return Promise . all ( [
208212 this . getBytes ( ) ,
@@ -217,11 +221,11 @@ export class Signable {
217221 } ) ;
218222 } ) ;
219223 }
220-
224+
221225 public hasMySignature ( ) : Promise < boolean > {
222226 return this . getMyProofs ( ) . then ( proofs => ! ! proofs . length ) ;
223227 }
224-
228+
225229 public addMyProof ( ) : Promise < string > {
226230 return this . hasMySignature ( ) . then ( hasMySignature => {
227231 if ( ! hasMySignature ) {
@@ -234,21 +238,21 @@ export class Signable {
234238 }
235239 } ) ;
236240 }
237-
241+
238242 public async getDataForApi ( needSign = true ) {
239243 const data = await this . getSignData ( ) ;
240244 if ( needSign ) {
241245 await this . addMyProof ( ) ;
242246 }
243247 const proofs = ( this . _proofs || [ ] ) . slice ( ) ;
244-
248+
245249 try {
246250 return convert ( { ...data , proofs } , ( item ) => new BigNumber ( item as string ) ) ;
247251 } catch ( e ) {
248252 return { ...data , proofs, signature : proofs [ 0 ] } ;
249253 }
250254 }
251-
255+
252256 private _makeSignPromise ( ) : Signable {
253257 if ( ! this . _signPromise ) {
254258 this . _signPromise = this . _bytePromise . then ( bytes => {
@@ -265,14 +269,14 @@ export class Signable {
265269 this . _forSign
266270 ) ;
267271 } ) ;
268-
272+
269273 this . _signPromise . catch ( ( ) => {
270274 this . _signPromise = undefined ;
271275 } ) ;
272276 }
273277 return this ;
274278 }
275-
279+
276280 private _getAmountPrecision ( ) {
277281 const data = this . _forSign . data as any ;
278282 if ( data . type === TRANSACTION_TYPE_NUMBER . SCRIPT_INVOCATION ) {
@@ -292,7 +296,7 @@ export class Signable {
292296 const data = this . _forSign . data as any ;
293297 return data . fee && data . fee . asset && data . fee . asset . precision ? data . fee . asset . precision : 0 ;
294298 }
295-
299+
296300}
297301
298302export interface ISign2faOptions {
0 commit comments