import { HashType , CKBModel } from '../interfaces' ; import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' ; import { Address , AddressType , AddressPrefix , getDefaultPrefix , } from './address' ; import { generateAddress , LumosConfigs } from '../utils' ; import { validators , transformers } from 'ckb-js-toolkit' ; export class Script implements CKBModel { static fromRPC ( data ) { if ( ! data ) return undefined ; validators . ValidateScript ( data ) ; return new Script ( data . code_hash , data . args , data . hash_type ) ; } constructor ( public codeHash , public args , public hashType ) { } sameWith ( script ) { validators . ValidateScript ( transformers . TransformScript ( script ) ) ; return ( this . args === script . args && this . codeHash === script . codeHash && this . hashType === script . hashType ) ; } validate ( ) { validators . ValidateScript ( transformers . TransformScript ( this ) ) ; return true ; } serializeJson ( ) { return { code_hash : this . codeHash , args : this . args , hash_type : this . hashType , } ; } toHash ( ) { return scriptToHash ( this ) ; } toAddress ( prefix = getDefaultPrefix ( ) ) { const address = generateAddress ( this . serializeJson ( ) , { config : LumosConfigs [ prefix ] , } ) ; return new Address ( address , AddressType . ckb ) ; } }