encodeNprofile static method
Encode nprofile (profile reference)
pubkey - 32-byte hex public key (required)
relays - optional list of relay URLs where the profile may be found
Implementation
static String encodeNprofile({
required String pubkey,
List<String>? relays,
}) {
final tlvData = <int>[];
// Type 0: pubkey (special) - 32 bytes
final pubkeyBytes = hex.decode(pubkey);
if (pubkeyBytes.length != 32) {
throw ArgumentError('Public key must be 32 bytes (64 hex characters)');
}
tlvData.add(0); // type
tlvData.add(32); // length
tlvData.addAll(pubkeyBytes); // value
// Type 1: relays (optional, can be multiple)
if (relays != null) {
for (final relay in relays) {
final relayBytes = utf8.encode(relay);
if (relayBytes.length > 255) {
throw ArgumentError(
'Relay URL too long: ${relay.length} bytes (max 255)');
}
tlvData.add(1); // type
tlvData.add(relayBytes.length); // length
tlvData.addAll(relayBytes); // value
}
}
// Convert to bech32
final data = Nip19Utils.convertBits(tlvData, 8, 5, true);
var encoder = Bech32Encoder();
Bech32 input = Bech32(Hrps.kNprofile, data);
var encoded = encoder.convert(input, input.hrp.length + data.length + 10);
return encoded;
}