Skip to content

Commit

Permalink
fix: NameClaimTx salt 256bit bytearray was being converted to uint64 …
Browse files Browse the repository at this point in the history
…and back, which mangles the bytes. Solved by making it a BigInt, which has proper 32 byte big endian representation with Bytes()
  • Loading branch information
randomshinichi committed Apr 5, 2019
1 parent 88e7ac4 commit 45ff92d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
11 changes: 8 additions & 3 deletions aeternity/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/aeternity/aepp-sdk-go/rlp"
"github.com/aeternity/aepp-sdk-go/utils"
"github.com/btcsuite/btcutil/base58"
uuid "github.com/satori/go.uuid"
"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -113,13 +114,17 @@ func uuidV4() (u string) {
// 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)
func generateCommitmentID(name string) (ch string, salt *utils.BigInt, err error) {
saltBytes, err := randomBytes(32)
if err != nil {
return
}

ch, err = computeCommitmentID(name, salt)
ch, err = computeCommitmentID(name, saltBytes)

salt = utils.NewBigInt()
salt.SetBytes(saltBytes)

return ch, salt, err
}

Expand Down
16 changes: 7 additions & 9 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aeternity

import (
"encoding/binary"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -132,33 +131,32 @@ func (ae *Ae) BroadcastTransaction(txSignedBase64 string) (err error) {
return
}

// NamePreclaimTx creates a name preclaim transaction and nameSalt (required for claiming)
// NamePreclaimTx creates a name preclaim transaction and salt (required for claiming)
// It should return the Tx struct, not the base64 encoded RLP, to ease subsequent inspection.
func (n *Aens) NamePreclaimTx(name string, fee utils.BigInt) (tx NamePreclaimTx, nameSalt uint64, err error) {
func (n *Aens) NamePreclaimTx(name string, fee utils.BigInt) (tx NamePreclaimTx, nameSalt *utils.BigInt, err error) {
txTTL, accountNonce, err := getTTLNonce(n.nodeClient, n.owner.Address, Config.Client.TTL)
if err != nil {
return
}

// calculate the commitment and get the preclaim salt
cm, salt, err := computeCommitmentID(name)
// since the salt is 32 bytes long, you must use a big.Int to convert it into an integer
cm, nameSalt, err := generateCommitmentID(name)
if err != nil {
return NamePreclaimTx{}, 0, err
return NamePreclaimTx{}, utils.NewBigInt(), err
}
// convert the salt back into uint64 from binary
nameSalt = binary.BigEndian.Uint64(salt)

// build the transaction
tx = NewNamePreclaimTx(n.owner.Address, cm, fee, txTTL, accountNonce)
if err != nil {
return NamePreclaimTx{}, 0, err
return NamePreclaimTx{}, utils.NewBigInt(), err
}

return
}

// NameClaimTx creates a claim transaction
func (n *Aens) NameClaimTx(name string, nameSalt uint64, fee utils.BigInt) (tx NameClaimTx, err error) {
func (n *Aens) NameClaimTx(name string, nameSalt utils.BigInt, fee utils.BigInt) (tx NameClaimTx, err error) {
txTTL, accountNonce, err := getTTLNonce(n.nodeClient, n.owner.Address, Config.Client.TTL)
if err != nil {
return
Expand Down
8 changes: 4 additions & 4 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func NewNamePreclaimTx(accountID, commitmentID string, fee utils.BigInt, ttl, no
type NameClaimTx struct {
AccountID string
Name string
NameSalt uint64
NameSalt utils.BigInt
Fee utils.BigInt
TTL uint64
Nonce uint64
Expand All @@ -229,7 +229,7 @@ func (t *NameClaimTx) RLP() (rlpRawMsg []byte, err error) {
aID,
t.Nonce,
t.Name,
t.NameSalt,
t.NameSalt.Int,
t.Fee.Int,
t.TTL)
return
Expand All @@ -244,7 +244,7 @@ func (t *NameClaimTx) JSON() (string, error) {
AccountID: models.EncodedHash(t.AccountID),
Fee: t.Fee,
Name: &nameAPIEncoded,
NameSalt: &t.NameSalt,
NameSalt: t.NameSalt,
Nonce: t.Nonce,
TTL: t.TTL,
}
Expand All @@ -254,7 +254,7 @@ func (t *NameClaimTx) JSON() (string, error) {
}

// NewNameClaimTx is a constructor for a NameClaimTx struct
func NewNameClaimTx(accountID, name string, nameSalt uint64, fee utils.BigInt, ttl, nonce uint64) NameClaimTx {
func NewNameClaimTx(accountID, name string, nameSalt utils.BigInt, fee utils.BigInt, ttl, nonce uint64) NameClaimTx {
return NameClaimTx{accountID, name, nameSalt, fee, ttl, nonce}
}

Expand Down
6 changes: 3 additions & 3 deletions aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestNameClaimTx_RLP(t *testing.T) {
type fields struct {
AccountID string
Name string
NameSalt uint64
NameSalt utils.BigInt
Fee utils.BigInt
TTL uint64
Nonce uint64
Expand All @@ -159,12 +159,12 @@ func TestNameClaimTx_RLP(t *testing.T) {
fields: fields{
AccountID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
Name: "fdsa.test",
NameSalt: 12345,
NameSalt: *utils.RequireBigIntFromString("9795159241593061970"),
Fee: *utils.NewBigIntFromUint64(10),
TTL: uint64(10),
Nonce: uint64(1),
},
wantTx: "tx_9CABoQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wGJZmRzYS50ZXN0gjA5CgplC94E",
wantTx: "tx_+DogAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMBiWZkc2EudGVzdIiH72Vu6YoCUgoKx4dL6Q==",
wantErr: false,
},
}
Expand Down
17 changes: 16 additions & 1 deletion api/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def traverse(data):
v["$ref"] = "#/definitions/Amount"
elif "block_height" == key:
v["format"] = "uint64"

elif "name_salt" == key:
v.pop("type", None)
v.pop("format", None)
v["$ref"] = "#/definitions/NameSalt"

traverse(v)
except TypeError:
pass
Expand Down Expand Up @@ -80,6 +84,17 @@ def add_definitions(data):
}
}

data["definitions"]["NameSalt"] = {
"type": "integer",
"minimum": 0,
"x-go-type": {
"import": {
"package": "github.com/aeternity/aepp-sdk-go/utils"
},
"type": "BigInt"
}
}

return data


Expand Down
13 changes: 11 additions & 2 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3553,8 +3553,7 @@
"type": "string"
},
"name_salt": {
"type": "integer",
"format": "uint64"
"$ref": "#/definitions/NameSalt"
},
"fee": {
"$ref": "#/definitions/Fee"
Expand Down Expand Up @@ -5944,6 +5943,16 @@
},
"type": "BigInt"
}
},
"NameSalt": {
"type": "integer",
"minimum": 0,
"x-go-type": {
"import": {
"package": "github.com/aeternity/aepp-sdk-go/utils"
},
"type": "BigInt"
}
}
},
"externalDocs": {
Expand Down
7 changes: 5 additions & 2 deletions generated/models/name_claim_tx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45ff92d

Please sign in to comment.