gasgasgas/js/registry.js

43 lines
2.7 KiB
JavaScript

const registryInterface = [{"inputs":[{"internalType":"bytes32[]","name":"_identifiers","type":"bytes32[]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"_key","type":"bytes32"},{"indexed":false,"internalType":"address","name":"_address","type":"address"}],"name":"AddressKey","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"_newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"}],"name":"addressOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"identifier","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"identifierCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_identifier","type":"bytes32"},{"internalType":"address","name":"_address","type":"address"}],"name":"set","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}];
function fromIdentifier(s) {
let r = '';
if (s.substr(0, 2) == '0x') {
s = s.substr(2);
}
for (let i = 0; i < 64; i += 2) {
const v = s.substring(i, i+2);
if (v == '00') {
break;
}
r += String.fromCharCode(parseInt(v, 16));
console.log('v', v, r);
}
return r;
}
async function scanRegistry(registry) {
const o = new ethers.Contract(registry, registryInterface, current_provider);
let i = 0;
let r = {};
while (true) {
let identifier;
try {
identifier = await o.identifier(i);
} catch(e) {
break;
}
if (!identifier) {
break;
}
const v = await o.addressOf(identifier);
const identifierString = fromIdentifier(identifier);
r[identifierString] = v;
console.debug('found ' + identifierString + ' = ' + v + ' in registry ' + registry);
i++;
}
return r;
}