import { AssetAddress , AssetAddressValue , H128 , H160 , H160Value , H256 , H256Value , H512 , H512Value , PlatformAddress , PlatformAddressValue , U256 , U64 , U64Value } from "codechain-primitives" ; import { Asset } from "./Asset" ; import { AssetScheme } from "./AssetScheme" ; import { Block } from "./Block" ; import { Script } from "./Script" ; import { SignedTransaction } from "./SignedTransaction" ; import { Transaction } from "./Transaction" ; import { AssetMintOutput } from "./transaction/AssetMintOutput" ; import { AssetOutPoint } from "./transaction/AssetOutPoint" ; import { AssetTransferInput , Timelock } from "./transaction/AssetTransferInput" ; import { AssetTransferOutput } from "./transaction/AssetTransferOutput" ; import { ChangeAssetScheme } from "./transaction/ChangeAssetScheme" ; import { CreateShard } from "./transaction/CreateShard" ; import { Custom } from "./transaction/Custom" ; import { IncreaseAssetSupply } from "./transaction/IncreaseAssetSupply" ; import { MintAsset } from "./transaction/MintAsset" ; import { Order } from "./transaction/Order" ; import { OrderOnTransfer } from "./transaction/OrderOnTransfer" ; import { Pay } from "./transaction/Pay" ; import { Remove } from "./transaction/Remove" ; import { SetRegularKey } from "./transaction/SetRegularKey" ; import { SetShardOwners } from "./transaction/SetShardOwners" ; import { SetShardUsers } from "./transaction/SetShardUsers" ; import { Store } from "./transaction/Store" ; import { TransferAsset } from "./transaction/TransferAsset" ; import { UnwrapCCC } from "./transaction/UnwrapCCC" ; import { WrapCCC } from "./transaction/WrapCCC" ; import { NetworkId } from "./types" ; export class Core { public static classes = { H128 , H160 , H256 , H512 , U256 , U64 , Block , Transaction , SignedTransaction , Pay , SetRegularKey , CreateShard , SetShardOwners , SetShardUsers , WrapCCC , Store , Remove , Custom , AssetTransferInput , AssetTransferOutput , AssetOutPoint , Asset , AssetScheme , Script , PlatformAddress , AssetAddress } ; public classes = Core . classes ; private networkId : NetworkId ; param params . networkId constructor ( params ) { const { networkId } = params ; this . networkId = networkId ; } param params . recipient param params . quantity throws throws public createPayTransaction ( params ) { const { recipient , quantity } = params ; checkPlatformAddressRecipient ( recipient ) ; checkAmount ( quantity ) ; return new Pay ( PlatformAddress . ensure ( recipient ) , U64 . ensure ( quantity ) , this . networkId ) ; } param params . key throws public createSetRegularKeyTransaction ( params ) { const { key } = params ; checkKey ( key ) ; return new SetRegularKey ( H512 . ensure ( key ) , this . networkId ) ; } /**
     * Creates CreateShard type which can create new shard
     */ public createCreateShardTransaction ( params ) { const { users } = params ; return new CreateShard ( { users : users . map ( PlatformAddress . ensure ) } , this . networkId ) ; } public createSetShardOwnersTransaction ( params ) { const { shardId , owners } = params ; checkShardId ( shardId ) ; checkOwners ( owners ) ; return new SetShardOwners ( { shardId , owners : owners . map ( PlatformAddress . ensure ) } , this . networkId ) ; } param params . shardId param params . users public createSetShardUsersTransaction ( params ) { const { shardId , users } = params ; checkShardId ( shardId ) ; checkUsers ( users ) ; return new SetShardUsers ( { shardId , users : users . map ( PlatformAddress . ensure ) } , this . networkId ) ; } param params . shardId param params . lockScriptHash param params . parameters param params . quantity throws throws public createWrapCCCTransaction ( params ) { const { shardId , quantity , payer } = params ; checkShardId ( shardId ) ; checkAmount ( quantity ) ; checkPayer ( payer ) ; let data ; if ( "recipient" in params ) { checkAssetAddressRecipient ( params . recipient ) ; data = { shardId , recipient : AssetAddress . ensure ( params . recipient ) , quantity : U64 . ensure ( quantity ) , payer : PlatformAddress . ensure ( payer ) } ; } else { const { lockScriptHash , parameters } = params ; checkLockScriptHash ( lockScriptHash ) ; checkParameters ( parameters ) ; data = { shardId , lockScriptHash : H160 . ensure ( lockScriptHash ) , parameters , quantity : U64 . ensure ( quantity ) , payer : PlatformAddress . ensure ( payer ) } ; } return new WrapCCC ( data , this . networkId ) ; } param params . content param params . secret param params . certifier param params . signature throws public createStoreTransaction ( params ) { let storeParams ; if ( "secret" in params ) { const { content , secret } = params ; checkSecret ( secret ) ; storeParams = { content , secret : H256 . ensure ( secret ) } ; } else { const { content , certifier , signature } = params ; checkCertifier ( certifier ) ; checkSignature ( signature ) ; storeParams = { content , certifier : PlatformAddress . ensure ( certifier ) , signature } ; } return new Store ( storeParams , this . networkId ) ; } param params . hash param params . secret param params . signature throws public createRemoveTransaction ( params ) { let removeParam = null ; if ( "secret" in params ) { const { hash , secret } = params ; checkTransactionHash ( hash ) ; checkSecret ( secret ) ; removeParam = { hash : H256 . ensure ( hash ) , secret : H256 . ensure ( secret ) } ; } else { const { hash , signature } = params ; checkTransactionHash ( hash ) ; checkSignature ( signature ) ; removeParam = { hash : H256 . ensure ( hash ) , signature } ; } return new Remove ( removeParam , this . networkId ) ; } param params . handlerId param params . bytes throws public createCustomTransaction ( params ) { const { handlerId , bytes } = params ; checkHandlerId ( handlerId ) ; checkBytes ( bytes ) ; const customParam = { handlerId : U64 . ensure ( handlerId ) , bytes } ; return new Custom ( customParam , this . networkId ) ; } param params . metadata param params . supply param params . approver param params . registrar throws throws public createAssetScheme ( params ) { const { shardId , supply , approver = null , registrar = null , allowedScriptHashes = null , pool = [ ] } = params ; checkMetadata ( params . metadata ) ; const metadata = typeof params . metadata === "string" ? params . metadata : JSON . stringify ( params . metadata ) ; checkShardId ( shardId ) ; checkAmount ( supply ) ; checkApprover ( approver ) ; checkregistrar ( registrar ) ; return new AssetScheme ( { networkId : this . networkId , shardId , metadata , supply : U64 . ensure ( supply ) , approver : approver == null ? null : PlatformAddress . ensure ( approver ) , registrar : registrar == null ? null : PlatformAddress . ensure ( registrar ) , allowedScriptHashes : allowedScriptHashes == null ? [ ] : allowedScriptHashes , pool : pool . map ( ( { assetType , quantity : assetQuantity } ) => ( { assetType : H160 . ensure ( assetType ) , quantity : U64 . ensure ( assetQuantity ) } ) ) } ) ; } public createOrder ( params ) { const { assetTypeFrom , assetTypeTo , assetTypeFee = H160 . zero ( ) , shardIdFrom , shardIdTo , shardIdFee = 0 , assetQuantityFrom , assetQuantityTo , assetQuantityFee = 0 , originOutputs , expiration } = params ; checkAssetType ( assetTypeFrom ) ; checkAssetType ( assetTypeTo ) ; checkAssetType ( assetTypeFee ) ; checkShardId ( shardIdFrom ) ; checkShardId ( shardIdTo ) ; checkShardId ( shardIdFee ) ; checkAmount ( assetQuantityFrom ) ; checkAmount ( assetQuantityTo ) ; checkAmount ( assetQuantityFee ) ; checkExpiration ( expiration ) ; const originOutputsConv = [ ] ; for ( let i = 0 ; i < originOutputs . length ; i ++ ) { const originOutput = originOutputs [ i ] ; const { tracker , index , assetType , shardId , quantity , lockScriptHash , parameters } = originOutput ; checkAssetOutPoint ( originOutput ) ; originOutputsConv [ i ] = originOutput instanceof AssetOutPoint ? originOutput : new AssetOutPoint ( { tracker : H256 . ensure ( tracker ) , index , assetType : H160 . ensure ( assetType ) , shardId , quantity : U64 . ensure ( quantity ) , lockScriptHash : lockScriptHash ? H160 . ensure ( lockScriptHash ) : undefined , parameters } ) ; } const baseParams = { assetTypeFrom : H160 . ensure ( assetTypeFrom ) , assetTypeTo : H160 . ensure ( assetTypeTo ) , assetTypeFee : H160 . ensure ( assetTypeFee ) , shardIdFrom , shardIdTo , shardIdFee , assetQuantityFrom : U64 . ensure ( assetQuantityFrom ) , assetQuantityTo : U64 . ensure ( assetQuantityTo ) , assetQuantityFee : U64 . ensure ( assetQuantityFee ) , expiration : U64 . ensure ( expiration ) , originOutputs : originOutputsConv } ; let toParams ; let feeParams ; if ( "recipientFrom" in params ) { checkAssetAddressRecipient ( params . recipientFrom ) ; toParams = { recipientFrom : AssetAddress . ensure ( params . recipientFrom ) } ; } else { const { lockScriptHashFrom , parametersFrom } = params ; checkLockScriptHash ( lockScriptHashFrom ) ; checkParameters ( parametersFrom ) ; toParams = { lockScriptHashFrom : H160 . ensure ( lockScriptHashFrom ) , parametersFrom } ; } if ( "recipientFee" in params ) { checkAssetAddressRecipient ( params . recipientFee ) ; feeParams = { recipientFee : AssetAddress . ensure ( params . recipientFee ) } ; } else if ( "lockScriptHashFee" in params ) { const { lockScriptHashFee , parametersFee } = params ; checkLockScriptHash ( lockScriptHashFee ) ; checkParameters ( parametersFee ) ; feeParams = { lockScriptHashFee : H160 . ensure ( lockScriptHashFee ) , parametersFee } ; } else { feeParams = { lockScriptHashFee : H160 . ensure ( "0" . repeat ( 40 ) ) , parametersFee : [ ] } ; } return new Order ( { ... baseParams , ... toParams , ... feeParams } ) ; } public createOrderOnTransfer ( params ) { const { order , spentQuantity , inputFromIndices , inputFeeIndices , outputFromIndices , outputToIndices , outputOwnedFeeIndices , outputTransferredFeeIndices } = params ; checkOrder ( order ) ; checkAmount ( spentQuantity ) ; checkIndices ( inputFromIndices ) ; checkIndices ( inputFeeIndices ) ; checkIndices ( outputFromIndices ) ; checkIndices ( outputToIndices ) ; checkIndices ( outputOwnedFeeIndices ) ; checkIndices ( outputTransferredFeeIndices ) ; return new OrderOnTransfer ( { order , spentQuantity : U64 . ensure ( spentQuantity ) , inputFromIndices , inputFeeIndices , outputFromIndices , outputToIndices , outputOwnedFeeIndices , outputTransferredFeeIndices } ) ; } public createMintAssetTransaction ( params ) { const { scheme , recipient , approvals = [ ] } = params ; if ( scheme != null && typeof scheme !== "object" ) { throw Error ( `Expected scheme param to be either an AssetScheme or an object but found ${ scheme }` ) ; } const { networkId = this . networkId , shardId , approver : approver = null , registrar : registrar = null , allowedScriptHashes = null , supply = U64 . MAX_VALUE } = scheme ; checkMetadata ( scheme . metadata ) ; const metadata = typeof scheme . metadata === "string" ? scheme . metadata : JSON . stringify ( scheme . metadata ) ; checkAssetAddressRecipient ( recipient ) ; checkNetworkId ( networkId ) ; if ( shardId === undefined ) { throw Error ( `shardId is undefined` ) ; } checkShardId ( shardId ) ; checkApprover ( approver ) ; checkregistrar ( registrar ) ; checkAmount ( supply ) ; return new MintAsset ( { networkId , shardId , approver : approver == null ? null : PlatformAddress . ensure ( approver ) , registrar : registrar == null ? null : PlatformAddress . ensure ( registrar ) , allowedScriptHashes : allowedScriptHashes == null ? [ ] : allowedScriptHashes , metadata , output : new AssetMintOutput ( { supply : U64 . ensure ( supply ) , recipient : AssetAddress . ensure ( recipient ) } ) , approvals } ) ; } public createChangeAssetSchemeTransaction ( params ) { const { shardId , assetType , seq = 0 , scheme , approvals = [ ] } = params ; if ( scheme != null && typeof scheme !== "object" ) { throw Error ( `Expected scheme param to be either an AssetScheme or an object but found ${ scheme }` ) ; } const { networkId = this . networkId , approver : approver = null , registrar : registrar = null , allowedScriptHashes = null } = scheme ; checkMetadata ( scheme . metadata ) ; const metadata = typeof scheme . metadata === "string" ? scheme . metadata : JSON . stringify ( scheme . metadata ) ; checkNetworkId ( networkId ) ; checkAssetType ( assetType ) ; checkApprover ( approver ) ; checkregistrar ( registrar ) ; return new ChangeAssetScheme ( { networkId , shardId , assetType : H160 . ensure ( assetType ) , seq , metadata , approver : approver == null ? null : PlatformAddress . ensure ( approver ) , registrar : registrar == null ? null : PlatformAddress . ensure ( registrar ) , allowedScriptHashes : allowedScriptHashes == null ? [ ] : allowedScriptHashes , approvals } ) ; } public createIncreaseAssetSupplyTransaction ( params ) { const { shardId , assetType , recipient , seq = 0 , supply = U64 . MAX_VALUE , approvals = [ ] } = params ; checkNetworkId ( this . networkId ) ; checkShardId ( shardId ) ; checkAssetType ( assetType ) ; checkAmount ( supply ) ; return new IncreaseAssetSupply ( { networkId : this . networkId , shardId , assetType : H160 . ensure ( assetType ) , seq , output : new AssetMintOutput ( { supply : U64 . ensure ( supply ) , recipient : AssetAddress . ensure ( recipient ) } ) , approvals } ) ; } public createTransferAssetTransaction ( params ? ) { const { burns = [ ] , inputs = [ ] , outputs = [ ] , orders = [ ] , networkId = this . networkId , metadata = "" , approvals = [ ] , expiration = null } = params || { } ; checkMetadata ( metadata ) ; checkTransferBurns ( burns ) ; checkTransferInputs ( inputs ) ; checkTransferOutputs ( outputs ) ; checkNetworkId ( networkId ) ; return new TransferAsset ( { burns , inputs , outputs , orders , networkId , metadata : typeof metadata === "string" ? metadata : JSON . stringify ( metadata ) , approvals , expiration } ) ; } public createUnwrapCCCTransaction ( params ) { const { burn , networkId = this . networkId } = params ; const receiver = PlatformAddress . ensure ( params . receiver ) ; checkNetworkId ( networkId ) ; if ( burn instanceof Asset ) { const burnInput = burn . createTransferInput ( ) ; checkTransferBurns ( [ burnInput ] ) ; return new UnwrapCCC ( { burn : burnInput , networkId , receiver } ) ; } else { checkTransferBurns ( [ burn ] ) ; return new UnwrapCCC ( { burn , networkId , receiver } ) ; } } public createAssetTransferInput ( params ) { const { assetOutPoint , timelock = null , lockScript , unlockScript } = params ; checkAssetOutPoint ( assetOutPoint ) ; checkTimelock ( timelock ) ; if ( lockScript ) { checkLockScript ( lockScript ) ; } if ( unlockScript ) { checkUnlockScript ( unlockScript ) ; } const { tracker , index , assetType , shardId , quantity , lockScriptHash , parameters } = assetOutPoint ; return new AssetTransferInput ( { prevOut : assetOutPoint instanceof AssetOutPoint ? assetOutPoint : new AssetOutPoint ( { tracker : H256 . ensure ( tracker ) , index , assetType : H160 . ensure ( assetType ) , shardId , quantity : U64 . ensure ( quantity ) , lockScriptHash : lockScriptHash ? H160 . ensure ( lockScriptHash ) : undefined , parameters } ) , timelock , lockScript , unlockScript } ) ; } public createAssetOutPoint ( params ) { const { tracker , index , assetType , shardId , quantity } = params ; checkTracker ( tracker ) ; checkIndex ( index ) ; checkAssetType ( assetType ) ; checkShardId ( shardId ) ; checkAmount ( quantity ) ; return new AssetOutPoint ( { tracker : H256 . ensure ( tracker ) , index , assetType : H160 . ensure ( assetType ) , shardId , quantity : U64 . ensure ( quantity ) } ) ; } public createAssetTransferOutput ( params ) { const { assetType , shardId } = params ; const quantity = U64 . ensure ( params . quantity ) ; checkAssetType ( assetType ) ; checkShardId ( shardId ) ; checkAmount ( quantity ) ; if ( "recipient" in params ) { const { recipient } = params ; checkAssetAddressRecipient ( recipient ) ; return new AssetTransferOutput ( { recipient : AssetAddress . ensure ( recipient ) , assetType : H160 . ensure ( assetType ) , shardId , quantity } ) ; } else if ( "lockScriptHash" in params && "parameters" in params ) { const { lockScriptHash , parameters } = params ; checkLockScriptHash ( lockScriptHash ) ; checkParameters ( parameters ) ; return new AssetTransferOutput ( { lockScriptHash : H160 . ensure ( lockScriptHash ) , parameters , assetType : H160 . ensure ( assetType ) , shardId , quantity } ) ; } else { throw Error ( `Unexpected params: ${ params }` ) ; } } } function checkNetworkId ( networkId ) { if ( typeof networkId !== "string" || networkId . length !== 2 ) { throw Error ( `Expected networkId param to be a string of length 2 but found ${ networkId }` ) ; } } function checkPlatformAddressRecipient ( recipient ) { if ( ! PlatformAddress . check ( recipient ) ) { throw Error ( `Expected recipient param to be a PlatformAddress but found ${ recipient }` ) ; } } function checkAssetAddressRecipient ( recipient ) { if ( ! AssetAddress . check ( recipient ) ) { throw Error ( `Expected recipient param to be a AssetAddress but found ${ recipient }` ) ; } } function checkAmount ( amount ) { if ( ! U64 . check ( amount ) ) { throw Error ( `Expected amount param to be a U64 value but found ${ amount }` ) ; } } function checkExpiration ( expiration ) { if ( ! U64 . check ( expiration ) ) { throw Error ( `Expected expiration param to be a U64 value but found ${ expiration }` ) ; } } function checkKey ( key ) { if ( ! H512 . check ( key ) ) { throw Error ( `Expected key param to be an H512 value but found ${ key }` ) ; } } function checkShardId ( shardId ) { if ( typeof shardId !== "number" || ! Number . isInteger ( shardId ) || shardId < 0 || shardId > 0xffff ) { throw Error ( `Expected shardId param to be a number but found ${ shardId }` ) ; } } function checkMetadata ( metadata ) { if ( typeof metadata !== "string" && typeof metadata !== "object" && metadata != null ) { throw Error ( `Expected metadata param to be either a string or an object but found ${ metadata }` ) ; } } function checkApprover ( approver ) { if ( approver != null && ! PlatformAddress . check ( approver ) ) { throw Error ( `Expected approver param to be either null or a PlatformAddress value but found ${ approver }` ) ; } } function checkregistrar ( registrar ) { if ( registrar != null && ! PlatformAddress . check ( registrar ) ) { throw Error ( `Expected registrar param to be either null or a PlatformAddress value but found ${ registrar }` ) ; } } function checkCertifier ( certifier ) { if ( ! PlatformAddress . check ( certifier ) ) { throw Error ( `Expected certifier param to be a PlatformAddress but found ${ certifier }` ) ; } } function checkPayer ( payer ) { if ( ! PlatformAddress . check ( payer ) ) { throw Error ( `Expected payer param to be a PlatformAddress but found ${ payer }` ) ; } } function checkOwners ( owners ) { if ( ! Array . isArray ( owners ) ) { throw Error ( `Expected owners param to be an array but found ${ owners }` ) ; } owners . forEach ( ( owner , index ) => { if ( ! PlatformAddress . check ( owner ) ) { throw Error ( `Expected an owner address to be a PlatformAddress value but found ${ owner } at index ${ index }` ) ; } } ) ; } function checkUsers ( users ) { if ( ! Array . isArray ( users ) ) { throw Error ( `Expected users param to be an array but found ${ users }` ) ; } users . forEach ( ( user , index ) => { if ( ! PlatformAddress . check ( user ) ) { throw Error ( `Expected a user address to be a PlatformAddress value but found ${ user } at index ${ index }` ) ; } } ) ; } function checkTransferBurns ( burns ) { if ( ! Array . isArray ( burns ) ) { throw Error ( `Expected burns param to be an array but found ${ burns }` ) ; } burns . forEach ( ( burn , index ) => { if ( ! ( burn instanceof AssetTransferInput ) ) { throw Error ( `Expected an item of burns to be an AssetTransferInput but found ${ burn } at index ${ index }` ) ; } } ) ; } function checkTransferInputs ( inputs ) { if ( ! Array . isArray ( inputs ) ) { throw Error ( `Expected inputs param to be an array but found ${ inputs }` ) ; } inputs . forEach ( ( input , index ) => { if ( ! ( input instanceof AssetTransferInput ) ) { throw Error ( `Expected an item of inputs to be an AssetTransferInput but found ${ input } at index ${ index }` ) ; } } ) ; } function checkTransferOutputs ( outputs ) { if ( ! Array . isArray ( outputs ) ) { throw Error ( `Expected outputs param to be an array but found ${ outputs }` ) ; } outputs . forEach ( ( output , index ) => { if ( ! ( output instanceof AssetTransferOutput ) ) { throw Error ( `Expected an item of outputs to be an AssetTransferOutput but found ${ output } at index ${ index }` ) ; } } ) ; } function checkTracker ( value ) { if ( ! H256 . check ( value ) ) { throw Error ( `Expected tracker param to be an H256 value but found ${ value }` ) ; } } function checkIndex ( index ) { if ( typeof index !== "number" ) { throw Error ( `Expected index param to be a number but found ${ index }` ) ; } } function checkAssetType ( value ) { if ( ! H160 . check ( value ) ) { throw Error ( `Expected assetType param to be an H160 value but found ${ value }` ) ; } } function checkAssetOutPoint ( value ) { if ( value != null && typeof value !== "object" ) { throw Error ( `Expected assetOutPoint param to be either an AssetOutPoint or an object but found ${ value }` ) ; } const { tracker , index , assetType , shardId , quantity , lockScriptHash , parameters } = value ; checkTracker ( tracker ) ; checkIndex ( index ) ; checkAssetType ( assetType ) ; checkShardId ( shardId ) ; checkAmount ( quantity ) ; if ( lockScriptHash ) { checkLockScriptHash ( lockScriptHash ) ; } if ( parameters ) { checkParameters ( parameters ) ; } } function checkOrder ( order ) { if ( order != null && ! ( order instanceof Order ) ) { throw Error ( `Expected order param to be either null or an Order value but found ${ order }` ) ; } } function checkIndices ( indices ) { if ( ! Array . isArray ( indices ) ) { throw Error ( `Expected indices param to be an array but found ${ indices }` ) ; } indices . forEach ( ( value , idx ) => { if ( typeof value !== "number" ) { throw Error ( `Expected an indices to be an array of numbers but found ${ value } at index ${ idx }` ) ; } } ) ; } function checkLockScriptHash ( value ) { if ( ! H160 . check ( value ) ) { throw Error ( `Expected lockScriptHash param to be an H160 value but found ${ value }` ) ; } } function checkTransactionHash ( value ) { if ( ! H256 . check ( value ) ) { throw Error ( `Expected hash param to be an H256 value but found ${ value }` ) ; } } function checkSecret ( value ) { if ( ! H256 . check ( value ) ) { throw Error ( `Expected secret param to be an H256 value but found ${ value }` ) ; } } function checkParameters ( parameters ) { if ( ! Array . isArray ( parameters ) ) { throw Error ( `Expected parameters param to be an array but found ${ parameters }` ) ; } parameters . forEach ( ( p , index ) => { if ( ! ( p instanceof Buffer ) ) { throw Error ( `Expected an item of parameters to be a Buffer instance but found ${ p } at index ${ index }` ) ; } } ) ; } function checkTimelock ( timelock ) { if ( timelock == null ) { return ; } const { type , value } = timelock ; if ( type === "block" || type === "blockAge" || type === "time" || type === "timeAge" ) { return ; } if ( typeof value === "number" ) { return ; } throw Error ( `Expected timelock param to be either null or an object containing both type and value but found ${ timelock }` ) ; } function checkLockScript ( lockScript ) { if ( ! ( lockScript instanceof Buffer ) ) { throw Error ( `Expedted lockScript param to be an instance of Buffer but found ${ lockScript }` ) ; } } function checkUnlockScript ( unlockScript ) { if ( ! ( unlockScript instanceof Buffer ) ) { throw Error ( `Expected unlockScript param to be an instance of Buffer but found ${ unlockScript }` ) ; } } function checkSignature ( signature ) { if ( typeof signature !== "string" || ! /^(0x)?[0-9a-fA-F]{130}$/ . test ( signature ) ) { throw Error ( `Expected signature param to be a 65 byte hexstring but found ${ signature }` ) ; } } function checkHandlerId ( handlerId ) { if ( typeof handlerId !== "number" || ! Number . isInteger ( handlerId ) || handlerId < 0 ) { throw Error ( `Expected handlerId param to be a non-negative number value but found ${ handlerId }` ) ; } } function checkBytes ( bytes ) { if ( ! ( bytes instanceof Buffer ) ) { throw Error ( `Expected bytes param to be an instance of Buffer but found ${ bytes }` ) ; } }