Skip to content

Commit

Permalink
nvm: update blockchain.js crypto.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bibibong committed Jun 6, 2018
1 parent dfa0845 commit b4431a7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
"assert.js": {"1.0.0"},
"instruction_counter.js": {"1.0.0"},
"typescriptServices.js": {"1.0.0"},
"blockchain.js": {"1.0.0"},
"blockchain.js": {"1.0.0", "1.0.5"},
"console.js": {"1.0.0"},
"event.js": {"1.0.0"},
"storage.js": {"1.0.0"},
Expand Down
8 changes: 4 additions & 4 deletions nf/nvm/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
//export Sha256Func
func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))
*gasCnt = C.size_t(len(s) * 100)

r := hash.Sha256([]byte(s))
return C.CString(byteutils.Hex(r))
Expand All @@ -43,7 +43,7 @@ func Sha256Func(data *C.char, gasCnt *C.size_t) *C.char {
//export Sha3256Func
func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))
*gasCnt = C.size_t(len(s) * 100)

r := hash.Sha3256([]byte(s))
return C.CString(byteutils.Hex(r))
Expand All @@ -53,7 +53,7 @@ func Sha3256Func(data *C.char, gasCnt *C.size_t) *C.char {
//export Ripemd160Func
func Ripemd160Func(data *C.char, gasCnt *C.size_t) *C.char {
s := C.GoString(data)
*gasCnt = C.size_t(1000 + len(s))
*gasCnt = C.size_t(len(s) * 100)

r := hash.Ripemd160([]byte(s))
return C.CString(byteutils.Hex(r))
Expand All @@ -65,7 +65,7 @@ func RecoverAddressFunc(alg int, data, sign *C.char, gasCnt *C.size_t) *C.char {
d := C.GoString(data)
s := C.GoString(sign)

*gasCnt = C.size_t(0)
*gasCnt = C.size_t(1000)

plain, err := byteutils.FromHex(d)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ func TestMultiLibVersion(t *testing.T) {
{"test/test_uint.js", nil, "\"\""},
{"test/test_date_1.0.5.js", nil, "\"\""},
{"test/test_crypto.js", nil, "\"\""},
{"test/test_blockchain_1.0.5.js", nil, "\"\""},
}

for _, tt := range tests {
Expand Down
51 changes: 51 additions & 0 deletions nf/nvm/test/test_blockchain_1.0.5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2018 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//

'use strict';

if (typeof _native_blockchain === "undefined") {
throw new Error("_native_blockchain is undefined.");
}

var ok = Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", "1");
console.log("transfer:" + ok)

var result = Blockchain.verifyAddress("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE");
console.log("verifyAddress:" + result)

try {
Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", -1);
} catch (err) {
if (err.message !== "invalid value" ) {
throw err;
}
}
try {
Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", NaN);
} catch (err) {
if (err.message !== "invalid value" ) {
throw err;
}
}
try {
Blockchain.transfer("n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE", Infinity);
} catch (err) {
if (err.message !== "invalid value" ) {
throw err;
}
}
82 changes: 82 additions & 0 deletions nf/nvm/v8/libjs/1.0.5/blockchain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (C) 2017 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//

'use strict';

var Blockchain = function () {
Object.defineProperty(this, "nativeBlockchain", {
configurable: false,
enumerable: false,
get: function(){
return _native_blockchain;
}
});
};

Blockchain.prototype = {
AccountAddress: 0x57,
ContractAddress: 0x58,

blockParse: function (str) {
var block = JSON.parse(str);
if (block != null) {
var fb = Object.freeze(block);
Object.defineProperty(this, "block", {
configurable: false,
enumerable: false,
get: function(){
return fb;
}
});
}
},
transactionParse: function (str) {
var tx = JSON.parse(str);
if (tx != null) {
var value = tx.value === undefined || tx.value.length === 0 ? "0" : tx.value;
tx.value = new BigNumber(value);
var gasPrice = tx.gasPrice === undefined || tx.gasPrice.length === 0 ? "0" : tx.gasPrice;
tx.gasPrice = new BigNumber(gasPrice);
var gasLimit = tx.gasLimit === undefined || tx.gasLimit.length === 0 ? "0" : tx.gasLimit;
tx.gasLimit = new BigNumber(gasLimit);

var ft = Object.freeze(tx);
Object.defineProperty(this, "transaction", {
configurable: false,
enumerable: false,
get: function(){
return ft;
}
});
}
},
transfer: function (address, value) {
if (!(value instanceof BigNumber)) {
value = new BigNumber(value);
}
if (value.isNaN() || value.isNegative() || !value.isFinite()) {
throw new Error("invalid value");
}
var ret = this.nativeBlockchain.transfer(address, value.toString(10));
return ret == 0;
},
verifyAddress: function (address) {
return this.nativeBlockchain.verifyAddress(address);
}
};
module.exports = new Blockchain();

0 comments on commit b4431a7

Please sign in to comment.