Exemple #1
0
func Decode(address string) (pkHash []byte, version byte, err error) {

	binaryAddress, err := base58.BitcoinEncoding.DecodeFromString(address)
	if err != nil {
		return nil, 0, err
	}

	if len(binaryAddress) != 25 {
		return nil, 0, AddressUnderunError(address)
	}

	extended := binaryAddress[0:21]
	version = binaryAddress[0]
	pkHash = binaryAddress[1:21]
	extractedChecksum := binaryAddress[21:]

	s256d := sha256d.New()
	s256d.Write(extended)
	res := s256d.Sum(nil)

	calculatedChecksum := res[0:4]

	if !bytes.Equal(calculatedChecksum, extractedChecksum) {
		return nil, 0, AddressChecksumError(address)
	}

	return pkHash, version, nil
}
Exemple #2
0
func (this *Tx) Hash() (TxHash, error) {

	var txHash TxHash

	h := sha256d.New()
	_, err := this.WriteTo(h)
	if err != nil {
		return txHash, err
	}

	h.Sum(txHash[:0])

	return txHash, nil
}
Exemple #3
0
func (this *Message) Checksum() uint32 {
	h := sha256d.New()
	_, err := h.Write(this.Payload)
	if err != nil {
		panic(err)
	}
	hash := h.Sum(nil)

	var checksum uint32

	checksum = binary.LittleEndian.Uint32(hash)

	return checksum
}
Exemple #4
0
func op_HASH256(c *Context) error {

	top := c.Pop()

	h := sha256d.New()
	_, err := h.Write(top)
	if err != nil {
		return errors.New("Hash Failed")
	}

	res := h.Sum(nil)

	c.Push(res)

	return nil
}
Exemple #5
0
func (this *Block) Hash() (BlockHash, error) {

	var blockHash BlockHash

	h := sha256d.New()
	blockData, err := this.Bytes()
	if err != nil {
		return blockHash, err
	}
	_, err = h.Write(blockData)
	if err != nil {
		return blockHash, err
	}

	h.Sum(blockHash[:0])

	return blockHash, nil
}
Exemple #6
0
func EncodePublicKeyHash(publicKeyHash []byte, version byte) string {
	var err error

	extended := make([]byte, 1, len(publicKeyHash)+5)
	extended[0] = version
	extended = append(extended, publicKeyHash...)

	s256d := sha256d.New()
	s256d.Write(extended)
	res := s256d.Sum(nil)

	checksum := res[0:4]

	binaryAddress := append(extended, checksum...)
	address, err := base58.BitcoinEncoding.EncodeToString(binaryAddress)
	if err != nil {
		panic(err)
	}

	return address

}