import * as blakejs from 'blakejs' ; import { JSONPath } from 'jsonpath-plus' ; import { TezosMessageUtils } from '../TezosMessageUtil' ; import { TezosNodeReader } from '../TezosNodeReader' ; /**
 * Interface for the Tezos Commons Baker Registry contract outlined here: https://forum.tezosagora.org/t/bakers-registry-discussion/1238/55
 *
 * Compatible with the contract as of December 22, 2019.
 */ export namespace TCFBakerRegistryHelper { param server param address export async function verifyDestination ( server , address ) { const contract = await TezosNodeReader . getAccountForBlock ( server , 'head' , address ) ; if ( ! ! ! contract . script ) { throw new Error ( `No code found at ${ address }` ) ; } const k = Buffer . from ( blakejs . blake2s ( contract [ 'script' ] . toString ( ) , null , 16 ) ) . toString ( 'hex' ) ; if ( k !== '1527ddf08bdf582dce0b28c051044897' ) { throw new Error ( `Contract at ${ address } does not match the expected code hash` ) ; } return true ; } param server param address export async function getSimpleStorage ( server , address ) { const storageResult = await TezosNodeReader . getContractStorage ( server , address ) ; return { mapid : parseInt ( JSONPath ( { path : '$.args[0].int' , json : storageResult } ) [ 0 ] ) , owner : JSONPath ( { path : '$.args[1].args[0].string' , json : storageResult } ) [ 0 ] , signupFee : parseInt ( JSONPath ( { path : '$.args[1].args[1].args[0].int' , json : storageResult } ) [ 0 ] ) , updateFee : parseInt ( JSONPath ( { path : '$.args[1].args[1].args[1].int' , json : storageResult } ) [ 0 ] ) } ; } param server param address param baker param name param isAcceptingDelegation param detailsURL param payoutShare export async function updateRegistration ( server , address , baker , name , isAcceptingDelegation , detailsURL , payoutShare ) { } param server param mapid param baker export async function queryRegistration ( server , mapid , baker ) { const key = TezosMessageUtils . encodeBigMapKey ( Buffer . from ( TezosMessageUtils . writePackedData ( baker , 'key_hash' ) , 'hex' ) ) ; const mapResult = await TezosNodeReader . getValueForBigMapKey ( server , mapid , key ) ; if ( ! ! ! mapResult ) { return undefined ; } const textDecoder = new TextDecoder ( ) ; const paymentConfigMask = Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[1].args[0].args[1].int' , json : mapResult } ) [ 0 ] ) ; return { name : textDecoder . decode ( Buffer . from ( JSONPath ( { path : '$.args[0].args[0].args[0].args[0].args[0].args[0].bytes' , json : mapResult } ) [ 0 ] , 'hex' ) ) , isAcceptingDelegation : Boolean ( JSONPath ( { path : '$.args[0].args[0].args[0].args[0].args[0].args[1].prim' , json : mapResult } ) [ 0 ] ) , externalDataURL : textDecoder . decode ( Buffer . from ( JSONPath ( { path : '$.args[0].args[0].args[0].args[0].args[1].bytes' , json : mapResult } ) [ 0 ] , 'hex' ) ) , split : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[0].args[0].int' , json : mapResult } ) [ 0 ] ) / 10000 , paymentAccounts : JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[0].args[1]..string' , json : mapResult } ) , minimumDelegation : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[0].args[0].args[0].int' , json : mapResult } ) [ 0 ] ) , isGreedy : Boolean ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[0].args[0].args[1].prim' , json : mapResult } ) [ 0 ] ) , payoutDelay : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[0].args[1].args[0].int' , json : mapResult } ) [ 0 ] ) , payoutFrequency : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[0].args[1].args[1].args[0].int' , json : mapResult } ) [ 0 ] ) , minimumPayout : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[0].args[1].args[1].args[1].int' , json : mapResult } ) [ 0 ] ) , isCheap : Boolean ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[1].args[0].args[0].prim' , json : mapResult } ) [ 0 ] ) , paymentConfig : { payForOwnBlocks : Boolean ( paymentConfigMask & 1 ) , payForEndorsements : Boolean ( paymentConfigMask & 2 ) , payGainedFees : Boolean ( paymentConfigMask & 4 ) , payForAccusationGains : Boolean ( paymentConfigMask & 8 ) , subtractLostDepositsWhenAccused : Boolean ( paymentConfigMask & 16 ) , subtractLostRewardsWhenAccused : Boolean ( paymentConfigMask & 32 ) , subtractLostFeesWhenAccused : Boolean ( paymentConfigMask & 64 ) , payForRevelation : Boolean ( paymentConfigMask & 128 ) , subtractLostRewardsWhenMissRevelation : Boolean ( paymentConfigMask & 256 ) , subtractLostFeesWhenMissRevelation : Boolean ( paymentConfigMask & 512 ) , compensateMissedBlocks : ! Boolean ( paymentConfigMask & 1024 ) , payForStolenBlocks : Boolean ( paymentConfigMask & 2048 ) , compensateMissedEndorsements : ! Boolean ( paymentConfigMask & 4096 ) , compensateLowPriorityEndorsementLoss : ! Boolean ( paymentConfigMask & 8192 ) } , overdelegationThreshold : Number ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[1].args[1].args[0].int' , json : mapResult } ) [ 0 ] ) , subtractRewardsFromUninvitedDelegation : Boolean ( JSONPath ( { path : '$.args[0].args[0].args[0].args[1].args[1].args[1].args[1].args[1].prim' , json : mapResult } ) [ 0 ] ) , recordManager : JSONPath ( { path : '$.args[0].args[1].args[0].string' , json : mapResult } ) [ 0 ] , timestamp : new Date ( JSONPath ( { path : '$.args[1].string' , json : mapResult } ) [ 0 ] ) } ; } }