decodeNprofile static method
Decode nprofile and return Nprofile object
Implementation
static Nprofile decodeNprofile(String nprofileStr) {
var decoder = Bech32Decoder();
var bech32Result = decoder.convert(nprofileStr, nprofileStr.length);
if (bech32Result.hrp != Hrps.kNprofile) {
throw ArgumentError(
"Invalid HRP: expected '${Hrps.kNprofile}', got '${bech32Result.hrp}'");
}
var data = Nip19Utils.convertBits(bech32Result.data, 5, 8, false);
final tlv = Nip19TLV.parseTLV(data);
String? pubkey;
List<String> relays = [];
for (var t in tlv) {
switch (t.type) {
case 0: // pubkey (special)
if (t.value.length == 32) {
pubkey = hex.encode(t.value);
}
break;
case 1: // relay
try {
relays.add(utf8.decode(t.value));
} catch (e) {
// Ignore invalid UTF-8 per spec
}
break;
default:
// Ignore unrecognized TLV types per spec
break;
}
}
// Validate required fields
if (pubkey == null) {
throw ArgumentError('Missing required pubkey field (type 0)');
}
return Nprofile(
pubkey: pubkey,
relays: relays.isEmpty ? null : relays,
);
}