github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/1.0.5/blockchain.js (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 'use strict'; 20 21 var Blockchain = function () { 22 Object.defineProperty(this, "nativeBlockchain", { 23 configurable: false, 24 enumerable: false, 25 get: function(){ 26 return _native_blockchain; 27 } 28 }); 29 }; 30 31 Blockchain.prototype = { 32 AccountAddress: 0x57, 33 ContractAddress: 0x58, 34 35 blockParse: function (str) { 36 var block = JSON.parse(str); 37 if (block != null) { 38 var fb = Object.freeze(block); 39 Object.defineProperty(this, "block", { 40 configurable: false, 41 enumerable: false, 42 get: function(){ 43 return fb; 44 } 45 }); 46 } 47 }, 48 transactionParse: function (str) { 49 var tx = JSON.parse(str); 50 if (tx != null) { 51 var value = tx.value === undefined || tx.value.length === 0 ? "0" : tx.value; 52 tx.value = new BigNumber(value); 53 var gasPrice = tx.gasPrice === undefined || tx.gasPrice.length === 0 ? "0" : tx.gasPrice; 54 tx.gasPrice = new BigNumber(gasPrice); 55 var gasLimit = tx.gasLimit === undefined || tx.gasLimit.length === 0 ? "0" : tx.gasLimit; 56 tx.gasLimit = new BigNumber(gasLimit); 57 58 var ft = Object.freeze(tx); 59 Object.defineProperty(this, "transaction", { 60 configurable: false, 61 enumerable: false, 62 get: function(){ 63 return ft; 64 } 65 }); 66 } 67 }, 68 transfer: function (address, value) { 69 if (!Uint.isUint(value)) { 70 if (!(value instanceof BigNumber)) { 71 value = new BigNumber(value); 72 } 73 if (value.isNaN() || value.isNegative() || !value.isFinite()) { 74 throw new Error("invalid value"); 75 } 76 } 77 78 var ret = this.nativeBlockchain.transfer(address, value.toString(10)); 79 return ret == 0; 80 }, 81 82 verifyAddress: function (address) { 83 return this.nativeBlockchain.verifyAddress(address); 84 }, 85 86 getAccountState: function(address) { 87 if (address) { 88 var result = this.nativeBlockchain.getAccountState(address); 89 if (result) { 90 return JSON.parse(result); 91 } else { 92 throw "getAccountState: invalid address"; 93 } 94 } else { 95 throw "getAccountState: inValid address"; 96 } 97 }, 98 99 getPreBlockHash: function (offset) { 100 offset = parseInt(offset); 101 if (!offset) { 102 throw "getPreBlockHash: invalid offset" 103 } 104 105 if (offset <= 0) { 106 throw "getPreBlockHash: offset should large than 0" 107 } 108 109 if (offset >= this.block.height) { 110 throw "getPreBlockHash: block not exist" 111 } 112 113 return this.nativeBlockchain.getPreBlockHash(offset); 114 }, 115 116 getPreBlockSeed: function (offset) { 117 offset = parseInt(offset); 118 if (!offset) { 119 throw "getPreBlockSeed: invalid offset" 120 } 121 122 if (offset <= 0) { 123 throw "getPreBlockSeed: offset should large than 0" 124 } 125 126 if (offset >= this.block.height) { 127 throw "getPreBlockSeed: block not exist" 128 } 129 130 return this.nativeBlockchain.getPreBlockSeed(offset); 131 } 132 }; 133 module.exports = new Blockchain();