import { Buffer } from "buffer" ; import { AssetAddress , H160 , U64 } from "codechain-primitives" ; import { P2PKH } from "../../key/P2PKH" ; import { P2PKHBurn } from "../../key/P2PKHBurn" ; export interface AssetTransferOutputJSON { lockScriptHash : string ; parameters : string [ ] ; assetType : string ; shardId : number ; quantity : string ; } export interface AssetTransferOutputData { lockScriptHash : H160 ; parameters : Buffer [ ] ; assetType : H160 ; shardId : number ; quantity : U64 ; } export interface AssetTransferOutputAddressData { recipient : AssetAddress ; assetType : H160 ; shardId : number ; quantity : U64 ; } /**
 * An AssetTransferOutput consists of:
 *  - A lock script hash and parameters, which mark ownership of the asset.
 *  - An asset type and quantity, which indicate the asset's type and quantity.
 */ export class AssetTransferOutput { param data returns public static fromJSON ( data ) { const { lockScriptHash , parameters , assetType , shardId , quantity } = data ; return new AssetTransferOutput ( { lockScriptHash : H160 . ensure ( lockScriptHash ) , parameters : parameters . map ( ( p ) => Buffer . from ( p , "hex" ) ) , assetType : H160 . ensure ( assetType ) , shardId , quantity : U64 . ensure ( quantity ) } ) ; } public readonly lockScriptHash : H160 ; public readonly parameters : Buffer [ ] ; public readonly assetType : H160 ; public readonly shardId : number ; public readonly quantity : U64 ; param data . lockScriptHash param data . parameters param data . assetType param data . shardId param data . quantity constructor ( data ) { if ( "recipient" in data ) { const { type , payload } = data . recipient ; if ( "pubkeys" in payload ) { throw Error ( "Multisig payload is not supported yet" ) ; } switch ( type ) { case 0x00 : this . lockScriptHash = payload ; this . parameters = [ ] ; break ; case 0x01 : this . lockScriptHash = P2PKH . getLockScriptHash ( ) ; this . parameters = [ Buffer . from ( payload . value , "hex" ) ] ; break ; case 0x02 : this . lockScriptHash = P2PKHBurn . getLockScriptHash ( ) ; this . parameters = [ Buffer . from ( payload . value , "hex" ) ] ; break ; default : throw Error ( `Unexpected type of AssetAddress: ${ type }, ${ data . recipient }` ) ; } } else { const { lockScriptHash , parameters } = data ; this . lockScriptHash = lockScriptHash ; this . parameters = parameters ; } const { assetType , shardId , quantity } = data ; this . assetType = assetType ; this . shardId = shardId ; this . quantity = quantity ; } /**
     * Convert to an object for RLP encoding.
     */ public toEncodeObject ( ) { const { lockScriptHash , parameters , assetType , shardId , quantity } = this ; return [ lockScriptHash . toEncodeObject ( ) , parameters . map ( parameter => Buffer . from ( parameter ) ) , assetType . toEncodeObject ( ) , shardId , quantity . toEncodeObject ( ) ] ; } returns public toJSON ( ) { const { lockScriptHash , parameters , assetType , shardId , quantity } = this ; return { lockScriptHash : lockScriptHash . toJSON ( ) , parameters : parameters . map ( ( p ) => p . toString ( "hex" ) ) , assetType : assetType . toJSON ( ) , shardId , quantity : quantity . toJSON ( ) } ; } }