github.com/ethersphere/bee/v2@v2.2.0/pkg/bzz/address_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bzz_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethersphere/bee/v2/pkg/bzz"
    12  	"github.com/ethersphere/bee/v2/pkg/crypto"
    13  
    14  	ma "github.com/multiformats/go-multiaddr"
    15  )
    16  
    17  func TestBzzAddress(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	node1ma, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1634/p2p/16Uiu2HAkx8ULY8cTXhdVAcMmLcH9AsTKz6uBQ7DPLKRjMLgBVYkA")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	nonce := common.HexToHash("0x2").Bytes()
    26  
    27  	privateKey1, err := crypto.GenerateSecp256k1Key()
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	overlay, err := crypto.NewOverlayAddress(privateKey1.PublicKey, 3, nonce)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	signer1 := crypto.NewDefaultSigner(privateKey1)
    37  
    38  	bzzAddress, err := bzz.NewAddress(signer1, node1ma, overlay, 3, nonce)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	bzzAddress2, err := bzz.ParseAddress(node1ma.Bytes(), overlay.Bytes(), bzzAddress.Signature, nonce, true, 3)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	if !bzzAddress.Equal(bzzAddress2) {
    49  		t.Fatalf("got %s expected %s", bzzAddress2, bzzAddress)
    50  	}
    51  
    52  	bytes, err := bzzAddress.MarshalJSON()
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	var newbzz bzz.Address
    58  	if err := newbzz.UnmarshalJSON(bytes); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if !newbzz.Equal(bzzAddress) {
    63  		t.Fatalf("got %s expected %s", newbzz, bzzAddress)
    64  	}
    65  }