import { blake160 , blake256 } from "../../utils" ; import { Asset } from "../Asset" ; import { AssetScheme , H160 , H256 , PlatformAddress } from "../classes" ; import { AssetTransaction , Transaction } from "../Transaction" ; import { NetworkId } from "../types" ; import { AssetMintOutput , AssetMintOutputJSON } from "./AssetMintOutput" ; const RLP = require ( "rlp" ) ; export interface AssetMintTransactionJSON { networkId : string ; shardId : number ; metadata : string ; output : AssetMintOutputJSON ; approver : string | null ; registrar : string | null ; allowedScriptHashes : string [ ] ; } export interface MintAssetActionJSON extends AssetMintTransactionJSON { approvals : string [ ] ; } export class MintAsset extends Transaction implements AssetTransaction { private readonly _transaction : AssetMintTransaction ; private readonly approvals : string [ ] ; public constructor ( input ) { super ( input . networkId ) ; this . _transaction = new AssetMintTransaction ( input ) ; this . approvals = input . approvals ; } returns public tracker ( ) { return new H256 ( blake256 ( this . _transaction . rlpBytes ( ) ) ) ; } param approval public addApproval ( approval ) { this . approvals . push ( approval ) ; } public output ( ) { return this . _transaction . output ; } returns public getMintedAsset ( ) { const { lockScriptHash , parameters , supply } = this . _transaction . output ; if ( supply == null ) { throw Error ( "not implemented" ) ; } return new Asset ( { assetType : this . getAssetType ( ) , shardId : this . _transaction . shardId , lockScriptHash , parameters , quantity : supply , tracker : this . tracker ( ) , transactionOutputIndex : 0 } ) ; } return public getAssetScheme ( ) { const { networkId , shardId , metadata , output : { supply } , approver , registrar , allowedScriptHashes } = this . _transaction ; if ( supply == null ) { throw Error ( "not implemented" ) ; } return new AssetScheme ( { networkId , shardId , metadata , supply , approver , registrar , allowedScriptHashes , pool : [ ] } ) ; } returns public getAssetType ( ) { const blake = blake160 ( this . tracker ( ) . value ) ; return new H160 ( blake ) ; } public type ( ) { return "mintAsset" ; } protected actionToEncodeObject ( ) { const encoded = this . _transaction . toEncodeObject ( ) ; encoded . push ( this . approvals ) ; return encoded ; } protected actionToJSON ( ) { const json = this . _transaction . toJSON ( ) ; return { ... json , approvals : this . approvals } ; } } /**
 * Creates a new asset type and that asset itself.
 *
 * The owner of the new asset created can be assigned by a lock script hash and parameters.
 *  - A metadata is a string that explains the asset's type.
 *  - Supply defines the quantity of asset to be created. If set as null, it
 *  will be set as the maximum value of a 64-bit unsigned integer by default.
 *  - If approver exists, the approver must be the Signer of the Transaction when
 *  sending the created asset through AssetTransferTransaction.
 *  - If registrar exists, the registrar can transfer without unlocking.
 */ class AssetMintTransaction { public readonly networkId : NetworkId ; public readonly shardId : number ; public readonly metadata : string ; public readonly output : AssetMintOutput ; public readonly approver : PlatformAddress | null ; public readonly registrar : PlatformAddress | null ; public readonly allowedScriptHashes : H160 [ ] ; param data . networkId param data . shardId param data . metadata param data . output . lockScriptHash param data . output . parameters param data . output . supply param data . approver param data . registrar param data . allowedScriptHashes constructor ( data ) { const { networkId , shardId , metadata , output , approver , registrar , allowedScriptHashes } = data ; this . networkId = networkId ; this . shardId = shardId ; this . metadata = typeof metadata === "string" ? metadata : JSON . stringify ( metadata ) ; this . output = output ; this . approver = approver ; this . registrar = registrar ; this . allowedScriptHashes = allowedScriptHashes ; } returns public toJSON ( ) { const { networkId , shardId , metadata , output , approver , registrar , allowedScriptHashes } = this ; return { networkId , shardId , metadata , output : output . toJSON ( ) , approver : approver == null ? null : approver . toString ( ) , registrar : registrar == null ? null : registrar . toString ( ) , allowedScriptHashes : allowedScriptHashes . map ( hash => hash . toJSON ( ) ) } ; } /**
     * Convert to an object for RLP encoding.
     */ public toEncodeObject ( ) { const { networkId , shardId , metadata , output : { lockScriptHash , parameters , supply } , approver , registrar , allowedScriptHashes } = this ; return [ 0x13 , networkId , shardId , metadata , lockScriptHash . toEncodeObject ( ) , parameters . map ( parameter => Buffer . from ( parameter ) ) , supply . toEncodeObject ( ) , approver ? [ approver . getAccountId ( ) . toEncodeObject ( ) ] : [ ] , registrar ? [ registrar . getAccountId ( ) . toEncodeObject ( ) ] : [ ] , allowedScriptHashes . map ( hash => hash . toEncodeObject ( ) ) ] ; } /**
     * Convert to RLP bytes.
     */ public rlpBytes ( ) { return RLP . encode ( this . toEncodeObject ( ) ) ; } }