Beispiel #1
0
func (t *Transaction) VerifyTransaction(pow []byte) bool {

	headerHash := t.Hash()
	payloadHash := helpers.SHA256(t.Payload)

	return reflect.DeepEqual(payloadHash, t.Header.PayloadHash) && CheckProofOfWork(pow, headerHash) && SignatureVerify(t.Header.From, t.Signature, headerHash)
}
Beispiel #2
0
func (b *Block) GenerateMerkelRoot() []byte {

	var merkell func(hashes [][]byte) []byte
	merkell = func(hashes [][]byte) []byte {

		l := len(hashes)
		if l == 0 {
			return nil
		}
		if l == 1 {
			return hashes[0]
		} else {

			if l%2 == 1 {
				return merkell([][]byte{merkell(hashes[:l-1]), hashes[l-1]})
			}

			bs := make([][]byte, l/2)
			for i, _ := range bs {
				j, k := i*2, (i*2)+1
				bs[i] = helpers.SHA256(append(hashes[j], hashes[k]...))
			}
			return merkell(bs)
		}
	}

	ts := functional.Map(func(t Transaction) []byte { return t.Hash() }, []Transaction(*b.TransactionSlice)).([][]byte)
	return merkell(ts)

}
Beispiel #3
0
func TestMerkellHash(t *testing.T) {

	tr1 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr2 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr3 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
	tr4 := NewTransaction(nil, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))

	b := new(Block)
	b.TransactionSlice = &TransactionSlice{*tr1, *tr2, *tr3, *tr4}

	mt := b.GenerateMerkelRoot()
	manual := helpers.SHA256(append(helpers.SHA256(append(tr1.Hash(), tr2.Hash()...)), helpers.SHA256(append(tr3.Hash(), tr4.Hash()...))...))

	if !reflect.DeepEqual(mt, manual) {
		t.Error("Merkel tree generation fails")
	}
}
Beispiel #4
0
// Returns bytes to be sent to the network
func NewTransaction(from, to, payload []byte) *Transaction {

	t := Transaction{Header: TransactionHeader{From: from, To: to}, Payload: payload}

	t.Header.Timestamp = uint32(time.Now().Unix())
	t.Header.PayloadHash = helpers.SHA256(t.Payload)
	t.Header.PayloadLength = uint32(len(t.Payload))

	return &t
}
Beispiel #5
0
func TestKeySigning(t *testing.T) {

	for i := 0; i < 5; i++ {
		keypair := GenerateNewKeypair()

		data := helpers.ArrayOfBytes(i, 'a')
		hash := helpers.SHA256(data)

		signature, err := keypair.Sign(hash)

		if err != nil {

			t.Error("base58 error")

		} else if !SignatureVerify(keypair.Public, signature, hash) {

			t.Error("Signing and verifying error", len(keypair.Public))
		}
	}

}
Beispiel #6
0
func (t *Transaction) Hash() []byte {

	headerBytes, _ := t.Header.MarshalBinary()
	return helpers.SHA256(headerBytes)
}
Beispiel #7
0
func (b *Block) Hash() []byte {

	headerHash, _ := b.BlockHeader.MarshalBinary()
	return helpers.SHA256(headerHash)
}