import { JSONPath } from 'jsonpath-plus' ; import { KeyStore , Signer } from '../../../types/ExternalInterfaces' ; import * as TezosTypes from '../../../types/tezos/TezosChainTypes' ; import { TezosMessageUtils } from '../TezosMessageUtil' ; import { TezosNodeReader } from '../TezosNodeReader' ; import { TezosNodeWriter } from '../TezosNodeWriter' ; import { TezosContractUtils } from './TezosContractUtils' ; import { TezosParameterFormat } from '../../../types/tezos/TezosChainTypes' ; /**
 * Interface for the Name Service contract developed by Cryptonomic, Inc.
 */ export namespace CryptonomicNameServiceHelper { param server param address export async function verifyDestination ( server , address ) { return TezosContractUtils . verifyDestination ( server , address , 'c020219e31ee3b462ed93c33124f117f' ) ; } export async function commitName ( server , signer , keystore , contract , name , nonce , operationFee , freight ? , gas ? ) { const commitRecord = `(Pair "${ name }" (Pair ${ nonce } 0x${ TezosMessageUtils . writeAddress ( keystore . publicKeyHash ) }))` ; const packedCommit = TezosMessageUtils . writePackedData ( commitRecord , 'record' , TezosParameterFormat . Michelson ) ; const hashedCommit = TezosMessageUtils . simpleHash ( Buffer . from ( packedCommit , 'hex' ) , 32 ) ; const parameters = `0x${ hashedCommit . toString ( 'hex' ) }` ; if ( ! freight || ! gas ) { const cost = await TezosNodeWriter . testContractInvocationOperation ( server , 'main' , keystore , contract , 0 , operationFee , 6000 , 500000 , 'commit' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; if ( ! freight ) { freight = Number ( cost [ 'storageCost' ] ) || 0 ; } if ( ! gas ) { gas = Number ( cost [ 'gas' ] ) + 300 ; } } const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , operationFee , freight , gas , 'commit' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } param server param signer param keystore param contract param name param { number } nonce param registrationPeriod param registrationFee param operationFee param freight param gas export async function registerName ( server , signer , keystore , contract , name , nonce , registrationPeriod , registrationFee , operationFee , freight ? , gas ? ) { const parameters = `(Pair ${ registrationPeriod } (Pair "${ name }" ${ nonce }))` ; if ( ! freight || ! gas ) { const cost = await TezosNodeWriter . testContractInvocationOperation ( server , 'main' , keystore , contract , registrationFee , operationFee , 6000 , 500000 , 'registerName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; if ( ! freight ) { freight = Number ( cost [ 'storageCost' ] ) || 0 ; } if ( ! gas ) { gas = Number ( cost [ 'gas' ] ) + 300 ; } } const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , registrationFee , operationFee , freight , gas , 'registerName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function updateRegistrationPeriod ( server , signer , keystore , contract , name , newRegistrationPeriod , registrationFee , operationFee , freight ? , gas ? ) { const parameters = `(Pair "${ name }" ${ newRegistrationPeriod })` ; if ( ! freight || ! gas ) { const cost = await TezosNodeWriter . testContractInvocationOperation ( server , 'main' , keystore , contract , registrationFee , operationFee , 1000 , 100000 , 'updateRegistrationPeriod' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; if ( ! freight ) { freight = Number ( cost [ 'storageCost' ] ) || 0 ; } if ( ! gas ) { gas = Number ( cost [ 'gas' ] ) + 300 ; } } const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , registrationFee , operationFee , freight , gas , 'updateRegistrationPeriod' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function setPrimaryName ( server , signer , keystore , contract , name , fee , freight ? , gas ? ) { const parameters = `"${ name }"` ; if ( ! freight || ! gas ) { const cost = await TezosNodeWriter . testContractInvocationOperation ( server , 'main' , keystore , contract , 0 , fee , 1000 , 100000 , 'setPrimaryName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; if ( ! freight ) { freight = Number ( cost [ 'storageCost' ] ) || 0 ; } if ( ! gas ) { gas = Number ( cost [ 'gas' ] ) + 300 ; } } const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , 'deleteName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function deleteName ( server , signer , keystore , contract , name , fee , freight ? , gas ? ) { const parameters = `"${ name }"` ; if ( ! freight || ! gas ) { const cost = await TezosNodeWriter . testContractInvocationOperation ( server , 'main' , keystore , contract , 0 , fee , 1000 , 100000 , 'deleteName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; if ( ! freight ) { freight = Number ( cost [ 'storageCost' ] ) || 0 ; } if ( ! gas ) { gas = Number ( cost [ 'gas' ] ) + 300 ; } } const nodeResult = await TezosNodeWriter . sendContractInvocationOperation ( server , signer , keystore , contract , 0 , fee , freight , gas , 'deleteName' , parameters , TezosTypes . TezosParameterFormat . Michelson ) ; return TezosContractUtils . clearRPCOperationGroupHash ( nodeResult . operationGroupID ) ; } export async function getNameForAddress ( server , mapid , address ) { const packedKey = TezosMessageUtils . encodeBigMapKey ( Buffer . from ( TezosMessageUtils . writePackedData ( address , 'address' ) , 'hex' ) ) ; try { const mapResult = await TezosNodeReader . getValueForBigMapKey ( server , mapid , packedKey ) ; return JSONPath ( { path : '$.string' , json : mapResult } ) [ 0 ] ; } catch { } return '' ; } param server param mapid param name export async function getNameInfo ( server , mapid , name ) { const packedKey = TezosMessageUtils . encodeBigMapKey ( Buffer . from ( TezosMessageUtils . writePackedData ( name , 'string' ) , 'hex' ) ) ; const mapResult = await TezosNodeReader . getValueForBigMapKey ( server , mapid , packedKey ) ; return { name : JSONPath ( { path : '$.args[0].args[1].string' , json : mapResult } ) [ 0 ] , modified : Boolean ( JSONPath ( { path : '$.args[0].args[0].prim' , json : mapResult } ) [ 0 ] ) , owner : JSONPath ( { path : '$.args[1].args[0].string' , json : mapResult } ) [ 0 ] , registeredAt : new Date ( JSONPath ( { path : '$.args[1].args[1].args[0].string' , json : mapResult } ) [ 0 ] ) , registrationPeriod : JSONPath ( { path : '$.args[1].args[1].args[1].int' , json : mapResult } ) [ 0 ] } ; } param server param contract returns export async function getSimpleStorage ( server , contract ) { const storageResult = await TezosNodeReader . getContractStorage ( server , contract ) ; return { addressMap : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].int' , json : storageResult } ) [ 0 ] ) , commitmentMap : Number ( JSONPath ( { path : '$.args[0].args[0].args[1].int' , json : storageResult } ) [ 0 ] ) , manager : JSONPath ( { path : '$.args[0].args[1].args[0].string' , json : storageResult } ) [ 0 ] , interval : Number ( JSONPath ( { path : '$.args[0].args[1].args[1].int' , json : storageResult } ) [ 0 ] ) , maxCommitTime : Number ( JSONPath ( { path : '$.args[1].args[0].args[0].int' , json : storageResult } ) [ 0 ] ) , maxDuration : Number ( JSONPath ( { path : '$.args[1].args[0].args[1].int' , json : storageResult } ) [ 0 ] ) , minCommitTime : Number ( JSONPath ( { path : '$.args[1].args[1].args[0].int' , json : storageResult } ) [ 0 ] ) , nameMap : Number ( JSONPath ( { path : '$.args[1].args[1].args[1].args[0].int' , json : storageResult } ) [ 0 ] ) , intervalFee : Number ( JSONPath ( { path : '$.args[1].args[1].args[1].args[1].int' , json : storageResult } ) [ 0 ] ) } ; } } export interface NameServiceStorage { addressMap : number ; commitmentMap : number ; manager : string ; interval : number ; maxCommitTime : number ; maxDuration : number ; minCommitTime : number ; nameMap : number ; intervalFee : number ; } export interface NameServiceRecord { name : string ; modified : boolean ; owner : string ; registeredAt : Date ; registrationPeriod : number ; }