github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/rpc/verifySignature.test.js (about) 1 'use strict'; 2 var expect = require('chai').expect; 3 var rpc_client = require('./rpc_client/rpc_client.js'); 4 var Wallet = require("nebulas"); 5 var Account = Wallet.Account; 6 var protocol_version = '/neb/1.0.0' 7 var node_version = '0.7.0'; 8 9 10 var server_address = 'localhost:8684'; 11 var coinbase = "eb31ad2d8a89a0ca6935c308d5425730430bc2d63f2573b8"; 12 var sourceAccount; 13 var chain_id = 100; 14 var env = 'local'; 15 16 if (env === 'testneb1') { 17 chain_id = 1001; 18 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 19 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 20 server_address = "35.182.48.19:8684"; 21 22 } else if (env === "testneb2") { 23 chain_id = 1002; 24 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 25 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 26 server_address = "34.205.26.12:8684"; 27 28 } else if (env === "testneb3") { 29 chain_id = 1003; 30 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 31 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 32 server_address = "35.177.214.138:8684"; 33 34 } else if (env === "testneb4") { //super node 35 chain_id = 1004; 36 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 37 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 38 server_address = "35.154.108.11:8684"; 39 } else if (env === "testneb4_normalnode"){ 40 chain_id = 1004; 41 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 42 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 43 server_address = "18.197.107.228:8684"; 44 } else if (env === "local") { 45 chain_id = 100; 46 sourceAccount = new Wallet.Account("d80f115bdbba5ef215707a8d7053c16f4e65588fd50b0f83369ad142b99891b5"); 47 coinbase = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5"; 48 server_address = "127.0.0.1:8684"; 49 50 } else if (env === "maintest"){ 51 chain_id = 2; 52 sourceAccount = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c"); 53 coinbase = "n1dZZnqKGEkb1LHYsZRei1CH6DunTio1j1q"; 54 server_address = "54.149.15.132:8684"; 55 } else { 56 throw new Error("invalid env (" + env + ")."); 57 } 58 59 var client; 60 var normalOutput; 61 62 function testRpc(testInput, testExpect, done) { 63 client.verifySignature(testInput.rpcInput, function (err, response) { 64 if (err != null) { 65 try { 66 expect(testExpect.errMsg).to.be.equal(err.details); 67 } catch (err) { 68 console.log(err); 69 done(err) 70 return; 71 } 72 console.log(err); 73 done(); 74 return; 75 } else { 76 try { 77 if (testInput.isNormal) { 78 normalOutput = response; 79 console.log("response for normal input is: " + JSON.stringify(response)) 80 expect(JSON.stringify(response)).to.be.equal(JSON.stringify(testExpect.rpcOutput)); 81 } else { 82 if (testExpect.isNormalOutput) { //非正常输入,正常输出? 83 expect(JSON.stringify(response)).to.be.equal(JSON.stringify(normalOutput)); 84 } else { 85 expect(testExpect.isNormalOutput).to.be.equal(false); 86 expect(JSON.stringify(response)).not.be.equal(JSON.stringify(normalOutput)); 87 expect(response.result).to.be.a("boolean"); 88 //In JS, uint64 is converted to string 89 expect(response.address).to.be.equal('n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc'); 90 } 91 } 92 } catch (err) { 93 done(err); 94 return; 95 }; 96 } 97 done(); 98 return; 99 }); 100 101 } 102 103 describe('rpc: verifySignature', function () { 104 before(function () { 105 client = rpc_client.new_client(server_address); 106 }); 107 108 it('normal rpc', function (done) { 109 var testInput = { 110 rpcInput: { 111 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d49245564', 112 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 113 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc', 114 alg: 1 115 }, 116 isNormal: true 117 } 118 119 var testExpect = { 120 isNormalOutput: true, 121 rpcOutput: { 122 result: true, 123 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc' 124 }, 125 } 126 127 testRpc(testInput, testExpect, done); 128 }) 129 130 it('empty alg', function (done) { 131 var testInput = { 132 rpcInput: { 133 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d49245564', 134 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 135 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc', 136 }, 137 isNormal: true 138 } 139 140 var testExpect = { 141 isNormalOutput: true, 142 rpcOutput: { 143 result: true, 144 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc' 145 }, 146 } 147 148 testRpc(testInput, testExpect, done); 149 }) 150 151 it('error rpc', function (done) { 152 var testInput = { 153 rpcInput: { 154 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d49245564', 155 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 156 address: 'none_exist_address', 157 alg: 1 158 }, 159 isNormal: true 160 } 161 162 var testExpect = { 163 isNormalOutput: true, 164 rpcOutput: { 165 result: false, 166 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc' 167 }, 168 } 169 170 testRpc(testInput, testExpect, done); 171 }) 172 173 it('empty address', function (done) { 174 var testInput = { 175 rpcInput: { 176 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d49245564', 177 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 178 alg: 1 179 }, 180 isNormal: true 181 } 182 183 var testExpect = { 184 isNormalOutput: false, 185 rpcOutput: { 186 result: false, 187 address: 'n1HUbJZ45Ra5jrRqWvfVaRMiBMB3CACGhqc' 188 }, 189 } 190 191 testRpc(testInput, testExpect, done); 192 }); 193 194 it('invalid message: odd length', function (done) { 195 var testInput = { 196 rpcInput: { 197 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d4924556', // 198 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 199 alg: 1 200 }, 201 isNormal: false 202 } 203 204 var testExpect = { 205 isNormalOutput: false, 206 errMsg: "encoding/hex: odd length hex string" 207 } 208 209 testRpc(testInput, testExpect, done); 210 }); 211 212 it('invalid message: not a 32Byte', function (done) { 213 var testInput = { 214 rpcInput: { 215 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d492455', 216 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 217 alg: 1 218 }, 219 isNormal: false 220 } 221 222 var testExpect = { 223 isNormalOutput: false, 224 errMsg: "invalid message length, need 32 bytes" 225 } 226 227 testRpc(testInput, testExpect, done); 228 }); 229 230 it('invalid message: not a 32Byte', function (done) { 231 var testInput = { 232 rpcInput: { 233 msg: '9dedc6db0d895e346355f2c702a7a8e462993fee16a1ec8847b2852d492455', 234 signature: 'ee291ab49ba4ad1c5874a3842bcf02ce3e948ea0938289835eea353394297a166d8b3a93c4d10ffb115a30466c4499fd38e2288586efb36f9fb83399350d3ce600', 235 alg: 2 236 }, 237 isNormal: false 238 } 239 240 var testExpect = { 241 isNormalOutput: false, 242 errMsg: "invalid Algorithm" 243 } 244 245 testRpc(testInput, testExpect, done); 246 }); 247 248 249 });