github.com/annchain/OG@v0.0.9/rpc/controller_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package rpc 15 16 import ( 17 "bytes" 18 "encoding/base64" 19 "encoding/json" 20 "fmt" 21 "github.com/annchain/OG/arefactor/og_interface" 22 "github.com/annchain/OG/common" 23 "github.com/annchain/OG/common/crypto" 24 "github.com/annchain/OG/common/hexutil" 25 "github.com/annchain/OG/common/math" 26 "github.com/annchain/OG/deprecated/ogcrypto" 27 "github.com/annchain/OG/og/types" 28 "io" 29 "net/http" 30 "os" 31 "testing" 32 "time" 33 ) 34 35 const ( 36 ROOT = "http://localhost:8000" 37 PATH_NEW_ACCOUNT = "/new_account" 38 PATH_NEW_TX = "/new_transaction" 39 PATH_NONCE = "/query_nonce" 40 ) 41 42 func TestNewAccount(t *testing.T) { 43 pri, pub, addr, err := newAccount("secp256k1") 44 if err != nil { 45 t.Error(err.Error()) 46 } else { 47 fmt.Println("prikey: ", pri) 48 fmt.Println("pubkey: ", pub) 49 fmt.Println("address:", addr) 50 } 51 } 52 53 type account struct { 54 Privkey string `json:"privkey"` 55 Pubkey string `json:"pubkey"` 56 } 57 58 func newAccount(algorithm string) (string, string, string, error) { 59 body := map[string]string{"algorithm": algorithm} 60 jsonData, err := json.Marshal(body) 61 if err != nil { 62 return "", "", "", err 63 } 64 resp, err := http.Post(ROOT+PATH_NEW_ACCOUNT, "application/json", bytes.NewBuffer(jsonData)) 65 if err != nil { 66 return "", "", "", err 67 } 68 defer resp.Body.Close() 69 var buf bytes.Buffer 70 var a account 71 var signer og_interface.ISigner 72 io.Copy(&buf, resp.Body) 73 err = json.Unmarshal(buf.Bytes(), &a) 74 if err != nil { 75 return "", "", "", err 76 } 77 switch algorithm { 78 case "secp256k1": 79 signer = &ogcrypto.SignerSecp256k1{} 80 case "ed25519": 81 signer = &ogcrypto.SignerEd25519{} 82 } 83 pubkey, err := crypto.PublicKeyFromString(a.Pubkey) 84 if err != nil { 85 return "", "", "", err 86 } 87 addr := signer.Address(pubkey) 88 return a.Privkey, a.Pubkey, addr.String(), nil 89 } 90 func TestQueryNonce(t *testing.T) { 91 _, _, addr, err := newAccount("secp256k1") 92 if err != nil { 93 t.Error(err.Error()) 94 return 95 } 96 97 n, err := nonce(addr) 98 if err != nil { 99 t.Error(err.Error()) 100 return 101 } 102 t.Log(n) 103 //nonce("0xcfad46e0bcd2229f6cdd6fdc36365b738127b7a6") 104 } 105 106 type nonceResp struct { 107 Nonce int `json:"nonce"` 108 } 109 110 func nonce(addr string) (int, error) { 111 resp, err := http.Get(ROOT + PATH_NONCE + "?address=" + addr) 112 if err != nil { 113 return 0, err 114 } 115 defer resp.Body.Close() 116 var buf bytes.Buffer 117 var n nonceResp 118 io.Copy(&buf, resp.Body) 119 err = json.Unmarshal(buf.Bytes(), &n) 120 if err != nil { 121 return 0, err 122 } 123 return n.Nonce, nil 124 } 125 126 func TestSendTx(t *testing.T) { 127 err := sendTx("secp256k1") 128 if err != nil { 129 t.Fatal(err) 130 } 131 } 132 133 func sendTx(algorithm string) error { 134 priv1, pub1, addr1, err := newAccount(algorithm) 135 if err != nil { 136 return err 137 } 138 139 _, _, addr2, err := newAccount(algorithm) 140 if err != nil { 141 return err 142 } 143 144 fromPriv, err := crypto.PrivateKeyFromString(priv1) 145 if err != nil { 146 return err 147 } 148 fromPub, err := crypto.PublicKeyFromString(pub1) 149 if err != nil { 150 return err 151 } 152 fromAddr, err := common.StringToAddress(addr1) 153 if err != nil { 154 return err 155 } 156 toAddr, err := common.StringToAddress(addr2) 157 158 var signer og_interface.ISigner 159 switch algorithm { 160 case "secp256k1": 161 signer = &ogcrypto.SignerSecp256k1{} 162 case "ed25519": 163 signer = &ogcrypto.SignerEd25519{} 164 } 165 166 for nonce := 0; nonce < 10; nonce++ { 167 tx := types.Tx{ 168 TxBase: types.TxBase{ 169 AccountNonce: uint64(nonce), 170 }, 171 From: &fromAddr, 172 To: toAddr, 173 Value: math.NewBigInt(0), 174 } 175 176 signature := signer.Sign(fromPriv, tx.SignatureTargets()) 177 178 newTxData := map[string]string{ 179 "nonce": fmt.Sprintf("%d", tx.TxBase.AccountNonce), 180 "from": tx.From.String(), 181 "to": tx.To.String(), 182 "value": fmt.Sprintf("%d", tx.Value.GetInt64()), 183 "signature": hexutil.Encode(signature.SignatureBytes), 184 "pubkey": fromPub.String(), 185 } 186 187 jsonData, err := json.MarshalIndent(newTxData, "", "\t") 188 if err != nil { 189 return err 190 } 191 fmt.Println(string(jsonData)) 192 resp, err := http.Post(ROOT+PATH_NEW_TX, "appliaction/json", bytes.NewBuffer(jsonData)) 193 if err != nil { 194 return err 195 } 196 defer resp.Body.Close() 197 io.Copy(os.Stdout, resp.Body) 198 time.Sleep(time.Millisecond * 100) 199 } 200 return nil 201 202 } 203 204 func TestRpcController_NewArchive(t *testing.T) { 205 data := []byte("hhhh") 206 fmt.Println(data) 207 dataOut := base64.StdEncoding.EncodeToString(data) 208 //buf.Write(data) 209 fmt.Println(dataOut, []byte(dataOut)) 210 encodeData, _ := base64.StdEncoding.DecodeString(dataOut) 211 fmt.Println(encodeData) 212 213 }