Skip to content

Commit

Permalink
feat: storing keys in memory (#197)
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Jasuwienas <[email protected]>
  • Loading branch information
arianejasuwienas committed Feb 12, 2025
1 parent 5f561c5 commit 087a7d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
34 changes: 24 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,34 @@ 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: string}>}*/(token['token_keys'])deej;
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) => key.key_type === keyType);
await mirrorNodeClient.getAccountsByPublicKey(keyFound);
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

0 comments on commit 087a7d9

Please sign in to comment.