Skip to content

NKN Wallet Specification: Wallet Address Format

gdmmx edited this page Mar 30, 2021 · 10 revisions

Wallet Address Format as below:

Prefix Uint160 Uint32
Fool-proof design 160 bits Hash of RedeemScript Checksum
  • Fool-proof:
Fix 0x35 in v0.8 for base58.encode got string start with "N" 
Fix 0x02b825 in v0.9 for base58.encode got string start with "NKN"
  • 160 bits Hash of RedeemScript:
generate RedeemScript vmCode from publicKey, and hash it with ripemd160
  • Checksum:
SHA256 hash twice with (Prefix + Uint160), retain lowest 32bits

Valid address range

As part #1 definition, the valid address range (without consider Checksum) should be:

Minimal Maximum
Expr (0x2b825<<192) | (0^160)<<32 | 0^32 (0x2b825<<192) | ((1<<160)-1)<<32 | (1<<32)-1
Hex 0x2b8250000000000...00000000000000L 0x2b825ffffffffffff...ffffffffffffffffffffL
Base58 NKNBV71h5oUBjknMHPikPU7nraw9KtkegU1m NKNaphzoP6BNCdbnRUkAinbv8NZeaqTtGJL6

Process of convert public Key to wallet address

  1. publicKey
  2. generated RedeemScript vmCode
  3. sha256 & ripemd160 RedeemScript got 160bits Hash
  4. calc Checksum of (Prefix + 160 bits Hash)
  5. append Checksum
  6. base58.encode

Convertor Example

https://github.com/nknorg/nkn/blob/master/util/pubkey2addr/pubkey2addr.go#L12

go run util/pubkey2addr/pubkey2addr.go 1111111122222222333333334444444455555555666666667777777788888888
NKNRyqyBLzghJWGwogJTKNFM5WUnoqopiTYV
if pk, err := hex.DecodeString(pubkeyHexStr); err == nil {
	if err = crypto.CheckPublicKey(pk); err == nil {
		if redeemHash, err := program.CreateProgramHash(pk); err == nil {
			return redeemHash.ToAddress()
		}
	}
}