Пример #1
0
func TestBase58Check(t *testing.T) {
	for x, test := range checkEncodingStringTests {
		var ver [2]byte
		ver[0] = test.version
		ver[1] = 0
		// test encoding
		if res := base58.CheckEncode([]byte(test.in), ver); res != test.out {
			t.Errorf("CheckEncode test #%d failed: got %s, want: %s", x, res, test.out)
		}

		// test decoding
		res, version, err := base58.CheckDecode(test.out)
		if err != nil {
			t.Errorf("CheckDecode test #%d failed with err: %v", x, err)
		} else if version != ver {
			t.Errorf("CheckDecode test #%d failed: got version: %d want: %d", x, version, ver)
		} else if string(res) != test.in {
			t.Errorf("CheckDecode test #%d failed: got: %s want: %s", x, res, test.in)
		}
	}

	// test the two decoding failure cases
	// case 1: checksum error
	_, _, err := base58.CheckDecode("Axk2WA6M")
	if err != base58.ErrChecksum {
		t.Error("Checkdecode test failed, expected ErrChecksum")
	}
	// case 2: invalid formats (string lengths below 5 mean the version byte and/or the checksum
	// bytes are missing).
	testString := ""
	for len := 0; len < 4; len++ {
		// make a string of length `len`
		_, _, err = base58.CheckDecode(testString)
		if err != base58.ErrInvalidFormat {
			t.Error("Checkdecode test failed, expected ErrInvalidFormat")
		}
	}

}
Пример #2
0
// This example demonstrates how to decode Base58Check encoded data.
func ExampleCheckDecode() {
	// Decode an example Base58Check encoded data.
	encoded := "11Jd3UC9A6K74nhNDieHobyxT1hyRfhHDWQQ81CcUT4EMFdXZTpJemXF5s8FiZ"
	decoded, version, err := base58.CheckDecode(encoded)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Show the decoded data.
	fmt.Printf("Decoded data: %x\n", decoded)
	fmt.Println("Version Byte:", version)

	// Output:
	// Decoded data: 36326539303762313563626632376435343235333939656266366630666235306562623838663138
	// Version Byte: [0 0]
}
Пример #3
0
// DecodeAddress decodes the string encoding of an address and returns
// the Address if addr is a valid encoding for a known address type on
// the network provided.
func DecodeAddress(addr string, defaultNet *chaincfg.Params) (Address, error) {
	// Switch on decoded length to determine the type.
	decoded, netID, err := base58.CheckDecode(addr)
	if err != nil {
		if err == base58.ErrChecksum {
			return nil, ErrChecksumMismatch
		}
		return nil, fmt.Errorf("decoded address is of unknown format: %v",
			err.Error())
	}
	if defaultNet == nil {
		return nil, ErrMissingDefaultNet
	}
	switch netID {
	case defaultNet.PubKeyAddrID:
		// First byte is the signature suite and ybit.
		suite := decoded[0]
		suite &= ^uint8(1 << 7)
		ybit := !(decoded[0]&(1<<7) == 0)

		switch int(suite) {
		case chainec.ECTypeSecp256k1:
			if len(decoded) == 33 {
				toAppend := uint8(0x02)
				if ybit {
					toAppend = 0x03
				}
				return NewAddressSecpPubKey(
					append([]byte{toAppend}, decoded[1:]...),
					defaultNet)
			}
		case chainec.ECTypeEdwards:
			if len(decoded) == 33 {
				return NewAddressEdwardsPubKey(decoded, defaultNet)
			}
		case chainec.ECTypeSecSchnorr:
			if len(decoded) == 33 {
				toAppend := uint8(0x02)
				if ybit {
					toAppend = 0x03
				}
				return NewAddressSecSchnorrPubKey(
					append([]byte{toAppend}, decoded[1:]...),
					defaultNet)
			}
		}
		return nil, ErrUnknownAddressType

	case defaultNet.PubKeyHashAddrID:
		return NewAddressPubKeyHash(decoded, defaultNet, chainec.ECTypeSecp256k1)

	case defaultNet.PKHEdwardsAddrID:
		return NewAddressPubKeyHash(decoded, defaultNet, chainec.ECTypeEdwards)

	case defaultNet.PKHSchnorrAddrID:
		return NewAddressPubKeyHash(decoded, defaultNet, chainec.ECTypeSecSchnorr)

	case defaultNet.ScriptHashAddrID:
		return NewAddressScriptHashFromHash(decoded, defaultNet)

	default:
		return nil, ErrUnknownAddressType
	}
}