-
Notifications
You must be signed in to change notification settings - Fork 46
/
node-example.js
62 lines (52 loc) · 2.96 KB
/
node-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use strict";
var Nebulas = require("../index");
var Neb = Nebulas.Neb;
var neb = new Neb();
neb.setRequest(new Nebulas.HttpRequest("https://testnet.nebulas.io"));
neb.api.getAccountState("n1PfySvoUyNfWg6xKDohK96TCWbSxQXLdwB").then(function (state) {
console.log(state);
}).catch(function (err) {
console.log(err);
});
var Account = Nebulas.Account;
var account = Account.NewAccount();
console.log(account.getPrivateKeyString());
console.log(account.getPublicKeyString());
console.log(account.getAddressString());
console.log(Account.isValidAddress(account.getAddressString()));
var key = account.toKey("passphrase");
console.log(JSON.stringify(key));
console.log("********************");
var a1 = new Account();
a1 = a1.fromKey(key, "passphrase");
console.log(a1.getPrivateKeyString());
var Transaction = Nebulas.Transaction;
var tx = new Transaction(1001, account, account, "10", 1);
tx.signTransaction();
console.log("hash:" + tx.hash.toString("hex"));
console.log("sign:" + tx.sign.toString("hex"));
console.log(tx.toString());
var data = tx.toProtoString();
console.log(data);
tx.fromProto(data);
console.log(tx.toString());
console.log("address:"+tx.from.getAddressString());
var tx = Transaction.fromProto(data);
console.log(tx.toString());
var pubkey = Transaction.recover(tx.hash, tx.sign);
var formAddr = Account.fromPubKey(pubkey);
console.log("recover address:", formAddr.getAddressString());
if (formAddr.getAddressString() != tx.from.getAddressString()) {
throw new Error("transaction recover faild.");
}
var cryptoUtils = Nebulas.CryptoUtils;
console.log("black:" + cryptoUtils.sha3("").toString("hex"));
console.log("Hello, world:" + cryptoUtils.sha3("Hello, world").toString("hex"));
console.log("=======test compatibility version3/version4=======");
var v4 = '{"address":"n1J7bHLFjxggYu156tzEma8NNewCL9varnL","crypto":{"cipher":"aes-128-ctr","ciphertext":"cc1dce65f3beae84744a78d2f4a779549fb26cfe720b4d1f0c1b97593b4cc059","cipherparams":{"iv":"43b084fcaa4c723d269280705c67dc4f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":4096,"p":1,"r":8,"salt":"44a8704914b45e496138199fa92d9742257d49e5e02af2c9a3169bfe18078bc1"},"mac":"21e452d670c0ecf0a2db6f12322a8a482486d3941de1e59535063c9738d6b3ff","machash":"sha3256"},"id":"6eb10613-1d25-44f6-a5be-815919ea0ae5","version":4}';
var acc = new Account();
acc = acc.fromKey(v4, "passphrase", true);
var v3 = '{"address":"n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE","crypto":{"cipher":"aes-128-ctr","ciphertext":"b5041a4b9d4738bc2bcce580aeaadf53aa7c63b6aa3916b76c452630692fc397","cipherparams":{"iv":"f9d54f7854929e9e28731ee69d306a22"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":4096,"p":1,"r":8,"salt":"daa130fd5e3f9a77efe6028170becf7b1d9c73ce5c1d75d1142e90a68df12fed"},"mac":"aa390e6ed50741ed38670d1e1b11a1e44e174f9f66e41acc2e2d1762ebf1dfad","machash":"sha3256"},"id":"078fcad9-8f82-40e0-96c4-fb14b986c134","version":3}';
acc = new Account();
acc = acc.fromKey(v3, "passphrase", true);
console.log("=======test compatibility version3/version4 END=======");