Skip to content

Commit

Permalink
feat: calculate contract ID
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed May 27, 2019
1 parent 225da65 commit a072966
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions aeternity/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"math/big"
"strings"

"github.com/aeternity/aepp-sdk-go/rlp"
Expand Down Expand Up @@ -114,6 +115,25 @@ func buildOracleQueryID(sender string, senderNonce uint64, recipient string) (id
return
}

func buildContractID(sender string, senderNonce uint64) (ctID string, err error) {
senderBin, err := Decode(sender)
if err != nil {
return ctID, err
}

l := big.Int{}
l.SetUint64(senderNonce)

ctIDUnhashed := append(senderBin, l.Bytes()...)
ctIDHashed, err := hash(ctIDUnhashed)
if err != nil {
return ctID, err
}

ctID = Encode(PrefixContractPubkey, ctIDHashed)
return ctID, err
}

// Namehash calculate the Namehash of a string
// TODO: link to the
func Namehash(name string) []byte {
Expand Down
62 changes: 62 additions & 0 deletions aeternity/hashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,65 @@ func Test_leftPadByteSlice(t *testing.T) {
})
}
}

func Test_buildContractID(t *testing.T) {
type args struct {
sender string
senderNonce uint64
}
tests := []struct {
name string
args args
wantCtID string
wantErr bool
}{
{
name: "Genesis address, nonce 1",
args: args{
sender: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
senderNonce: uint64(1),
},
wantCtID: "ct_2pfWWzeRzWSdm68HXZJn61KhxdsBA46wzYgvo1swkdJZij1rKm",
wantErr: false,
},
{
name: "Genesis address, nonce 5",
args: args{
sender: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
senderNonce: uint64(5),
},
wantCtID: "ct_223vybq7Ljr2VKaVhRyveFoSJMBZ8CyBCpPAFZ1BxgvMXggAA",
wantErr: false,
},
{
name: "Genesis address, nonce 256",
args: args{
sender: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
senderNonce: uint64(256),
},
wantCtID: "ct_FT6XgwatDufGJ2RUaLkMmnebfVHNju5YK7cbjnbtby8LwdcJB",
wantErr: false,
},
{
name: "Genesis address, nonce 65536",
args: args{
sender: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
senderNonce: uint64(65536),
},
wantCtID: "ct_vuq6dPXiAgMuGfVvFveL6j3kEPJC32orJmaG5zL1oHgT3WCLB",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCtID, err := buildContractID(tt.args.sender, tt.args.senderNonce)
if (err != nil) != tt.wantErr {
t.Errorf("buildContractID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotCtID != tt.wantCtID {
t.Errorf("buildContractID() = %v, want %v", gotCtID, tt.wantCtID)
}
})
}
}

0 comments on commit a072966

Please sign in to comment.