From 4881ad9211360da8b2256fdb05a75614aa6b7291 Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Fri, 5 Apr 2019 15:17:37 +0200 Subject: [PATCH] chore: separate salt generation from calculating the commitmentId, so we can unittest commitmentId generation against GET /v2/debug/names/commitment-id --- aeternity/hashing.go | 14 +++++++--- aeternity/hashing_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/aeternity/hashing.go b/aeternity/hashing.go index 9ef6657a..b8f7a68d 100644 --- a/aeternity/hashing.go +++ b/aeternity/hashing.go @@ -110,16 +110,22 @@ func uuidV4() (u string) { return fmt.Sprint(uuid.NewV4()) } -// naming -func computeCommitmentID(name string) (ch string, salt []byte, err error) { +// since the salt is a uint256, which Erlang handles well, but Go has nothing similar to it, +// it is imperative that the salt be kept as a bytearray unless you really have to convert it +// into an integer. Which you usually don't, because it's a salt. +func generateCommitmentID(name string) (ch string, salt []byte, err error) { salt, err = randomBytes(32) if err != nil { return } - // TODO: this is done using the api (concatenating ) + + ch, err = computeCommitmentID(name, salt) + return ch, salt, err +} + +func computeCommitmentID(name string, salt []byte) (ch string, err error) { nh := append(Namehash(name), salt...) nh, _ = hash(nh) - // nh := namehash(name) ch = Encode(PrefixCommitment, nh) return } diff --git a/aeternity/hashing_test.go b/aeternity/hashing_test.go index 6ff9eff0..d1f79d94 100644 --- a/aeternity/hashing_test.go +++ b/aeternity/hashing_test.go @@ -186,3 +186,58 @@ func TestGenerate(t *testing.T) { }) } } + +func Test_computeCommitmentID(t *testing.T) { + type args struct { + name string + salt []byte + } + tests := []struct { + name string + args args + wantCh string + wantErr bool + }{ + { + name: "fdsa.test, 0", + args: args{ + name: "fdsa.test", + salt: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + wantCh: "cm_2jJov6dn121oKkHo6TuWaAAL4ZEMonnCjpo8jatkCixrLG8Uc4", + wantErr: false, + }, + { + name: "fdsa.test, 255", + args: args{ + name: "fdsa.test", + salt: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255}, + }, + wantCh: "cm_sa8UUjorPzCTLfYp6YftR4jwF4kPaZVsoP5bKVAqRw9zm43EE", + wantErr: false, + }, + { + // erlang Eshell: rp(<<9795159241593061970:256>>). + name: "fdsa.test, 9795159241593061970 (do not use Golang to convert salt integers)", + args: args{ + name: "fdsa.test", + salt: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 239, 101, 110, 233, 138, 2, 82}, + }, + wantCh: "cm_QhtcYow8krP3xQSTsAhFihfBstTjQMiApaPCgZuciDHZmMNtZ", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // fmt.Println(saltBytes) + gotCh, err := computeCommitmentID(tt.args.name, tt.args.salt) + if (err != nil) != tt.wantErr { + t.Errorf("computeCommitmentID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotCh != tt.wantCh { + t.Errorf("computeCommitmentID() = %v, want %v", gotCh, tt.wantCh) + } + }) + } +}