Skip to content

Commit

Permalink
Add flags to coinID.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Apr 27, 2021
1 parent fedbdf3 commit fda6e98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
29 changes: 17 additions & 12 deletions server/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package eth

import (
"context"
"encoding/binary"
"errors"
"fmt"
"sync"
Expand All @@ -21,8 +22,10 @@ func init() {
}

const (
assetName = "eth"
coinIDSize = 52
assetName = "eth"
// coinIdSize = flags (2) + smart contract address where funds are locked (20) + secret
// hash map key (32)
coinIDSize = 54
// The blockPollInterval is the delay between calls to bestBlockHash to
// check for new blocks.
blockPollInterval = time.Second
Expand Down Expand Up @@ -351,29 +354,31 @@ out:
wg.Wait()
}

// decodeCoinID decodes the coin ID into a contract address and secret hash.
func decodeCoinID(coinID []byte) (common.Address, []byte, error) {
// decodeCoinID decodes the coin ID into flags, a contract address, and secret hash.
func decodeCoinID(coinID []byte) (uint16, common.Address, []byte, error) {
if len(coinID) != coinIDSize {
return common.Address{}, nil, fmt.Errorf("coin ID wrong length. expected %d, got %d",
return 0, common.Address{}, nil, fmt.Errorf("coin ID wrong length. expected %d, got %d",
coinIDSize, len(coinID))
}
secretHash := make([]byte, 32)
copy(secretHash, coinID[20:])
return common.BytesToAddress(coinID[:20]), secretHash, nil
copy(secretHash, coinID[22:])
return binary.BigEndian.Uint16(coinID[:2]), common.BytesToAddress(coinID[2:22]), secretHash, nil
}

func coinIDToString(coinID []byte) (string, error) {
addr, secretHash, err := decodeCoinID(coinID)
flags, addr, secretHash, err := decodeCoinID(coinID)
if err != nil {
return "", err
}
return fmt.Sprintf("%x:%x", addr, secretHash), err
return fmt.Sprintf("%x:%x:%x", flags, addr, secretHash), nil
}

// toCoinID converts the address and secret hash to a coin ID.
func toCoinID(addr *common.Address, secretHash []byte) []byte {
func toCoinID(flags uint16, addr *common.Address, secretHash []byte) []byte {
b := make([]byte, coinIDSize)
copy(b[:], addr[:])
copy(b[20:], secretHash[:])
b[0] = byte(flags)
b[1] = byte(flags >> 8)
copy(b[2:], addr[:])
copy(b[22:], secretHash[:])
return b
}
15 changes: 13 additions & 2 deletions server/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ func TestLoad(t *testing.T) {
func TestDecodeCoinID(t *testing.T) {
tests := []struct {
name string
wantFlags uint16
wantAddr common.Address
coinID, wantSecretHash []byte
wantErr bool
}{{
name: "ok",
coinID: []byte{
0xFF, 0x01, // 2 byte flags
0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb,
0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48,
0x26, 0x05, // 20 byte addr
Expand All @@ -99,6 +101,7 @@ func TestDecodeCoinID(t *testing.T) {
0x8f, 0xd7, 0x99, 0x49, 0x98, 0xbb, 0x3e, 0x50, 0x48,
0x56, 0x7f, 0x2f, 0x07, 0x3c, // 32 byte secret hash
},
wantFlags: 65281,
wantAddr: common.Address{
0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb,
0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48,
Expand All @@ -113,6 +116,7 @@ func TestDecodeCoinID(t *testing.T) {
}, {
name: "wrong length",
coinID: []byte{
0xFF, 0x01, // 2 byte flags
0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb,
0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48,
0x26, 0x05, // 20 byte addr
Expand All @@ -125,7 +129,7 @@ func TestDecodeCoinID(t *testing.T) {
}}

for _, test := range tests {
addr, secretHash, err := decodeCoinID(test.coinID)
flags, addr, secretHash, err := decodeCoinID(test.coinID)
if test.wantErr {
if err == nil {
t.Fatalf("expected error for test %v", test.name)
Expand All @@ -135,6 +139,10 @@ func TestDecodeCoinID(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error for test %v: %v", test.name, err)
}
if flags != test.wantFlags {
t.Fatalf("want flags value of %v but got %v for test %v",
test.wantFlags, flags, test.name)
}
if addr != test.wantAddr {
t.Fatalf("want addr value of %v but got %v for test %v",
test.wantAddr, addr, test.name)
Expand All @@ -147,6 +155,7 @@ func TestDecodeCoinID(t *testing.T) {
}

func TestCoinIDToString(t *testing.T) {
flags := "ff01"
addr := "18d65fb8d60c1199bb1ad381be47aa692b482605"
secretHash := "71d810d39333296b518c846a3e49eca55f998fd7994998bb3e5048567f2f073c"
tests := []struct {
Expand All @@ -156,6 +165,7 @@ func TestCoinIDToString(t *testing.T) {
}{{
name: "ok",
coinID: []byte{
0xFF, 0x01, // 2 byte flags
0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb,
0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48,
0x26, 0x05, // 20 byte addr
Expand All @@ -164,10 +174,11 @@ func TestCoinIDToString(t *testing.T) {
0x8f, 0xd7, 0x99, 0x49, 0x98, 0xbb, 0x3e, 0x50, 0x48,
0x56, 0x7f, 0x2f, 0x07, 0x3c, // 32 byte secret hash
},
wantCoinID: addr + ":" + secretHash,
wantCoinID: flags + ":" + addr + ":" + secretHash,
}, {
name: "wrong length",
coinID: []byte{
0xFF, 0x01, // 2 byte flags
0x18, 0xd6, 0x5f, 0xb8, 0xd6, 0x0c, 0x11, 0x99, 0xbb,
0x1a, 0xd3, 0x81, 0xbe, 0x47, 0xaa, 0x69, 0x2b, 0x48,
0x26, 0x05, // 20 byte addr
Expand Down

0 comments on commit fda6e98

Please sign in to comment.