import { H160 , PlatformAddress , U64 } from "codechain-primitives" ; import { P2PKH } from "../../key/P2PKH" ; import { P2PKHBurn } from "../../key/P2PKHBurn" ; import { Asset } from "../Asset" ; import { AssetAddress } from "../classes" ; import { AssetTransaction , Transaction } from "../Transaction" ; import { NetworkId } from "../types" ; export interface WrapCCCData { shardId : number ; lockScriptHash : H160 ; parameters : Buffer [ ] ; quantity : U64 ; payer : PlatformAddress ; } export interface WrapCCCAddressData { shardId : number ; recipient : AssetAddress ; quantity : U64 ; payer : PlatformAddress ; } export interface WrapCCCActionJSON { shardId : number ; lockScriptHash : string ; parameters : string [ ] ; quantity : string ; payer : string ; } export class WrapCCC extends Transaction implements AssetTransaction { private readonly shardId : number ; private readonly lockScriptHash : H160 ; private readonly parameters : Buffer [ ] ; private readonly quantity : U64 ; private readonly payer : PlatformAddress ; public constructor ( data , networkId ) { super ( networkId ) ; 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 { shardId , quantity , payer } = data ; this . shardId = shardId ; this . quantity = quantity ; this . payer = payer ; } returns public getAssetType ( ) { return H160 . zero ( ) ; } returns public getAsset ( ) { const { shardId , lockScriptHash , parameters , quantity } = this ; return new Asset ( { assetType : this . getAssetType ( ) , shardId , lockScriptHash , parameters , quantity , tracker : this . tracker ( ) , transactionOutputIndex : 0 } ) ; } public tracker ( ) { return this . unsignedHash ( ) ; } param approval public addApproval ( _approval ) { throw Error ( "Cannot approve WrapCCC" ) ; } public type ( ) { return "wrapCCC" ; } protected actionToEncodeObject ( ) { const { shardId , lockScriptHash , parameters , quantity , payer } = this ; return [ 7 , shardId , lockScriptHash . toEncodeObject ( ) , parameters . map ( parameter => Buffer . from ( parameter ) ) , quantity . toEncodeObject ( ) , payer . getAccountId ( ) . toEncodeObject ( ) ] ; } protected actionToJSON ( ) { const { shardId , lockScriptHash , parameters , quantity , payer } = this ; return { shardId , lockScriptHash : lockScriptHash . toJSON ( ) , parameters : parameters . map ( ( p ) => p . toString ( "hex" ) ) , quantity : quantity . toJSON ( ) , payer : payer . toString ( ) } ; } }