github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/rpc/admin.SignTransactionWithPassphrase.test.js (about) 1 'use strict'; 2 3 var expect = require('chai').expect; 4 var rpc_client = require('./rpc_client/rpc_client.js'); 5 var Wallet = require('nebulas'); 6 7 var coinbase, 8 sourceAccount, 9 chain_id, 10 server_address; 11 12 var env = process.env.NET || 'local'; 13 var env = 'local'; 14 if (env === 'testneb1') { 15 chain_id = 1001; 16 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 17 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 18 server_address = "35.182.48.19:8684"; 19 20 } else if (env === "testneb2") { 21 chain_id = 1002; 22 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 23 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 24 server_address = "34.205.26.12:8684"; 25 26 } else if (env === "testneb3") { 27 chain_id = 1003; 28 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 29 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 30 server_address = "35.177.214.138:8684"; 31 32 } else if (env === "testneb4") { //super node 33 chain_id = 1004; 34 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 35 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 36 server_address = "35.154.108.11:8684"; 37 } else if (env === "testneb4_normalnode"){ 38 chain_id = 1004; 39 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 40 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 41 server_address = "18.197.107.228:8684"; 42 } else if (env === "local") { 43 chain_id = 100; 44 sourceAccount = new Wallet.Account("d80f115bdbba5ef215707a8d7053c16f4e65588fd50b0f83369ad142b99891b5"); 45 coinbase = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5"; 46 server_address = "127.0.0.1:8684"; 47 48 } else if (env === "maintest"){ 49 chain_id = 2; 50 sourceAccount = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c"); 51 coinbase = "n1dZZnqKGEkb1LHYsZRei1CH6DunTio1j1q"; 52 server_address = "54.149.15.132:8684"; 53 } else { 54 throw new Error("invalid env (" + env + ")."); 55 } 56 57 var client, 58 address; 59 60 function testSignTransaction(testInput, testExpect, done) { 61 client.SignTransactionWithPassphrase(testInput, (err, resp) => { 62 try { 63 expect(!!err).to.equal(testExpect.hasError); 64 65 if (err) { 66 console.log(JSON.stringify(err)); 67 expect(err).have.property('details').equal(testExpect.errorMsg); 68 } else { 69 console.log(JSON.stringify(resp)); 70 expect(resp).to.have.property('data'); 71 } 72 done(); 73 } catch (err) { 74 done(err); 75 } 76 }); 77 } 78 79 describe("rpc: SignTransaction with passphrase", () => { 80 before((done) => { 81 client = rpc_client.new_client(server_address, 'AdminService'); 82 83 try { 84 client.NewAccount({passphrase: "passphrase"}, (err, resp) => { 85 expect(!!err).to.be.false; 86 expect(resp).to.have.property('address'); 87 address = resp.address; 88 console.log("create new account: " + address); 89 done(); 90 }); 91 } catch(err) { 92 done(err) 93 } 94 }); 95 96 it('1. normal', done => { 97 var testInput = { 98 transaction: { 99 from: address, 100 to: coinbase, 101 value: "123", 102 nonce: "10000", 103 gas_price: "1000000", 104 gas_limit: "1000000", 105 }, 106 passphrase: 'passphrase' 107 } 108 109 var testExpect = { 110 hasError: false, 111 errorMsg: "", 112 113 } 114 115 testSignTransaction(testInput, testExpect, done) 116 }); 117 118 it('2. `to` illegal', done => { 119 var testInput = { 120 transaction: { 121 from: address, 122 to: "faaaa", 123 value: "", 124 nonce: "10000", 125 gas_price: "1000000", 126 gas_limit: "1000000", 127 contract: {} 128 }, 129 passphrase: 'passphrase' 130 } 131 132 var testExpect = { 133 hasError: true, 134 errorMsg: "address: invalid address format", 135 136 } 137 138 testSignTransaction(testInput, testExpect, done) 139 }); 140 141 it('3. `to` empty', done => { 142 var testInput = { 143 transaction: { 144 from: address, 145 to: "", 146 value: "", 147 nonce: "10000", 148 gas_price: "1000000", 149 gas_limit: "1000000", 150 contract: {} 151 }, 152 passphrase: 'passphrase' 153 } 154 155 var testExpect = { 156 hasError: true, 157 errorMsg: "address: invalid address format", 158 159 } 160 161 testSignTransaction(testInput, testExpect, done) 162 }); 163 164 it('4. `value` empty', done => { 165 var testInput = { 166 transaction: { 167 from: address, 168 to: coinbase, 169 value: "", 170 nonce: "10000", 171 gas_price: "1000000", 172 gas_limit: "1000000", 173 contract: {} 174 }, 175 passphrase: 'passphrase' 176 } 177 178 var testExpect = { 179 hasError: true, 180 errorMsg: "invalid value", 181 182 } 183 184 testSignTransaction(testInput, testExpect, done) 185 }); 186 187 it('5. `value` alpha', done => { 188 var testInput = { 189 transaction: { 190 from: address, 191 to: coinbase, 192 value: "abc", 193 nonce: "10000", 194 gas_price: "1000000", 195 gas_limit: "1000000", 196 contract: {} 197 }, 198 passphrase: 'passphrase' 199 } 200 201 var testExpect = { 202 hasError: true, 203 errorMsg: "invalid value", 204 205 } 206 207 testSignTransaction(testInput, testExpect, done) 208 }); 209 210 it('6. `value` neg number', done => { 211 var testInput = { 212 transaction: { 213 from: address, 214 to: coinbase, 215 value: "-123", 216 nonce: "10000", 217 gas_price: "1000000", 218 gas_limit: "1000000", 219 contract: {} 220 }, 221 passphrase: 'passphrase' 222 } 223 224 var testExpect = { 225 hasError: true, 226 errorMsg: "invalid value", 227 228 } 229 230 testSignTransaction(testInput, testExpect, done) 231 }); 232 233 // it('7. `nonce` alpha', done => {//nonce can not be string 234 // var testInput = { 235 // transaction: { 236 // from: address, 237 // to: coinbase, 238 // value: '$', 239 // nonce: '', 240 // gas_price: "1000000", 241 // gas_limit: "1000000", 242 // }, 243 // passphrase: 'passphrase' 244 // } 245 246 // var testExpect = { 247 // hasError: false, 248 249 // } 250 251 // testSignTransaction(testInput, testExpect, done) 252 // }); 253 254 it('8. `nonce` neg number', done => { 255 var testInput = { 256 transaction: { 257 from: address, 258 to: coinbase, 259 value: "123", 260 nonce: -100, 261 gas_price: "1000000", 262 gas_limit: "1000000", 263 }, 264 passphrase: 'passphrase' 265 } 266 267 var testExpect = { 268 hasError: false, 269 270 } 271 272 testSignTransaction(testInput, testExpect, done) 273 }); 274 275 it('9. `gas_price` empty', done => { 276 var testInput = { 277 transaction: { 278 from: address, 279 to: coinbase, 280 value: "123", 281 nonce: "10000", 282 gas_price: "", 283 gas_limit: "1000000", 284 contract: {} 285 }, 286 passphrase: 'passphrase' 287 } 288 289 var testExpect = { 290 hasError: true, 291 errorMsg: "invalid gasPrice", 292 293 } 294 295 testSignTransaction(testInput, testExpect, done) 296 }); 297 298 it('10. `gas_price` alpha', done => { 299 var testInput = { 300 transaction: { 301 from: address, 302 to: coinbase, 303 value: "123", 304 nonce: "10000", 305 gas_price: "abcxz", 306 gas_limit: "1000000", 307 contract: {} 308 }, 309 passphrase: 'passphrase' 310 } 311 312 var testExpect = { 313 hasError: true, 314 errorMsg: "invalid gasPrice", 315 316 } 317 318 testSignTransaction(testInput, testExpect, done) 319 }); 320 321 it('11. `gas_price` negative', done => { 322 var testInput = { 323 transaction: { 324 from: address, 325 to: coinbase, 326 value: "123", 327 nonce: "10000", 328 gas_price: "-10000", 329 gas_limit: "1000000", 330 contract: {} 331 }, 332 passphrase: 'passphrase' 333 } 334 335 var testExpect = { 336 hasError: true, 337 errorMsg: "invalid gasPrice", 338 339 } 340 341 testSignTransaction(testInput, testExpect, done) 342 }); 343 344 it('12. `gas_limit` empty', done => { 345 var testInput = { 346 transaction: { 347 from: address, 348 to: coinbase, 349 value: "123", 350 nonce: "10000", 351 gas_price: "1000000", 352 gas_limit: "", 353 contract: {} 354 }, 355 passphrase: 'passphrase' 356 } 357 358 var testExpect = { 359 hasError: true, 360 errorMsg: "invalid gasLimit", 361 362 } 363 364 testSignTransaction(testInput, testExpect, done) 365 }); 366 367 it('13. `gas_limit` alpha', done => { 368 var testInput = { 369 transaction: { 370 from: address, 371 to: coinbase, 372 value: "123", 373 nonce: "10000", 374 gas_price: "1000000", 375 gas_limit: "aaz", 376 contract: {} 377 }, 378 passphrase: 'passphrase' 379 } 380 381 var testExpect = { 382 hasError: true, 383 errorMsg: "invalid gasLimit", 384 385 } 386 387 testSignTransaction(testInput, testExpect, done) 388 }); 389 390 it('14. `gas_limit` negative', done => { 391 var testInput = { 392 transaction: { 393 from: address, 394 to: coinbase, 395 value: "123", 396 nonce: "10000", 397 gas_price: "1000000", 398 gas_limit: "-10000", 399 contract: {} 400 }, 401 passphrase: 'passphrase' 402 } 403 404 var testExpect = { 405 hasError: true, 406 errorMsg: "invalid gasLimit", 407 408 } 409 410 testSignTransaction(testInput, testExpect, done) 411 }); 412 413 it('15. `contract` empty', done => { 414 var testInput = { 415 transaction: { 416 from: address, 417 to: coinbase, 418 value: "123", 419 nonce: "10000", 420 gas_price: "1000000", 421 gas_limit: "1000000", 422 contract: {} 423 }, 424 passphrase: 'passphrase' 425 } 426 427 var testExpect = { 428 hasError: true, 429 errorMsg: "invalid contract", 430 431 } 432 433 testSignTransaction(testInput, testExpect, done) 434 }); 435 436 it('16. `contract`', done => { 437 var testInput = { 438 transaction: { 439 from: address, 440 to: coinbase, 441 value: "123", 442 nonce: "10000", 443 gas_price: "1000000", 444 gas_limit: "1000000", 445 contract: { 446 "function": "save", 447 } 448 }, 449 passphrase: 'passphrase' 450 } 451 452 var testExpect = { 453 hasError: true, 454 errorMsg: "invalid contract", 455 456 } 457 458 testSignTransaction(testInput, testExpect, done) 459 }); 460 461 it('17. `invalid type`', done => { 462 var testInput = { 463 transaction: { 464 from: address, 465 to: coinbase, 466 value: "123", 467 nonce: "10000", 468 gas_price: "1000000", 469 gas_limit: "1000000", 470 contract: { 471 "function": "save", 472 }, 473 type: "invalid" 474 }, 475 passphrase: 'passphrase' 476 } 477 478 var testExpect = { 479 hasError: true, 480 errorMsg: "invalid transaction data payload type", 481 482 } 483 484 testSignTransaction(testInput, testExpect, done) 485 }); 486 487 it('18. `binary type`', done => { 488 var testInput = { 489 transaction: { 490 from: address, 491 to: coinbase, 492 value: "123", 493 nonce: "10000", 494 gas_price: "1000000", 495 gas_limit: "1000000", 496 type: "binary" 497 }, 498 passphrase: 'passphrase' 499 } 500 501 var testExpect = { 502 hasError: false, 503 errorMsg: "", 504 505 } 506 507 testSignTransaction(testInput, testExpect, done) 508 }); 509 510 it('19. `deploy type`', done => { 511 var testInput = { 512 transaction: { 513 from: address, 514 to: coinbase, 515 value: "123", 516 nonce: "10000", 517 gas_price: "1000000", 518 gas_limit: "1000000", 519 type: "deploy", 520 contract: { 521 "source": "var a = {}", 522 "source_type": "ts" 523 } 524 }, 525 passphrase: 'passphrase' 526 } 527 528 var testExpect = { 529 hasError: false, 530 errorMsg: "", 531 532 } 533 534 testSignTransaction(testInput, testExpect, done) 535 }); 536 537 it('20. `deploy type parse err`', done => { 538 var testInput = { 539 transaction: { 540 from: address, 541 to: coinbase, 542 value: "123", 543 nonce: "10000", 544 gas_price: "1000000", 545 gas_limit: "1000000", 546 type: "deploy", 547 contract: { 548 "source": "var a = {}" 549 } 550 }, 551 passphrase: 'passphrase' 552 } 553 554 var testExpect = { 555 hasError: true, 556 errorMsg: "invalid source type of deploy payload", 557 558 } 559 560 testSignTransaction(testInput, testExpect, done) 561 }); 562 563 it('21. `call type`', done => { 564 var testInput = { 565 transaction: { 566 from: address, 567 to: coinbase, 568 value: "123", 569 nonce: "10000", 570 gas_price: "1000000", 571 gas_limit: "1000000", 572 type: "call", 573 contract: { 574 "function": "save" 575 } 576 }, 577 passphrase: 'passphrase' 578 } 579 580 var testExpect = { 581 hasError: false, 582 errorMsg: "", 583 584 } 585 586 testSignTransaction(testInput, testExpect, done) 587 }); 588 it('22. `call type function err`', done => { 589 var testInput = { 590 transaction: { 591 from: address, 592 to: coinbase, 593 value: "123", 594 nonce: "10000", 595 gas_price: "1000000", 596 gas_limit: "1000000", 597 type: "call", 598 contract: { 599 "function": "_save" 600 } 601 }, 602 passphrase: 'passphrase' 603 } 604 605 var testExpect = { 606 hasError: true, 607 errorMsg: "invalid function of call payload", 608 609 } 610 611 testSignTransaction(testInput, testExpect, done) 612 }); 613 it('23. `call type no function`', done => { 614 var testInput = { 615 transaction: { 616 from: address, 617 to: coinbase, 618 value: "123", 619 nonce: "10000", 620 gas_price: "1000000", 621 gas_limit: "1000000", 622 type: "call" 623 }, 624 passphrase: 'passphrase' 625 } 626 627 var testExpect = { 628 hasError: true, 629 errorMsg: "invalid function of call payload", 630 631 } 632 633 testSignTransaction(testInput, testExpect, done) 634 }); 635 });