Created
November 2, 2022 06:30
-
-
Save ngocbd/890480feddd6b696761a959c6efc2b30 to your computer and use it in GitHub Desktop.
blockchain transaction in v
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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