import * as cryptoJS from 'crypto-js' ; import { Signable } from '../crypto/index' ; import { PrivateKey } from '../crypto/PrivateKey' ; import { TxType } from '../transaction/transaction' ; import { SignatureScheme } from './../crypto/SignatureScheme' ; import { TransactionAttribute } from './../transaction/txAttribute' ; import { num2hexstring , num2VarInt } from './../utils' ; import { Program } from './Program' ; import { TransactionInput } from './TransactionInput' ; import { TransactionOutput } from './TransactionOutput' ; export class TransactionNeo implements Signable { /**
     * Transaction type
     */ type : TxType ; version : number = 0 ; nonce : string ; attributes : TransactionAttribute [ ] ; inputs : TransactionInput [ ] ; outputs : TransactionOutput [ ] ; scripts : Program [ ] ; serialize ( ) { let result = this . serializeUnsigned ( ) ; result += num2VarInt ( this . scripts . length ) ; for ( const s of this . scripts ) { result += s . serialize ( ) ; } return result ; } serializeUnsigned ( ) { let result = '' ; result += num2hexstring ( this . type ) ; result += num2hexstring ( this . version ) ; result += this . serializeExclusiveData ( ) ; result += num2VarInt ( this . attributes . length ) ; for ( const a of this . attributes ) { result += a . serialize ( ) ; } result += num2VarInt ( this . inputs . length ) ; for ( const i of this . inputs ) { result += i . serialize ( ) ; } result += num2VarInt ( this . outputs . length ) ; for ( const o of this . outputs ) { result += o . serialize ( ) ; } return result ; } getHash ( ) { const data = this . serializeUnsigned ( ) ; const ProgramHexString = cryptoJS . enc . Hex . parse ( data ) ; const ProgramSha256 = cryptoJS . SHA256 ( ProgramHexString ) . toString ( ) ; const ProgramSha2562 = cryptoJS . SHA256 ( cryptoJS . enc . Hex . parse ( ProgramSha256 ) ) . toString ( ) ; return ProgramSha2562 ; } getSignContent ( ) { return this . getHashData ( ) ; } serializeUnsignedData ( ) { return this . getHashData ( ) ; } getHashData ( ) { return this . serializeUnsigned ( ) ; } sign ( privateKey , scheme = SignatureScheme . ECDSAwithSHA256 ) { const sig = privateKey . sign ( this . getHashData ( ) , scheme ) . serializeHex ( ) ; const signature = sig . substring ( 2 ) ; return signature ; } protected serializeExclusiveData ( ) { return '' ; } }