33 lines
942 B
JavaScript
33 lines
942 B
JavaScript
const crypto = require('crypto');
|
|
|
|
/**
|
|
* Converts a Nostr public key (hex string) to a FIPS IPv6 address.
|
|
*
|
|
* @param {string} pubkeyHex - The 32-byte (64 hex characters) Nostr public key.
|
|
* @returns {string} - The resulting IPv6 address.
|
|
*/
|
|
function pubkeyToFipsIpv6(pubkeyHex) {
|
|
// 1. Convert hex string to buffer
|
|
const pubkeyBytes = Buffer.from(pubkeyHex, 'hex');
|
|
|
|
// 2. Compute SHA-256(pubkey)
|
|
const hash = crypto.createHash('sha256').update(pubkeyBytes).digest();
|
|
|
|
// 3. Take the first 16 bytes for the NodeAddr
|
|
const nodeAddr = hash.slice(0, 16);
|
|
|
|
// 4. Set the first byte to 0xfd (FIPS prefix)
|
|
nodeAddr[0] = 0xfd;
|
|
|
|
// 5. Format as an IPv6 address string
|
|
const ipv6Groups = [];
|
|
for (let i = 0; i < 16; i += 2) {
|
|
const group = nodeAddr.readUInt16BE(i).toString(16);
|
|
ipv6Groups.push(group);
|
|
}
|
|
|
|
return ipv6Groups.join(':');
|
|
}
|
|
|
|
module.exports = { pubkeyToFipsIpv6 };
|