|
5 | 5 | package splitaddrcmd |
6 | 6 |
|
7 | 7 | import ( |
8 | | - "encoding/hex" |
9 | 8 | "fmt" |
10 | 9 | "path" |
11 | 10 | "strconv" |
12 | | - "strings" |
13 | 11 |
|
14 | 12 | root "github.com/BOXFoundation/boxd/commands/box/root" |
| 13 | + "github.com/BOXFoundation/boxd/config" |
15 | 14 | "github.com/BOXFoundation/boxd/core/types" |
16 | | - "github.com/BOXFoundation/boxd/script" |
| 15 | + "github.com/BOXFoundation/boxd/rpc/rpcutil" |
17 | 16 | "github.com/BOXFoundation/boxd/util" |
| 17 | + "github.com/BOXFoundation/boxd/wallet" |
18 | 18 | "github.com/spf13/cobra" |
| 19 | + "github.com/spf13/viper" |
19 | 20 | ) |
20 | 21 |
|
21 | 22 | const ( |
@@ -55,95 +56,74 @@ func init() { |
55 | 56 | } |
56 | 57 |
|
57 | 58 | func createCmdFunc(cmd *cobra.Command, args []string) { |
58 | | - fmt.Println("create called") |
| 59 | + fmt.Println("splitaddr create called") |
59 | 60 | if len(args) < 3 || len(args)%2 == 0 { |
60 | 61 | fmt.Println("Invalid argument number: expect odd number larger than or equal to 3") |
61 | 62 | return |
62 | 63 | } |
63 | | - //addrs, weights, err := parseAddrWeight(args[1:]) |
64 | | - //if err != nil { |
65 | | - // fmt.Println(err) |
66 | | - // return |
67 | | - //} |
68 | | - |
69 | | - //wltMgr, err := wallet.NewWalletManager(walletDir) |
70 | | - //if err != nil { |
71 | | - // fmt.Println(err) |
72 | | - // return |
73 | | - //} |
74 | | - //account, exists := wltMgr.GetAccount(args[0]) |
75 | | - //if !exists { |
76 | | - // fmt.Printf("Account %s not managed\n", args[0]) |
77 | | - // return |
78 | | - //} |
79 | | - //passphrase, err := wallet.ReadPassphraseStdin() |
80 | | - //if err != nil { |
81 | | - // fmt.Println(err) |
82 | | - // return |
83 | | - //} |
84 | | - //if err := account.UnlockWithPassphrase(passphrase); err != nil { |
85 | | - // fmt.Println("Fail to unlock account", err) |
86 | | - // return |
87 | | - //} |
88 | | - //fromAddr, err := types.NewAddress(args[0]) |
89 | | - //if err != nil { |
90 | | - // fmt.Println("Invalid address: ", args[0]) |
91 | | - // return |
92 | | - //} |
93 | | - //target := make(map[types.Address]uint64) |
94 | | - //target[fromAddr /* just a dummy value here */] = opReturnAmount |
95 | | - |
96 | | - //conn := client.NewConnectionWithViper(viper.GetViper()) |
97 | | - //defer conn.Close() |
98 | | - //tx, err := client.CreateSplitAddrTransaction(conn, fromAddr, account.PublicKey(), addrs, weights, account) |
99 | | - //if err != nil { |
100 | | - // fmt.Println(err) |
101 | | - // return |
102 | | - //} |
103 | | - //hash, _ := tx.TxHash() |
104 | | - //fmt.Println("Tx Hash:", hash.String()) |
105 | | - //fmt.Println(util.PrettyPrint(tx)) |
106 | | - |
107 | | - //splitAddr, err := getSplitAddr(tx.Vout[0].ScriptPubKey) |
108 | | - //if err != nil { |
109 | | - // fmt.Println(err) |
110 | | - // return |
111 | | - //} |
112 | | - //fmt.Printf("Split address generated for `%s`: %v\n", args[1:], splitAddr) |
113 | | -} |
114 | | - |
115 | | -func parseAddrWeight(args []string) ([]types.Address, []uint64, error) { |
116 | | - addrs := make([]types.Address, 0) |
117 | | - weights := make([]uint64, 0) |
118 | | - for i := 0; i < len(args)/2; i++ { |
119 | | - addr, err := types.NewAddress(args[i*2]) |
120 | | - if err != nil { |
121 | | - return nil, nil, err |
122 | | - } |
123 | | - addrs = append(addrs, addr) |
124 | | - |
125 | | - weight, err := strconv.Atoi(args[i*2+1]) |
| 64 | + // account |
| 65 | + wltMgr, err := wallet.NewWalletManager(walletDir) |
| 66 | + if err != nil { |
| 67 | + fmt.Println(err) |
| 68 | + return |
| 69 | + } |
| 70 | + account, exists := wltMgr.GetAccount(args[0]) |
| 71 | + if !exists { |
| 72 | + fmt.Printf("Account %s not managed\n", args[0]) |
| 73 | + return |
| 74 | + } |
| 75 | + passphrase, err := wallet.ReadPassphraseStdin() |
| 76 | + if err != nil { |
| 77 | + fmt.Println(err) |
| 78 | + return |
| 79 | + } |
| 80 | + if err := account.UnlockWithPassphrase(passphrase); err != nil { |
| 81 | + fmt.Println("Fail to unlock account", err) |
| 82 | + return |
| 83 | + } |
| 84 | + // addrs and weights |
| 85 | + addrs, weights := make([]string, 0), make([]uint64, 0) |
| 86 | + for i := 1; i < len(args)-1; i += 2 { |
| 87 | + addrs = append(addrs, args[i]) |
| 88 | + a, err := strconv.ParseUint(args[i+1], 10, 64) |
126 | 89 | if err != nil { |
127 | | - return nil, nil, err |
| 90 | + fmt.Printf("Invalid amount %s\n", args[i+1]) |
| 91 | + return |
128 | 92 | } |
129 | | - weights = append(weights, uint64(weight)) |
| 93 | + weights = append(weights, a) |
130 | 94 | } |
131 | | - return addrs, weights, nil |
132 | | -} |
133 | | - |
134 | | -// create a split address from arguments |
135 | | -func getSplitAddr(scriptPubKey []byte) (string, error) { |
136 | | - // e.g., OP_RETURN aaeb7c5e48182fbd309a4e6a7e0de57e56f4cb16 ce86056786e3415530f8cc739fb414a87435b4b6 01 3ba03e454aed097836f2957a120f95ecf76a2771 04 |
137 | | - splitAddrScriptStr := script.NewScriptFromBytes(scriptPubKey).Disasm() |
138 | | - s := strings.Split(splitAddrScriptStr, " ") |
139 | | - // e.g., aaeb7c5e48182fbd309a4e6a7e0de57e56f4cb16 |
140 | | - pubKeyHash, err := hex.DecodeString(s[1]) |
| 95 | + if err := types.ValidateAddr(addrs...); err != nil { |
| 96 | + fmt.Println(err) |
| 97 | + return |
| 98 | + } |
| 99 | + // fee |
| 100 | + fee := uint64(10) |
| 101 | + // conn |
| 102 | + conn, err := rpcutil.GetGRPCConn(getRPCAddr()) |
141 | 103 | if err != nil { |
142 | | - return "", err |
| 104 | + fmt.Println(err) |
| 105 | + return |
143 | 106 | } |
144 | | - addr, err := types.NewAddressPubKeyHash(pubKeyHash) |
| 107 | + defer conn.Close() |
| 108 | + // send tx |
| 109 | + tx, splitAddr, _, err := rpcutil.NewSplitAddrTxWithFee(account, addrs, |
| 110 | + weights, fee, conn) |
145 | 111 | if err != nil { |
146 | | - return "", err |
| 112 | + fmt.Println(err) |
| 113 | + return |
147 | 114 | } |
148 | | - return addr.String(), nil |
| 115 | + hashStr, err := rpcutil.SendTransaction(conn, tx) |
| 116 | + if err != nil { |
| 117 | + fmt.Println(err) |
| 118 | + return |
| 119 | + } |
| 120 | + fmt.Printf("SplitAddr generated: %s\n", splitAddr) |
| 121 | + fmt.Println("Tx Hash: ", hashStr) |
| 122 | + fmt.Println(util.PrettyPrint(tx)) |
| 123 | +} |
| 124 | + |
| 125 | +func getRPCAddr() string { |
| 126 | + var cfg config.Config |
| 127 | + viper.Unmarshal(&cfg) |
| 128 | + return fmt.Sprintf("%s:%d", cfg.RPC.Address, cfg.RPC.Port) |
149 | 129 | } |
0 commit comments