forked from nknorg/nkn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookkeeper.go
146 lines (133 loc) · 3.66 KB
/
bookkeeper.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
package bookkeeper
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"os"
"strconv"
"github.com/nknorg/nkn/api/httpjson/client"
. "github.com/nknorg/nkn/cli/common"
"github.com/nknorg/nkn/core/contract"
"github.com/nknorg/nkn/core/signature"
"github.com/nknorg/nkn/core/transaction"
"github.com/nknorg/nkn/crypto"
"github.com/nknorg/nkn/vault"
"github.com/urfave/cli"
)
func makeBookkeeperTransaction(pubkey *crypto.PubKey, op bool, cert []byte, issuer *vault.Account) (string, error) {
tx, _ := transaction.NewBookKeeperTransaction(pubkey, op, cert, issuer.PubKey())
attr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxnAttribute, 0)
tx.Attributes = append(tx.Attributes, &attr)
if err := signTransaction(issuer, tx); err != nil {
fmt.Println("Sign regist transaction failed.")
return "", err
}
var buffer bytes.Buffer
if err := tx.Serialize(&buffer); err != nil {
fmt.Println("Serialize bookkeeper transaction failed.")
return "", err
}
return hex.EncodeToString(buffer.Bytes()), nil
}
func signTransaction(signer *vault.Account, tx *transaction.Transaction) error {
signature, err := signature.SignBySigner(tx, signer)
if err != nil {
fmt.Println("SignBySigner failed.")
return err
}
transactionContract, err := contract.CreateSignatureContract(signer.PubKey())
if err != nil {
fmt.Println("CreateSignatureContract failed.")
return err
}
transactionContractContext := contract.NewContractContext(tx)
if err := transactionContractContext.AddContract(transactionContract, signer.PubKey(), signature); err != nil {
fmt.Println("AddContract failed")
return err
}
tx.SetPrograms(transactionContractContext.GetPrograms())
return nil
}
func assetAction(c *cli.Context) error {
if c.NumFlags() == 0 {
cli.ShowSubcommandHelp(c)
return nil
}
var pubkeyHex []byte
var err error
var add bool
addPubkey := c.String("add")
subPubkey := c.String("sub")
if addPubkey == "" && subPubkey == "" {
fmt.Println("missing --add or --sub")
return nil
}
if addPubkey != "" {
pubkeyHex, err = hex.DecodeString(addPubkey)
add = true
}
if subPubkey != "" {
if pubkeyHex != nil {
fmt.Println("using --add or --sub")
return nil
}
pubkeyHex, err = hex.DecodeString(subPubkey)
add = false
}
if err != nil {
fmt.Println("Invalid public key in hex")
return nil
}
pubkey, err := crypto.DecodePoint(pubkeyHex)
if err != nil {
fmt.Println("Invalid public key")
return nil
}
cert := c.String("cert")
wallet, err := vault.OpenWallet(vault.WalletFileName, WalletPassword(c.String("password")))
if err != nil {
fmt.Println("Failed to open wallet.")
os.Exit(1)
}
acc, _ := wallet.GetDefaultAccount()
txHex, err := makeBookkeeperTransaction(pubkey, add, []byte(cert), acc)
if err != nil {
return err
}
resp, err := client.Call(Address(), "sendrawtransaction", 0, map[string]interface{}{"tx": txHex})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
FormatOutput(resp)
return nil
}
func NewCommand() *cli.Command {
return &cli.Command{
Name: "bookkeeper",
Usage: "add or remove bookkeeper",
Description: "With nknc bookkeeper, you could add or remove bookkeeper.",
ArgsUsage: "[args]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "add, a",
Usage: "add a bookkeeper",
},
cli.StringFlag{
Name: "sub, s",
Usage: "sub a bookkeeper",
},
cli.StringFlag{
Name: "cert, c",
Usage: "authorized certificate",
},
},
Action: assetAction,
OnUsageError: func(c *cli.Context, err error, isSubcommand bool) error {
PrintError(c, err, "bookkeeper")
return cli.NewExitError("", 1)
},
}
}