Skip to content

nodech/go-hsd-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Handshake Golang utilities

Proof

You can verify proofs form JSON or Raw encoded.

Example:

package main

import (
	"fmt"

	"github.com/nodech/go-hsd-utils/proof"
)

var json = []byte(`{
	"type": "TYPE_EXISTS",
	"depth": 7,
	"nodes": [
		[
			"",
			"b38bd3162de23fd4242511f3d96518b41fb5ceaba1b2604718e9ff4905decc96"
		],
		[
			"",
			"a725d93e5424af84461ac485543bd9fc84875ef8b9eccdaec714cecc03cea73b"
		],
		[
			"",
			"ee019110c7f43813690f676307494281bb4d30e9b96334dc561ea506413827fb"
		],
		[
			"1",
			"d8fcc02cfdf8f1ab87ddddf80d1ab24e2bbc73b8ccf2e48e0e5b45b0a68b2ae2"
		],
		[
			"",
			"cee75163e06169984dff42cbce4ce516eeda81135f4f2b41899df4a532bb7184"
		],
		[
			"",
			"09daecd329756ed5e608f784903e4ec0570d8f30a1c9cfd7c6c3b8d997c47a57"
		]
	],
	"value": "68656c6c6f2032"
}`)

var data = []byte{
	0x07, 0xc0, 0x06, 0x00, 0x10, 0xb3, 0x8b, 0xd3, 0x16, 0x2d, 0xe2, 0x3f, 0xd4, 0x24, 0x25, 0x11,
	0xf3, 0xd9, 0x65, 0x18, 0xb4, 0x1f, 0xb5, 0xce, 0xab, 0xa1, 0xb2, 0x60, 0x47, 0x18, 0xe9, 0xff,
	0x49, 0x05, 0xde, 0xcc, 0x96, 0xa7, 0x25, 0xd9, 0x3e, 0x54, 0x24, 0xaf, 0x84, 0x46, 0x1a, 0xc4,
	0x85, 0x54, 0x3b, 0xd9, 0xfc, 0x84, 0x87, 0x5e, 0xf8, 0xb9, 0xec, 0xcd, 0xae, 0xc7, 0x14, 0xce,
	0xcc, 0x03, 0xce, 0xa7, 0x3b, 0xee, 0x01, 0x91, 0x10, 0xc7, 0xf4, 0x38, 0x13, 0x69, 0x0f, 0x67,
	0x63, 0x07, 0x49, 0x42, 0x81, 0xbb, 0x4d, 0x30, 0xe9, 0xb9, 0x63, 0x34, 0xdc, 0x56, 0x1e, 0xa5,
	0x06, 0x41, 0x38, 0x27, 0xfb, 0x01, 0x80, 0xd8, 0xfc, 0xc0, 0x2c, 0xfd, 0xf8, 0xf1, 0xab, 0x87,
	0xdd, 0xdd, 0xf8, 0x0d, 0x1a, 0xb2, 0x4e, 0x2b, 0xbc, 0x73, 0xb8, 0xcc, 0xf2, 0xe4, 0x8e, 0x0e,
	0x5b, 0x45, 0xb0, 0xa6, 0x8b, 0x2a, 0xe2, 0xce, 0xe7, 0x51, 0x63, 0xe0, 0x61, 0x69, 0x98, 0x4d,
	0xff, 0x42, 0xcb, 0xce, 0x4c, 0xe5, 0x16, 0xee, 0xda, 0x81, 0x13, 0x5f, 0x4f, 0x2b, 0x41, 0x89,
	0x9d, 0xf4, 0xa5, 0x32, 0xbb, 0x71, 0x84, 0x09, 0xda, 0xec, 0xd3, 0x29, 0x75, 0x6e, 0xd5, 0xe6,
	0x08, 0xf7, 0x84, 0x90, 0x3e, 0x4e, 0xc0, 0x57, 0x0d, 0x8f, 0x30, 0xa1, 0xc9, 0xcf, 0xd7, 0xc6,
	0xc3, 0xb8, 0xd9, 0x97, 0xc4, 0x7a, 0x57, 0x07, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x32,
}

var root = proof.UrkelHash{
	0x4e, 0xc8, 0x36, 0x28, 0x34, 0x1d, 0x5b, 0x97, 0xa6, 0x25, 0x9e, 0x06, 0x9d, 0xc5, 0xd8, 0x3a,
	0xf7, 0x74, 0x46, 0x30, 0x1f, 0xaf, 0xcb, 0x39, 0x37, 0x72, 0xe3, 0xe9, 0x08, 0x29, 0xec, 0x41,
}

var key = proof.UrkelHash{
	0x76, 0x72, 0x60, 0x46, 0xe2, 0xdb, 0x69, 0x01, 0x79, 0xb6, 0xbe, 0x0e, 0x81, 0x34, 0x80, 0x68,
	0xb1, 0x5c, 0x13, 0x23, 0xf5, 0xd0, 0x2e, 0xd3, 0x9b, 0x24, 0x84, 0x2f, 0xb1, 0x94, 0x72, 0x62,
}

func verify(nameProof *proof.Proof, root proof.UrkelHash, key proof.UrkelHash) {
	code, value := nameProof.Verify(root, key)

	if code != proof.ProofOk {
		fmt.Println("Proof verification failed")
	} else {
		fmt.Println("Proof verification succeeded")
	}

	if code == proof.ProofOk && value != nil {
		fmt.Println("Value:", value)
	}
}

func main() {
	nameProof, err := proof.NewFromBytes(data)

	if err != nil {
		panic(err)
	}

	verify(nameProof, root, key)

	nameProof, err = proof.NewFromJSON(json)

	if err != nil {
		panic(err)
	}

	verify(nameProof, root, key)
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages