import { CKBModel , WitnessArgs } from '../interfaces' ; import { ECDSA_WITNESS_LEN } from '../constants' ; import { validators , normalizers , transformers , Reader } from 'ckb-js-toolkit' ; import { SerializeTransaction , SerializeWitnessArgs , } from '@ckb-lumos/types/lib/core' ; import { RawTransaction } from '.' ; export class Transaction implements CKBModel { public witnesses : string [ ] ; constructor ( public raw , public witnessArgs , witnessLengths = [ ECDSA_WITNESS_LEN ] ) { this . witnesses = raw . inputs . map ( ( _ ) => '0x' ) ; for ( let i = 0 ; i < witnessLengths . length ; i ++ ) { this . witnesses [ i ] = '0x' + '0' . repeat ( witnessLengths [ i ] - 2 ) ; } if ( ! Array . isArray ( witnessArgs ) ) throw new Error ( '[Transaction] - witnessArgs must be an Array!' ) ; for ( let i = 0 ; i < witnessArgs . length ; i ++ ) { this . witnesses [ i ] = new Reader ( SerializeWitnessArgs ( normalizers . NormalizeWitnessArgs ( this . witnessArgs [ i ] ) ) ) . serializeJson ( ) ; } } sameWith ( tx ) { validators . ValidateTransaction ( transformers . TransformTransaction ( tx ) ) ; return ( tx . raw . sameWith ( this . raw ) && tx . witnesses . join ( '-' ) === this . witnesses . join ( '-' ) ) ; } getSize ( ) { const tx = transformers . TransformTransaction ( this ) ; validators . ValidateTransaction ( tx ) ; return ( SerializeTransaction ( normalizers . NormalizeTransaction ( tx ) ) . byteLength + 4 ) ; } validate ( ) { validators . ValidateTransaction ( transformers . TransformTransaction ( this ) ) ; return this ; } transform ( ) { return transformers . TransformTransaction ( this . serializeJson ( ) ) ; } serializeJson ( ) { return { ... this . raw , witnesses : this . witnesses , } ; } }