Skip to content

Commit

Permalink
refactor: NamePointer is now a struct of its own
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Apr 30, 2019
1 parent 152947c commit 9bd57e9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
43 changes: 37 additions & 6 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package aeternity

import (
"fmt"
"io"

"github.com/aeternity/aepp-sdk-go/generated/models"
"github.com/aeternity/aepp-sdk-go/rlp"
"github.com/aeternity/aepp-sdk-go/utils"
)

Expand Down Expand Up @@ -49,22 +51,22 @@ func ttlTypeIntToStr(i uint64) string {
return oracleTTLTypeStr
}

func buildPointers(pointers []string) (ptrs []*models.NamePointer, err error) {
func buildPointers(pointers []string) (ptrs []*NamePointer, err error) {
// TODO: handle errors
ptrs = make([]*models.NamePointer, len(pointers))
ptrs = make([]*NamePointer, len(pointers))
for i, p := range pointers {
switch GetHashPrefix(p) {
case PrefixAccountPubkey:
// pID, err := buildIDTag(IDTagAccount, p)
key := "account_pubkey"
ptrs[i] = &models.NamePointer{ID: models.EncodedHash(p), Key: &key}
ptrs[i] = NewNamePointer(key, p)
if err != nil {
break
}
case PrefixOraclePubkey:
// pID, err := buildIDTag(IDTagOracle, p)
key := "oracle_pubkey"
ptrs[i] = &models.NamePointer{ID: models.EncodedHash(p), Key: &key}
ptrs[i] = NewNamePointer(key, p)
if err != nil {
break
}
Expand Down Expand Up @@ -258,11 +260,40 @@ func NewNameClaimTx(accountID, name string, nameSalt utils.BigInt, fee utils.Big
return NameClaimTx{accountID, name, nameSalt, fee, ttl, nonce}
}

// NamePointer extends the swagger gener ated models.NamePointer to provide RLP serialization
type NamePointer struct {
ID string
Key string
}

func (t *NamePointer) RLP() (rlpRawMsg []byte, err error) {
return []byte{}, nil
}

func (t *NamePointer) EncodeRLP(w io.Writer) (err error) {
accountID, err := buildIDTag(IDTagAccount, t.ID)
if err != nil {
return
}

err = rlp.Encode(w, []interface{}{t.Key, accountID})
if err != nil {
return
}
return err
}

// NewNamePointer is a constructor for a swagger generated NamePointer struct.
// It returns a pointer because
func NewNamePointer(key string, id string) *NamePointer {
return &NamePointer{ID: id, Key: key}
}

// NameUpdateTx represents a transaction where one extends the lifetime of a reserved name on AENS
type NameUpdateTx struct {
AccountID string
NameID string
Pointers []*models.NamePointer
Pointers []*NamePointer
NameTTL uint64
ClientTTL uint64
Fee utils.BigInt
Expand Down Expand Up @@ -307,7 +338,7 @@ func (t *NameUpdateTx) JSON() (string, error) {
NameID: models.EncodedHash(t.NameID),
NameTTL: &t.NameTTL,
Nonce: t.Nonce,
Pointers: t.Pointers,
Pointers: []*models.NamePointer{},
TTL: t.TTL,
}

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

import (
"bytes"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -507,3 +508,43 @@ func OracleQueryTxRLP(t *testing.T) {
})
}
}

func TestNamePointer_EncodeRLP(t *testing.T) {
type fields struct {
ID string
Key string
}
tests := []struct {
name string
fields fields
wantW string
wantErr bool
}{
{
name: "1 pointer to a normal ak_ account",
fields: fields{
Key: "account_pubkey",
ID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
},
wantW: "I dunno",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &NamePointer{
ID: tt.fields.ID,
Key: tt.fields.Key,
}
w := &bytes.Buffer{}
if err := p.EncodeRLP(w); (err != nil) != tt.wantErr {
t.Errorf("NamePointer.EncodeRLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("NamePointer.EncodeRLP() = %v, want %v", gotW, tt.wantW)
fmt.Println(DecodeRLPMessage(w.Bytes()))
}
})
}
}

0 comments on commit 9bd57e9

Please sign in to comment.