1// SPDX-License-Identifier: ISC 2// Copyright (c) 2014-2020 Bitmark Inc. 3// Use of this source code is governed by an ISC 4// license that can be found in the LICENSE file. 5 6package bitcoin_test 7 8import ( 9 "encoding/hex" 10 "testing" 11 12 "github.com/bitmark-inc/bitmarkd/currency/bitcoin" 13) 14 15// for testing 16type testAddress struct { 17 address string 18 version bitcoin.Version 19 addrBytes string 20 valid bool 21} 22 23func TestMain(t *testing.T) { 24 25 // from: https://en.bitcoin.it/wiki/List_of_address_prefixes 26 addresses := []testAddress{ 27 { 28 address: "mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn", 29 version: bitcoin.Testnet, 30 addrBytes: "243f1394f44554f4ce3fd68649c19adc483ce924", 31 valid: true, 32 }, 33 { 34 address: "2MzQwSSnBHWHqSAqtTVQ6v47XtaisrJa1Vc", 35 version: bitcoin.TestnetScript, 36 addrBytes: "4e9f39ca4688ff102128ea4ccda34105324305b0", 37 valid: true, 38 }, 39 { 40 address: "17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem", 41 version: bitcoin.Livenet, 42 addrBytes: "47376c6f537d62177a2c41c4ca9b45829ab99083", 43 valid: true, 44 }, 45 { 46 address: "3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQX", 47 version: bitcoin.LivenetScript, 48 addrBytes: "8f55563b9a19f321c211e9b9f38cdf686ea07845", 49 valid: true, 50 }, 51 { 52 address: "3EktnHQD7RiAE6uzMj2ZifT9YgRrkSgzQZ", 53 }, 54 { 55 address: "mipcBbFg9gMiCh81Kj9tqqdgoZub1ZJRfn", 56 }, 57 } 58 59 for i, item := range addresses { 60 actualVersion, actualBytes, err := bitcoin.ValidateAddress(item.address) 61 if item.valid { 62 if nil != err { 63 t.Fatalf("%d: error: %s", i, err) 64 } 65 eb, err := hex.DecodeString(item.addrBytes) 66 if nil != err { 67 t.Fatalf("%d: hex decode error: %s", i, err) 68 } 69 expectedBytes := bitcoin.AddressBytes{} 70 if len(eb) != len(expectedBytes) { 71 t.Fatalf("%d: hex length actual: %d expected: %d", i, len(eb), len(expectedBytes)) 72 } 73 copy(expectedBytes[:], eb) 74 75 if actualVersion != item.version { 76 t.Errorf("%d: version mismatch actual: %d expected: %d", i, actualVersion, item.version) 77 } 78 if actualBytes != expectedBytes { 79 t.Errorf("%d: bytes mismatch actual: %x expected: %x", i, actualBytes, expectedBytes) 80 } 81 82 t.Logf("%d: version: %d bytes: %x", i, actualVersion, actualBytes) 83 } else if nil == err { 84 t.Errorf("%d: unexpected success", i) 85 } 86 } 87} 88