예제 #1
0
func New(addressVersion, stream uint64, eighteenByteRipe bool) (*address, error) {

	var err error
	addr := new(address)
	addr.SigningKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
	if err != nil {
		return nil, errors.New("address.New: Error generating ecdsa signing keys")
	}

	var ripe []byte

	for {
		addr.EncryptionKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
		if err != nil {
			return nil, errors.New("address.New: Error generating ecdsa encryption keys")
		}

		var keyMerge bytes.Buffer
		keyMerge.Write(addr.SigningKey.PublicKey.X.Bytes())
		keyMerge.Write(addr.SigningKey.PublicKey.Y.Bytes())
		keyMerge.Write(addr.EncryptionKey.PublicKey.X.Bytes())
		keyMerge.Write(addr.EncryptionKey.PublicKey.Y.Bytes())

		sha := sha512.New()
		sha.Write(keyMerge.Bytes())

		ripemd := ripemd160.New()
		ripemd.Write(sha.Sum(nil))
		ripe = ripemd.Sum(nil)

		if eighteenByteRipe {
			if ripe[0] == 0x00 && ripe[1] == 0x00 {
				ripe = ripe[2:]
				break
			}
		} else {
			if ripe[0] == 0x00 {
				ripe = ripe[1:]
				break
			}
		}
	}

	var bmAddr bytes.Buffer
	bmAddr.Write(varint.Encode(addressVersion))
	bmAddr.Write(varint.Encode(stream))
	bmAddr.Write(ripe)

	sha1, sha2 := sha512.New(), sha512.New()
	sha1.Write(bmAddr.Bytes())
	sha2.Write(sha1.Sum(nil))
	checksum := sha2.Sum(nil)[:4]
	bmAddr.Write(checksum)

	encoded, err := base58.Encode(bmAddr.Bytes())
	if err != nil {
		return nil, err
	}
	addr.Identifier = "BM-" + encoded

	return addr, nil
}
예제 #2
0
func TestWIF(t *testing.T) {

	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	keys1, err := bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
	if err != nil {
		t.Error(err.Error())
	}

	wif, err := Encode(keys1)
	if err != nil {
		t.Error(err.Error())
	}

	keys2, err := Decode(wif)
	if err != nil {
		t.Error(err.Error())
	}

	if bytes.Compare(keys1.D.Bytes(), keys2.D.Bytes()) != 0 {
		t.Error("Private keys are different. Expected %x, got %x\n", keys1.D.Bytes(), keys2.D.Bytes())
	}

	if bytes.Compare(keys1.PublicKey.X.Bytes(), keys2.PublicKey.X.Bytes()) != 0 {
		t.Error("Public point X are different. Expected %x, got %x\n", keys1.PublicKey.X.Bytes(), keys2.PublicKey.X.Bytes())
	}

	if bytes.Compare(keys1.PublicKey.Y.Bytes(), keys2.PublicKey.Y.Bytes()) != 0 {
		t.Error("Public point Y are different. Expected %x, got %x\n", keys1.PublicKey.Y.Bytes(), keys2.PublicKey.Y.Bytes())
	}

	if !keys2.PublicKey.BitCurve.IsOnCurve(keys2.PublicKey.X, keys2.PublicKey.Y) {
		t.Error("Public point is not on curve\n")
	}

	ok, err := ValidateChecksum(wif)
	if err != nil {
		t.Error(err.Error())
	}

	if !ok {
		t.Error("Invalid checksum")
	}

	// WIF: 5HtKNfWZH4QQZPUGRadud7wfyPGEKLhQJfnYPGvpiivgwfrHfpX
	// Priv. hex: 092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801
	// Pub. uncompressed hex: 0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486

	keys3, err := Decode("5HtKNfWZH4QQZPUGRadud7wfyPGEKLhQJfnYPGvpiivgwfrHfpX")
	privHex := hex.EncodeToString(keys3.D.Bytes())
	if privHex != "092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801" {
		t.Error("Private key (keys3) is wrong. Expected 092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801, got %s\n", privHex)
	}

	var pub bytes.Buffer
	pub.WriteByte(byte(0x04))
	pub.Write(keys3.PublicKey.X.Bytes())
	pub.Write(keys3.PublicKey.Y.Bytes())
	pubHex := hex.EncodeToString(pub.Bytes())
	if pubHex != "0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486" {
		t.Error("Public key (keys3) is wrong. Expected 0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486, got ", pubHex)
	}
}