-
Notifications
You must be signed in to change notification settings - Fork 86
/
account.go
105 lines (88 loc) · 2.49 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package account
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"decred.org/dcrdex/server/account/pki"
"github.com/decred/dcrd/crypto/blake256"
"github.com/decred/dcrd/dcrec/secp256k1/v2"
)
var HashFunc = blake256.Sum256
const (
HashSize = blake256.Size
)
type AccountID [HashSize]byte
// NewID generates a unique account id with the provided public key bytes.
func NewID(pk []byte) AccountID {
// Hash the pubkey hash.
h := HashFunc(pk)
return HashFunc(h[:])
}
// String returns a hexadecimal representation of the AccountID. String
// implements fmt.Stringer.
func (aid AccountID) String() string {
return hex.EncodeToString(aid[:])
}
// MarshalJSON satisfies the json.Marshaller interface, and will marshal the
// id to a hex string.
func (aid AccountID) MarshalJSON() ([]byte, error) {
return json.Marshal(aid.String())
}
// Value implements the sql/driver.Valuer interface.
func (aid AccountID) Value() (driver.Value, error) {
return aid[:], nil // []byte
}
// Scan implements the sql.Scanner interface.
func (aid *AccountID) Scan(src interface{}) error {
switch src := src.(type) {
case []byte:
copy(aid[:], src)
return nil
//case string:
// case nil:
// *oid = nil
// return nil
}
return fmt.Errorf("cannot convert %T to AccountID", src)
}
// Account represents a dex client account.
type Account struct {
ID AccountID
PubKey *secp256k1.PublicKey
}
// NewAccountFromPubKey creates a dex client account from the provided public
// key bytes.
func NewAccountFromPubKey(pk []byte) (*Account, error) {
if len(pk) != pki.PubKeySize {
return nil, fmt.Errorf("invalid pubkey length, "+
"expected %d, got %d", pki.PubKeySize, len(pk))
}
pubKey, err := secp256k1.ParsePubKey(pk)
if err != nil {
return nil, err
}
return &Account{
ID: NewID(pk),
PubKey: pubKey,
}, nil
}
// Rule represents a rule of community conduct.
type Rule uint8
const (
// NoRule indicates that no rules have been broken. This may be an invalid
// value in some contexts.
NoRule = iota
// FailureToAct means that an account has not followed through on one of their
// swap negotiation steps.
FailureToAct
// CancellationRatio means the account's cancellation ratio has dropped below
// the acceptable level.
CancellationRatio
// LowFees means an account made a transaction that didn't pay fees at the
// requisite level.
LowFees
// PreimageReveal means an account failed to respond with a valid preimage
// for their order during epoch processing.
PreimageReveal
)