import { H160 , H256 , H512 , PlatformAddress } from "codechain-primitives" ; import * as _ from "lodash" ; import { blake160 , blake256 , recoverEcdsa } from "../utils" ; import { Asset } from "./Asset" ; import { Transaction , TransactionJSON } from "./Transaction" ; import { NetworkId } from "./types" ; const RLP = require ( "rlp" ) ; export interface SignedTransactionJSON extends TransactionJSON { blockNumber : number | null ; blockHash : string | null ; transactionIndex : number | null ; sig : string ; hash : string ; } /**
 * A [Transaction](tx.html) signed by a private key. It is possible to request
 * the CodeChain network to process this tx with the
 * [sendSignedTransaction](chainrpc.html#sendsignedtransaction) function.
 *
 * Transactions signed with a regular key has the same effect as those signed with
 * the original key. The original key is the key of the account that registered
 * the regular key.
 *
 * If any of the following is true, the Transaction will not be processed:
 * - The Transaction's processing fee is less than 10.
 * - A network ID is not identical.
 * - A seq is not identical to the signer's seq.
 */ export class SignedTransaction { public unsigned : Transaction ; public blockNumber : number | null ; public blockHash : H256 | null ; public transactionIndex : number | null ; private _signature : string ; param unsigned param signature param blockNumber param blockHash param transactionIndex constructor ( unsigned , signature , blockNumber ? , blockHash ? , transactionIndex ? ) { this . unsigned = unsigned ; this . _signature = signature . startsWith ( "0x" ) ? signature . substr ( 2 ) : signature ; this . blockNumber = blockNumber === undefined ? null : blockNumber ; this . blockHash = blockHash || null ; this . transactionIndex = transactionIndex === undefined ? null : transactionIndex ; } /**
     * Get the signature of a tx.
     */ public signature ( ) { return this . _signature ; } /**
     * Convert to an object for RLP encoding.
     */ public toEncodeObject ( ) { const { unsigned , _signature } = this ; const result = unsigned . toEncodeObject ( ) ; result . push ( `0x${ _signature }` ) ; return result ; } /**
     * Convert to RLP bytes.
     */ public rlpBytes ( ) { return RLP . encode ( this . toEncodeObject ( ) ) ; } returns public hash ( ) { return new H256 ( blake256 ( this . rlpBytes ( ) ) ) ; } public getAsset ( ) { return ( this . unsigned as any ) . getAsset ( ) ; } returns deprecated public getSignerAccountId ( ) { const { _signature , unsigned } = this ; const publicKey = recoverEcdsa ( unsigned . unsignedHash ( ) . value , _signature ) ; return new H160 ( blake160 ( publicKey ) ) ; } returns deprecated public getSignerAddress ( params ) { return PlatformAddress . fromAccountId ( this . getSignerAccountId ( ) , params ) ; } returns public getSignerPublic ( ) { const { _signature , unsigned } = this ; return new H512 ( recoverEcdsa ( unsigned . unsignedHash ( ) . value , _signature ) ) ; } returns public toJSON ( ) { const { blockNumber , blockHash , transactionIndex , unsigned , _signature } = this ; const json = { ... unsigned . toJSON ( ) , blockNumber , blockHash : blockHash === null ? null : blockHash . toJSON ( ) , transactionIndex , sig : `0x${ _signature }` , hash : this . hash ( ) . toJSON ( ) } ; return json ; } }