Example #1
0
func Encode(keys *bitecdsa.PrivateKey) (string, error) {

	var extended bytes.Buffer
	extended.WriteByte(byte(0x80))
	extended.Write(keys.D.Bytes())
	sha1, sha2 := sha256.New(), sha256.New()
	sha1.Write(extended.Bytes())
	sha2.Write(sha1.Sum(nil))
	checksum := sha2.Sum(nil)[:4]
	extended.Write(checksum)
	encoded, err := base58.Encode(extended.Bytes())
	if err != nil {
		return "", err
	}
	return encoded, nil
}
Example #2
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
}