github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/address/address_test.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package address
    18  
    19  import "fmt"
    20  import "bytes"
    21  import "testing"
    22  import "encoding/hex"
    23  
    24  import "github.com/deroproject/derosuite/config"
    25  
    26  func TestAddressError(t *testing.T) {
    27  	_, err := NewAddress("")
    28  	want := fmt.Errorf("Address is not complete")
    29  	if err.Error() != want.Error() {
    30  		t.Fatalf("want: %s, got: %s", want, err)
    31  	}
    32  
    33  	_, err = NewAddress("dERoNzsi5WW1ABhQ1UGLwoLqBU6sbzvyuS4cCi4PGzW7QRM5TH4MUf3QvZUBNJCYSDPw6K495eroGe24cf75uDdD2QwWy9pchN")
    34  	want = fmt.Errorf("Checksum failed")
    35  	if err.Error() != want.Error() {
    36  		t.Fatalf("want: %s, got: %s", want, err)
    37  	}
    38  
    39  }
    40  
    41  func TestAddress(t *testing.T) {
    42  
    43  	const Monero_MainNetwork = 18
    44  	const Monero_TestNetwork = 53
    45  
    46  	tests := []struct {
    47  		name           string
    48  		Network        uint64
    49  		SpendingKeyHex string
    50  		ViewingKeyHex  string
    51  		Address        string
    52  	}{
    53  		{
    54  			name:           "generic",
    55  			Network:        Monero_MainNetwork,
    56  			SpendingKeyHex: "8c1a9d5ff5aaf1c3cdeb2a1be62f07a34ae6b15fe47a254c8bc240f348271679",
    57  			ViewingKeyHex:  "0a29b163e392eb9416a52907fd7d3b84530f8d02ff70b1f63e72fdcb54cf7fe1",
    58  			Address:        "46w3n5EGhBeZkYmKvQRsd8UK9GhvcbYWQDobJape3NLMMFEjFZnJ3CnRmeKspubQGiP8iMTwFEX2QiBsjUkjKT4SSPd3fKp",
    59  		},
    60  		{
    61  			name:           "generic 2",
    62  			Network:        Monero_MainNetwork,
    63  			SpendingKeyHex: "5007b84275af9a173c2080683afce90b2157ab640c18ddd5ce3e060a18a9ce99",
    64  			ViewingKeyHex:  "27024b45150037b677418fcf11ba9675494ffdf994f329b9f7a8f8402b7934a0",
    65  			Address:        "44f1Y84r9Lu4tQdLWRxV122rygfhUeVBrcmBaqcYCwUHScmf1ht8DFLXX9YN4T7nPPLcpqYLUdrFiY77nQYeH9RuK9gg4p6",
    66  		},
    67  		{
    68  			name:           "require 1 padding in middle",
    69  			Network:        Monero_MainNetwork,
    70  			SpendingKeyHex: "6add197bd82866e8bfbf1dc2fdf49873ec5f679059652da549cd806f2b166756",
    71  			ViewingKeyHex:  "f5cf2897088fda0f7ac1c42491ed7d558a46ee41d0c81d038fd53ff4360afda0",
    72  			Address:        "45fzHekTd5FfvxWBPYX2TqLPbtWjaofxYUeWCi6BRQXYFYd85sY2qw73bAuKhqY7deFJr6pN3STY81bZ9x2Zf4nGKASksqe",
    73  		},
    74  		{
    75  			name:           "require 1 padding in last chunk",
    76  			Network:        Monero_MainNetwork,
    77  			SpendingKeyHex: "50defe92d88b19aaf6bf66f061dd4380b79866a4122b25a03bceb571767dbe7b",
    78  			ViewingKeyHex:  "f8f6f28283921bf5a17f0bcf4306233fc25ce9b6276154ad0de22aebc5c67702",
    79  			Address:        "44grjkXtDHJVbZgtU1UKnrNXidcHfZ3HWToU5WjR3KgHMjgwrYLjXC6i5vm3HCp4vnBfYaNEyNiuZVwqtHD2SenS1JBRyco",
    80  		},
    81  		{
    82  			name:           "testnet",
    83  			Network:        Monero_TestNetwork,
    84  			SpendingKeyHex: "8de9cce254e60cd940abf6c77ef344c3a21fad74320e45734fbfcd5870e5c875",
    85  			ViewingKeyHex:  "27024b45150037b677418fcf11ba9675494ffdf994f329b9f7a8f8402b7934a0",
    86  			Address:        "9xYZvCDf6aFdLd7Qawg5XHZitWLKoeFvcLHfe5GxsGCFLbXSWeQNKciXX9YN4T7nPPLcpqYLUdrFiY77nQYeH9RuK9bogZJ",
    87  		},
    88  
    89  		{
    90  			name:           "DERO testnet",
    91  			Network:        config.Testnet.Public_Address_Prefix,
    92  			SpendingKeyHex: "ffb4baf32792d38d36c5f1792201d1cff142a10bad6aa088090156a35858739d",
    93  			ViewingKeyHex:  "0ea428a9608fc9dc06acceea608ac97cc9119647b943941a381306548ee43455",
    94  			Address:        "dETosYceeTxRZQBk5hQzN51JepzZn5H24JqR96q7mY7ZFo6JhJKPNSKR3vs9ES1ibyQDQgeRheDP6CJbb7AKJY2H9eacz2RtPy",
    95  		},
    96  		{
    97  			name:           "DERO mainnet requires padding in second block",
    98  			Network:        config.Mainnet.Public_Address_Prefix,
    99  			SpendingKeyHex: "10a80329a700f25c9892a696de768f5bdc73cafe6095d647e5707c04f48c0481",
   100  			ViewingKeyHex:  "b0fa8ca43a8f07681274ddd8fa891aea4222aa8027dd516bc144317a042547c4",
   101  			Address:        "dERoNzsi5WW1ABhQ1UGLwoLqBU6sbzvyuS4cCi4PGzW7QRM5TH4MUf3QvZUBNJCYSDPw6K495eroGe24cf75uDdD2QwWy9pchM",
   102  		},
   103  	}
   104  	var base58 string
   105  	var spendingKey, viewingKey []byte
   106  	for _, test := range tests {
   107  		spendingKey, _ = hex.DecodeString(test.SpendingKeyHex)
   108  		viewingKey, _ = hex.DecodeString(test.ViewingKeyHex)
   109  
   110  		address, err := NewAddress(test.Address)
   111  		if err != nil {
   112  			t.Fatalf("%s: Failed while parsing address %s", test.name, err)
   113  			continue
   114  		}
   115  
   116  		if address.Network != test.Network {
   117  			t.Fatalf("%s: want: %d, got: %d", test.name, test.Network, address.Network)
   118  			continue
   119  		}
   120  
   121  		if bytes.Compare(address.SpendKey[:], spendingKey) != 0 {
   122  			t.Fatalf("%s: want: %x, got: %s", test.name, spendingKey, address.SpendKey)
   123  			continue
   124  		}
   125  		if bytes.Compare(address.ViewKey[:], viewingKey) != 0 {
   126  			t.Fatalf("%s: want: %x, got: %s", test.name, viewingKey, address.ViewKey)
   127  			continue
   128  		}
   129  
   130  		base58 = address.Base58()
   131  		if base58 != test.Address {
   132  			t.Fatalf("%s: want: %s, got: %s", test.name, test.Address, base58)
   133  			continue
   134  		}
   135  
   136  	}
   137  }
   138  
   139  // more explaination here https://monero.stackexchange.com/questions/1910/how-do-payment-ids-work
   140  // test case created from here https://xmr.llcoins.net/addresstests.html
   141  func TestIntegratedAddress(t *testing.T) {
   142  
   143  	const Monero_MainNetwork = 18
   144  	const Monero_MainNetwork_Integrated = 19
   145  	const Monero_TestNetwork = 53
   146  
   147  	tests := []struct {
   148  		name           string
   149  		Network        uint64
   150  		NetworkI       uint64
   151  		SpendingKeyHex string
   152  		ViewingKeyHex  string
   153  		PaymentID      string
   154  		Address        string
   155  		AddressI       string
   156  	}{
   157  		{
   158  			name:           "generic",
   159  			Network:        Monero_MainNetwork,
   160  			NetworkI:       Monero_MainNetwork_Integrated,
   161  			SpendingKeyHex: "80d3eca27896f549abc41dd941d08a4c82cff165a7f8bc4c3c0841cffd11c095",
   162  			ViewingKeyHex:  "7849297236cd7c0d6c69a3c8c179c038d3c1c434735741bb3c8995c3c9d6f2ac",
   163  			PaymentID:      "90470a40196034b5",
   164  			Address:        "46WGHoGHRT2DKhdr4BxzhXDoFe5NBjNm1Dka5144aXZHS13cAoUQWRq3FE2gcT3LJjAWJ6fGWq8t8YKRqwwit8vmLT6tcxK",
   165  
   166  			AddressI: "4GCwJc5n2iYDKhdr4BxzhXDoFe5NBjNm1Dka5144aXZHS13cAoUQWRq3FE2gcT3LJjAWJ6fGWq8t8YKRqwwit8vmVs5oxyLeWQsMWmcgkC",
   167  		},
   168  
   169  		{
   170  			name:           "generic",
   171  			Network:        config.Mainnet.Public_Address_Prefix,
   172  			NetworkI:       config.Mainnet.Public_Address_Prefix_Integrated,
   173  			SpendingKeyHex: "bd7393b76af23611e6e0eb1e4974bcb5688fceea6ad8a1b08435a4e68fcb7b8c",
   174  			ViewingKeyHex:  "c828aa405d78c3a0b0a7263d2cb82811d4c6ee3374ada5cc753d8196a271b3d2",
   175  			PaymentID:      "0cbd6e050cf3b73c",
   176  			Address:        "dERoiVavtPjhWkdEPp17RJLXVoHkr2ucMdEbgGgpskhLb33732LBifWMCZhPga3EcjXoYqfM9jRv3W3bnWUSpdmK5Jur1PhN6P",
   177  
   178  			AddressI: "dERijfr9y7XhWkdEPp17RJLXVoHkr2ucMdEbgGgpskhLb33732LBifWMCZhPga3EcjXoYqfM9jRv3W3bnWUSpdmKL24FBjG6ctTAEg1jrhDHh",
   179  		},
   180  	}
   181  
   182  	var base58 string
   183  	var spendingKey, viewingKey []byte
   184  	for _, test := range tests {
   185  		spendingKey, _ = hex.DecodeString(test.SpendingKeyHex)
   186  		viewingKey, _ = hex.DecodeString(test.ViewingKeyHex)
   187  
   188  		address, err := NewAddress(test.Address)
   189  		if err != nil {
   190  			t.Fatalf("%s: Failed while parsing address %s", test.name, err)
   191  			continue
   192  		}
   193  
   194  		if address.Network != test.Network {
   195  			t.Errorf("%s: want: %d, got: %d", test.name, test.Network, address.Network)
   196  			continue
   197  		}
   198  
   199  		if bytes.Compare(address.SpendKey[:], spendingKey) != 0 {
   200  			t.Fatalf("%s: want: %x, got: %s", test.name, spendingKey, address.SpendKey)
   201  			continue
   202  		}
   203  		if bytes.Compare(address.ViewKey[:], viewingKey) != 0 {
   204  			t.Fatalf("%s: want: %x, got: %s", test.name, viewingKey, address.ViewKey)
   205  			continue
   206  		}
   207  
   208  		base58 = address.Base58()
   209  		if base58 != test.Address {
   210  			t.Fatalf("%s: want: %s, got: %s", test.name, test.Address, base58)
   211  			continue
   212  		}
   213  
   214  		address, err = NewAddress(test.AddressI)
   215  		if err != nil {
   216  			t.Fatalf("%s: Failed while parsing address %s", test.name, err)
   217  			continue
   218  		}
   219  
   220  		base58 = address.Base58()
   221  		if base58 != test.AddressI {
   222  			t.Fatalf("%s: want: %s, got: %s", test.name, test.AddressI, base58)
   223  			continue
   224  		}
   225  
   226  		if fmt.Sprintf("%x", address.PaymentID) != test.PaymentID {
   227  			t.Fatalf("%s: PaymentID want: %s, got: %s", test.name, test.PaymentID, address.PaymentID)
   228  		}
   229  
   230  	}
   231  
   232  }
   233  
   234  func Test_Bruteforce_IntegratedAddress(t *testing.T) {
   235      var AddressI string = "dERijfr9y7XhWkdEPp17RJLXVoHkr2ucMdEbgGgpskhLb33732LBifWMCZhPga3EcjXoYqfM9jRv3W3bnWUSpdmKL24FBjG6ctTAEg1jrhDHh"
   236      
   237      var PaymentID string = "0cbd6e050cf3b73c"
   238      
   239      
   240      for i := 0; i < 100000;i++  {
   241      address, err := NewAddress(AddressI)
   242  		if err != nil {
   243  			t.Fatalf("%s: Failed while parsing address %s", AddressI, err)
   244  			continue
   245  		}
   246  		if fmt.Sprintf("%x",address.PaymentID) != PaymentID{
   247                      t.Fatalf("Payment ID failed at loop %d", i)
   248                  }
   249      }
   250  }