Skip to content

Instantly share code, notes, and snippets.

@ngocbd
Created November 2, 2022 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngocbd/890480feddd6b696761a959c6efc2b30 to your computer and use it in GitHub Desktop.
Save ngocbd/890480feddd6b696761a959c6efc2b30 to your computer and use it in GitHub Desktop.
blockchain transaction in v
module core
import crypto.sha512
import crypto.ed25519
import encoding.hex
pub struct Transaction {
mut: hash string
sender string
recipient string
amount int
timestamp int
nonce u64
signature string
}
pub fn (t Transaction) hash() string{
// nonce + from + amount + to + pre
return sha512.hexhash(t.nonce.str() + t.sender + t.amount.str() + t.recipient)
}
pub fn (t Transaction) validate() {
assert t.hash() == t.hash
}
pub fn (mut t Transaction) sign( private_key string) !string {
sig := ed25519.sign(hex.decode(private_key)!, hex.decode(t.hash())!)!
t.signature=sig.hex()
return t.signature
}
pub fn (t Transaction) verify() !bool {
return ed25519.verify(hex.decode(t.sender)!, hex.decode(t.hash())!, hex.decode(t.signature)!)!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment