github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/integration_test/aens_test.go (about)

     1  package integrationtest
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/aeternity/aepp-sdk-go/aeternity"
    11  )
    12  
    13  func getNameEntry(t *testing.T, node *aeternity.Client, name string) (responseJSON string) {
    14  	response, err := node.APIGetNameEntryByName(name)
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	r, _ := response.MarshalBinary()
    19  	responseJSON = string(r)
    20  	return responseJSON
    21  }
    22  
    23  func randomName(length int) string {
    24  	rand.Seed(time.Now().UnixNano())
    25  	const letters = "abcdefghijklmnopqrstuvwxyz"
    26  	b := make([]byte, length)
    27  	for i := range b {
    28  		r := rand.Intn(len(letters))
    29  		b[i] = letters[r]
    30  	}
    31  
    32  	ans := fmt.Sprintf("%s.test", string(b))
    33  	return ans
    34  }
    35  
    36  func TestAENSWorkflow(t *testing.T) {
    37  	node := setupNetwork(t)
    38  	alice, bob := setupAccounts(t)
    39  	aensAlice := aeternity.Aens{Client: node, Account: alice}
    40  
    41  	name := randomName(6)
    42  	// Preclaim the name
    43  	preclaimTx, salt, err := aensAlice.NamePreclaimTx(name, aeternity.Config.Client.Fee)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	fmt.Printf("Preclaim %+v with name %s \n", preclaimTx, name)
    48  	hash := signBroadcast(t, &preclaimTx, alice, node)
    49  
    50  	// Wait for a bit
    51  	_ = waitForTransaction(node, hash)
    52  
    53  	// Claim the name
    54  	claimTx, err := aensAlice.NameClaimTx(name, *salt, aeternity.Config.Client.Fee)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	fmt.Printf("Claim %+v\n", claimTx)
    59  	hash = signBroadcast(t, &claimTx, alice, node)
    60  
    61  	// Wait for a bit
    62  	_ = waitForTransaction(node, hash)
    63  
    64  	// Verify that the name exists
    65  	var nameEntry string
    66  	printNameEntry := func() {
    67  		nameEntry = getNameEntry(t, node, name)
    68  		fmt.Println(nameEntry)
    69  	}
    70  	delay(printNameEntry)
    71  
    72  	// Update the name, make it point to something
    73  	updateTx, err := aensAlice.NameUpdateTx(name, alice.Address)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	fmt.Printf("Update %+v\n", updateTx)
    78  	_ = signBroadcast(t, &updateTx, alice, node)
    79  
    80  	// Verify that the name was updated
    81  	delay(printNameEntry)
    82  	if !strings.Contains(nameEntry, alice.Address) {
    83  		t.Fatalf("The AENS entry should now point to %s but doesn't: %s", alice.Address, nameEntry)
    84  	}
    85  
    86  	// Transfer the name to a recipient
    87  	transferTx, err := aensAlice.NameTransferTx(name, bob.Address)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	fmt.Printf("Transfer %+v\n", transferTx)
    92  	hash = signBroadcast(t, &transferTx, alice, node)
    93  
    94  	// Wait for a bit
    95  	_ = waitForTransaction(node, hash)
    96  
    97  	// Receiver updates the name, makes it point to himself
    98  	aensBob := aeternity.Aens{Client: node, Account: bob}
    99  
   100  	updateTx2, err := aensBob.NameUpdateTx(name, bob.Address)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	fmt.Printf("Update Signed By Recipient %+v\n", updateTx2)
   105  	_ = signBroadcast(t, &updateTx2, bob, node)
   106  
   107  	// Revoke the name - shouldn't work because it is signed by the sender, who no longer owns the address
   108  	revokeTx, err := aensAlice.NameRevokeTx(name, alice.Address)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	fmt.Printf("Revoke %+v\n", revokeTx)
   113  	hash = signBroadcast(t, &revokeTx, alice, node)
   114  
   115  	// Wait for a bit
   116  	revokeTxShouldHaveFailed := waitForTransaction(node, hash)
   117  	if revokeTxShouldHaveFailed == nil {
   118  		t.Fatal("After transferring the name to Recipient, the Sender should not have been able to revoke the name")
   119  	} else {
   120  		fmt.Println(revokeTxShouldHaveFailed)
   121  	}
   122  
   123  	// Revoke the name - signed by the recipient
   124  	revokeTx2, err := aensBob.NameRevokeTx(name, bob.Address)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	fmt.Printf("Revoke Signed By Recipient %+v\n", revokeTx2)
   129  	hash = signBroadcast(t, &revokeTx2, bob, node)
   130  	// Wait for a bit
   131  	_ = waitForTransaction(node, hash)
   132  
   133  }