Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

197 token freeze kyc and pause na #237

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,46 @@ async function getHtsStorageAt(address, requestedSlot, blockNumber, mirrorNodeCl

let unresolvedValues = persistentStorage.load(tokenId, blockNumber, nrequestedSlot);
if (unresolvedValues === undefined) {
const token = await mirrorNodeClient.getTokenById(tokenId, blockNumber);
const token =
/**@type{{token_keys: {key_type: string}[]}}*/ await mirrorNodeClient.getTokenById(
tokenId,
blockNumber
);
if (token === null) return ret(ZERO_HEX_32_BYTE, `Token \`${tokenId}\` not found`);
unresolvedValues = slotMapOf(token).load(nrequestedSlot);

for (const key of ['admin', 'kyc', 'freeze', 'wipe', 'supply', 'fee_schedule', 'pause']) {
const value = /**@type{{contractId: string}}*/ (token[`${key}_key`]);
if (!value) continue;
assert(typeof value === 'object' && 'key' in value && typeof value.key === 'string');
const result = await mirrorNodeClient.getAccountsByPublicKey(value.key);
if (result === undefined || !result || result.accounts.length === 0) continue;
value.contractId = result.accounts[0].evm_address;
// Encoded `address(tokenId).getKeyOwner(tokenId, keyType)` slot
// slot(256) = `getKeyOwner`selector(32) + padding(192) + keyType(32)
if (
nrequestedSlot >> 32n ===
0xc3c18f7c_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000n
) {
const keyType = parseInt(requestedSlot.slice(-1), 16);
const keys =
/**@type{Array<{key_type: number, key: string, _e_c_d_s_a_secp256k1: string, ed25519: string}>}*/ (
token['token_keys']
);
const noKeyRes = ret(
ZERO_HEX_32_BYTE,
`Token ${tokenId} has not set the owner of the key ${keyType}`
);
const keyFound = keys['find'](
(
/**@type{{key_type: number, key: string, _e_c_d_s_a_secp256k1: string, ed25519: string}}*/ key
) => key.key_type === keyType
);
if (keyFound === undefined) return noKeyRes;
await mirrorNodeClient.getAccountsByPublicKey(keyFound['key']);
const keyString = keyFound['_e_c_d_s_a_secp256k1'] || keyFound['ed25519'];
if (!keyString) return noKeyRes;
const result = await mirrorNodeClient.getAccountsByPublicKey(keyString);
if (result === undefined || !result || result.accounts.length === 0) return noKeyRes;
return ret(
result.accounts[0].evm_address,
`Token ${tokenId} has set the owner of the key ${keyType}: ${result.accounts[0].evm_address}`
);
}

unresolvedValues = slotMapOf(token).load(nrequestedSlot);

if (unresolvedValues === undefined)
return ret(ZERO_HEX_32_BYTE, `Requested slot does not match any field slots`);
}
Expand Down
34 changes: 8 additions & 26 deletions src/slotmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,10 @@ const _types = {
t_int64: str => [toIntHex256(str ?? 0)],
t_address: str => [
str
? async (mirrorNode, blockNumber) => {
return str.startsWith('0x')
? str.substring(2).padStart(64, '0')
: mirrorNode
.getAccount(str, blockNumber)
.then(acc =>
toIntHex256(acc?.evm_address ?? str?.replace('0.0.', '') ?? 0)
);
}
? (mirrorNode, blockNumber) =>
mirrorNode
.getAccount(str, blockNumber)
.then(acc => toIntHex256(acc?.evm_address ?? str?.replace('0.0.', '') ?? 0))
: toIntHex256(0),
],
t_bool: value => [toIntHex256(value ? 1 : 0)],
Expand Down Expand Up @@ -246,26 +241,13 @@ function slotMapOf(token) {
['fee_schedule_key', 0x20],
['pause_key', 0x40],
]).map(([prop, key_type]) => {
const key = /**@type{{contractId: string}}*/ (token[prop]);
const key = token[prop];
if (key === null) return { key_type, ed25519: '', _e_c_d_s_a_secp256k1: '' };
assert(typeof key === 'object');
assert('_type' in key && 'key' in key && typeof key.key === 'string');
if (!key.contractId && key.key) {
key.contractId = `0x${keccak256(Buffer.from(key.key, 'utf-8')).slice(-40).padStart(64, '0')}`;
}
assert('_type' in key && 'key' in key);
if (key._type === 'ED25519')
return {
key_type,
contract_id: key.contractId,
ed25519: key.key,
_e_c_d_s_a_secp256k1: '',
};
return {
key_type,
contract_id: key.contractId,
ed25519: '',
_e_c_d_s_a_secp256k1: key.key,
};
return { key_type, ed25519: key.key, _e_c_d_s_a_secp256k1: '' };
return { key_type, ed25519: '', _e_c_d_s_a_secp256k1: key.key };
});
const customFees = /**@type {Record<string, Record<string, unknown>[]>}*/ (
token['custom_fees']
Expand Down
Loading