Example PyTeal name/ID validity check

The following PyTeal code can be used to validate that the specified NFD name matches the passed NFD App ID and that it's an authentic NFD.

# is_nfd_id_authentic composes the name lookup LSIG and checks if the app id stored in the passed-in NFD name's
# registry-contract local state matches the passed Nfd App ID.
# ie: patrick.algo -> check registry contract discovery mechanism -> read app id.. does it match.
@Subroutine(TealType.uint64)
def is_nfd_id_authentic(nfd_name, nfd_app_id):
    lsigByteCode = ScratchVar(TealType.bytes)
    nameBytes = ScratchVar(TealType.bytes)
    progHash = ScratchVar(TealType.bytes)

    localGetMv = App.localGetEx(progHash.load(), Tmpl.Int("TMPL_NFD_RC_ID"), REGISTRY_KEY_APPID)
    return Seq(
        nameBytes.store(Concat(Bytes("name/"), nfd_name)),

        lsigByteCode.store(Concat(Bytes("base16", "0x052001018008010203040506"),
                                  Bytes("base16", "0x070817350031183400123110"),
                                  Bytes("base16", "0x810612103119221231198100"),
                                  Bytes("base16", "0x1211104000010022432601"))),
        # Now replace bytes 6-13 w/ NFD's registry contract id
        lsigByteCode.store(Replace(lsigByteCode.load(), Int(6), Itob(Tmpl.Int("TMPL_NFD_RC_ID")))),
        # then concat {length}name/{nfd_name} - uvar int (just last byte in BE value) - then bytes of string
        lsigByteCode.store(
            Concat(lsigByteCode.load(), encode_uvarint(Len(nameBytes.load())), nameBytes.load())),
        # we can now hash these bytes to get our LSIG address
        progHash.store(Sha512_256(Concat(Bytes("Program"), lsigByteCode.load()))),
        localGetMv,
        Assert(localGetMv.hasValue(),
               comment="lsig has to have i.appid localstate"),
        Return(Btoi(localGetMv.value()) == nfd_app_id))

See Registry Application IDsfor the correct registry ID to substitute in the TMPL_NFD_RC_ID parameter.

Last updated