This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organisation.go
174 lines (135 loc) · 4.04 KB
/
organisation.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"log"
"math/big"
"strings"
)
// Organisation struct
type Organisation struct {
ID []byte
Name []byte
GSTIN []byte
Prefix []byte
Role []byte
Signature []byte
PubKey []byte
AdminPubKey []byte
}
// Serialize returns a serialized Organisation
func (org Organisation) Serialize() []byte {
var encoded bytes.Buffer
enc := gob.NewEncoder(&encoded)
err := enc.Encode(org)
if err != nil {
log.Panic(err)
}
return encoded.Bytes()
}
// Hash returns the hash of the Organisation
func (org *Organisation) Hash() []byte {
var hash [32]byte
o := *org
o.ID = []byte{}
hash = sha256.Sum256(o.Serialize())
return hash[:]
}
// Deserialize deserializes Organisation
func DeserializeOrganisation(data []byte) Organisation {
var org Organisation
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&org)
if err != nil {
log.Panic(err)
}
return org
}
// Signs a Organisation
func (org *Organisation) Sign(privKey ecdsa.PrivateKey, pubKeyHash []byte) {
oCopy := org.Copy()
oCopy.Signature = nil
oCopy.AdminPubKey = pubKeyHash
oCopy.ID = oCopy.Hash()
oCopy.AdminPubKey = nil
r, s, err := ecdsa.Sign(rand.Reader, &privKey, oCopy.ID)
if err != nil {
log.Panic(err)
}
signature := append(r.Bytes(), s.Bytes()...)
org.Signature = signature
}
// String returns a human-readable representation of a organisation
func (org Organisation) String() string {
var lines []string
lines = append(lines, fmt.Sprintf("--- Organisation %x:", org.ID))
lines = append(lines, fmt.Sprintf(" Name: %s", org.Name))
lines = append(lines, fmt.Sprintf(" GSTIN: %s", org.GSTIN))
lines = append(lines, fmt.Sprintf(" Prefix: %s", org.Prefix))
lines = append(lines, fmt.Sprintf(" Role: %s", org.Role))
lines = append(lines, fmt.Sprintf(" Signature: %x", org.Signature))
lines = append(lines, fmt.Sprintf(" PubKey: %x", org.PubKey))
lines = append(lines, fmt.Sprintf(" AdminPubKey: %x", org.AdminPubKey))
return strings.Join(lines, "\n")
}
// Copy creates a copy of Organisation to be used in signing
func (org *Organisation) Copy() Organisation {
oCopy := Organisation{org.ID, org.Name, org.GSTIN, org.Prefix, org.Role, nil, org.PubKey, org.AdminPubKey}
return oCopy
}
// Verifies a Organisation
func (org *Organisation) Verify(pubKeyHash []byte) bool {
curve := elliptic.P256()
oCopy := org.Copy()
oCopy.Signature = nil
oCopy.AdminPubKey = pubKeyHash
oCopy.ID = oCopy.Hash()
oCopy.AdminPubKey = nil
r := big.Int{}
s := big.Int{}
sigLen := len(org.Signature)
r.SetBytes(org.Signature[:(sigLen / 2)])
s.SetBytes(org.Signature[(sigLen / 2):])
x := big.Int{}
y := big.Int{}
keyLen := len(org.AdminPubKey)
x.SetBytes(org.AdminPubKey[:(keyLen / 2)])
y.SetBytes(org.AdminPubKey[(keyLen / 2):])
rawPubKey := ecdsa.PublicKey{curve, &x, &y}
if ecdsa.Verify(&rawPubKey, oCopy.ID, &r, &s) == false {
return false
}
return true
}
// NewOrganisation creates a new organisation
func NewOrganisation(address, name, pubKey, gstin, prefix, role string, OrganisationCache *OrganisationCacheSet) (*Organisation, error) {
wallets, err := NewWallets()
if err != nil {
log.Panic(err)
}
wallet := wallets.GetWallet(address)
r := OrganisationCache.Blockchain.GetRole(wallet.PublicKey)
if r == nil {
return &Organisation{}, errors.New("Organisation not found")
}
if bytes.Compare(r, []byte("Admin")) != 0 {
return &Organisation{}, errors.New("Not authorized to perform this action")
}
gst := []byte(strings.ToUpper(gstin))
pre := []byte(prefix)
pub, _ := hex.DecodeString(pubKey)
if OrganisationCache.Blockchain.isOrgDuplicate(gst, pre, pub) {
return &Organisation{}, errors.New("Duplicate data cannot be added")
}
org := &Organisation{nil, []byte(name), gst, pre, []byte(role), nil, pub, wallet.PublicKey}
org.ID = org.Hash()
org.Sign(wallet.PrivateKey, Base58Decode([]byte(address)))
return org, nil
}