-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.go
59 lines (51 loc) · 1012 Bytes
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/gob"
)
// Hash of the public key
type AccountId SHA256Sum
type Signature []byte
type Account struct {
Id AccountId
PublicKey ed25519.PublicKey
PrivateKey ed25519.PrivateKey
}
func NewAccount() (*Account, error) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
id := sha256.Sum256(pub)
return &Account{
Id: id,
PublicKey: pub,
PrivateKey: priv,
}, nil
}
func (acc *Account) Sign(tx *Tx) Signature {
txHash := tx.Hash()
return ed25519.Sign(acc.PrivateKey, txHash[:])
}
func (acc *Account) Serialize() []byte {
buf := bytes.Buffer{}
encoder := gob.NewEncoder(&buf)
err := encoder.Encode(acc)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func AccountDeserialize(raw []byte) *Account {
var acc Account
buf := bytes.Buffer{}
buf.Write(raw)
decoder := gob.NewDecoder(&buf)
err := decoder.Decode(&acc)
if err != nil {
panic(err)
}
return &acc
}