SHA256
66 lines
2.9 KiB
JavaScript
66 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Creates a reviewable inventory of every SHiNE user record PDA currently
|
|
* owned by shine_users. It never sends a transaction.
|
|
*
|
|
* node scripts/registration-v2/list-user-pdas-for-reset.mjs \
|
|
* --rpc https://api.devnet.solana.com --output /tmp/shine-users-devnet.json
|
|
*/
|
|
import { writeFileSync } from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
const require = createRequire(import.meta.url);
|
|
const { Connection, PublicKey } = require('@solana/web3.js');
|
|
|
|
const PROGRAM_ID = new PublicKey('SHiNEPr1APdAgNBteUyBXcNovaHctpSjUu8oH2ZJdN6');
|
|
const MAGIC = Buffer.from('SHiNE');
|
|
|
|
function argsOf(argv) {
|
|
const out = {};
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const key = argv[i];
|
|
if (!key.startsWith('--') || !argv[i + 1] || argv[i + 1].startsWith('--')) throw Error('Expected --rpc and --output');
|
|
const name = key.slice(2);
|
|
if (out[name] !== undefined) throw Error('Repeated --' + name);
|
|
out[name] = argv[++i];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function readLogin(raw) {
|
|
// All historical user records that can be reset have this signed prefix:
|
|
// header(9), created(8), updated(8), number(4), previous hash(32), login.
|
|
const loginLengthOffset = 9 + 8 + 8 + 4 + 32;
|
|
if (raw.length <= loginLengthOffset) return null;
|
|
const length = raw[loginLengthOffset];
|
|
const start = loginLengthOffset + 1;
|
|
const end = start + length;
|
|
if (end > raw.length || length === 0) return null;
|
|
const login = raw.subarray(start, end).toString('utf8');
|
|
return Buffer.from(login, 'utf8').equals(raw.subarray(start, end)) ? login : null;
|
|
}
|
|
|
|
async function main() {
|
|
const a = argsOf(process.argv.slice(2));
|
|
if (!a.rpc || !a.output) throw Error('Specify --rpc and --output');
|
|
const url = new URL(a.rpc);
|
|
if (!['http:', 'https:'].includes(url.protocol)) throw Error('RPC must use HTTP(S)');
|
|
const conn = new Connection(a.rpc, 'confirmed');
|
|
const accounts = await conn.getProgramAccounts(PROGRAM_ID, { commitment: 'confirmed' });
|
|
const records = accounts
|
|
.filter(({ account }) => account.data.length >= 9 && account.data.subarray(0, 5).equals(MAGIC) && account.data[5] === 1)
|
|
.map(({ pubkey, account }) => ({
|
|
pda: pubkey.toBase58(),
|
|
login: readLogin(account.data),
|
|
format: `${account.data[5]}.${account.data[6]}`,
|
|
lamports: account.lamports.toString(),
|
|
dataLength: account.data.length,
|
|
}))
|
|
.sort((a, b) => a.pda.localeCompare(b.pda));
|
|
const unreadable = records.filter(({ login }) => login === null);
|
|
const output = { programId: PROGRAM_ID.toBase58(), generatedAt: new Date().toISOString(), records };
|
|
writeFileSync(a.output, JSON.stringify(output, null, 2) + '\n', { encoding: 'utf8', flag: 'wx' });
|
|
console.log(JSON.stringify({ output: a.output, candidates: records.length, unreadable: unreadable.length }, null, 2));
|
|
if (unreadable.length) process.exitCode = 2;
|
|
}
|
|
main().catch((e) => { console.error('Error:', e.message); process.exitCode = 1; });
|