-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_genesis.js
189 lines (166 loc) · 6.64 KB
/
create_genesis.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*jslint node: true */
"use strict";
const fs = require('fs');
const db = require('trustnote-common/db.js');
const eventBus = require('trustnote-common/event_bus.js');
const constants = require('trustnote-common/constants.js');
var objectHash = require('trustnote-common/object_hash.js');
var Mnemonic = require('bitcore-mnemonic');
var ecdsaSig = require('trustnote-common/signature.js');
var validation = require('trustnote-common/validation.js');
var headlessWallet = require('trustnote-headless');
const witness_budget = 1000000;
const witness_budget_count = 8;
const genesisConfigFile = "../../data/config.json";
const creation_message = "梦想从这里开始!,Dream begins here!"
var genesisConfigData = {};
var witnesses = [];
var allAddress = [];
var arrOutputs = [
{ address: "REPLACE_ARR_OUT_PUTS", amount: 0 } //第一笔,找零地址。
];
var contenta = fs.readFileSync('../../data/allAddress.json');
var contentb = fs.readFileSync('../../data/witnessAddress.json');
witnesses = JSON.parse(contentb);
allAddress = JSON.parse(contenta);
console.log(JSON.stringify(witnesses));
for (let address of allAddress) { // initial the payment arrOutputs
for (var i = 0; i < witness_budget_count; ++i) {
arrOutputs.push({ address: address, amount: witness_budget });
}
}
function createPayment(from_address) {
console.log('starting createPayment');
var composer = require('trustnote-common/composer.js');
var network = require('trustnote-common/network.js');
var callbacks = composer.getSavingCallbacks({
ifNotEnoughFunds: onError,
ifError: onError,
ifOk: function (objJoint) {
network.broadcastJoint(objJoint);
}
});
var from_address = "REPLACE_FROM_ADDRESS";
var payee_address = "REPLACE_PAYEE_ADDRESS";
var arrOutputs = [
{ address: from_address, amount: 0 }, // the change
{ address: payee_address, amount: 100 } // the receiver
];
composer.setGenesis(false);
composer.composePaymentJoint([from_address], arrOutputs, headlessWallet.signer, callbacks);
}
function rungen() {
fs.readFile(genesisConfigFile, 'utf8', function (err, data) {
if (err) {
console.log("Read genesis input file \"bgenesis.json\" failed: " + err);
process.exit(0);
}
// set global data
genesisConfigData = JSON.parse(data);
console.log("Read genesis input data\n: %s", JSON.stringify(genesisConfigData, null, 2));
createGenesisUnit(witnesses, function (genesisHash) {
console.log("\n\n---------->>->> Genesis d, hash=" + genesisHash + "\n\n");
fs.writeFileSync("../../genesisHash.txt", genesisHash);
// setTimeout(createPayment, 1000 * 30);
//
setTimeout(function(){
process.exit(0);
}, 1000 * 60);
// var placeholders = Array.apply(null, Array(witnesses.length)).map(function(){ return '(?)'; }).join(',');
// console.log('will insert witnesses', witnesses);
// var witnesslist = witnesses;
// db.query("INSERT INTO my_witnesses (address) VALUES "+placeholders, witnesses, function(){
// console.log('inserted witnesses');
// setInterval(createPayment,1000*30)
// });
});
})
}
function onError(err) {
throw Error(err);
}
function getConfEntryByAddress(address) {
for (let item of genesisConfigData) {
if (item["address"] === address) {
return item;
}
}
console.log(" \n >> Error: witness address "
+ address + " not founded in the \"bgensis.json\" file!!!!\n");
process.exit(0);
//return null;
}
function getDerivedKey(mnemonic_phrase, passphrase, account, is_change, address_index) {
console.log("**************************************************************");
console.log(mnemonic_phrase);
var mnemonic = new Mnemonic(mnemonic_phrase);
var xPrivKey = mnemonic.toHDPrivateKey(passphrase);
//console.log(">> about to signature with private key: " + xPrivKey);
var path = "m/44'/0'/" + account + "'/" + is_change + "/" + address_index;
var derivedPrivateKey = xPrivKey.derive(path).privateKey;
console.log(">> derived key: " + derivedPrivateKey);
return derivedPrivateKey.bn.toBuffer({ size: 32 }); // return as buffer
}
// signer that uses witeness address
var signer = {
readSigningPaths: function (conn, address, handleLengthsBySigningPaths) {
handleLengthsBySigningPaths({ r: constants.SIG_LENGTH });
},
readDefinition: function (conn, address, handleDefinition) {
var conf_entry = getConfEntryByAddress(address);
// console.log(" \n\n conf_entry is ---> \n" + JSON.stringify(conf_entry,null,2));
var definition = conf_entry["definition"];
handleDefinition(null, definition);
},
sign: function (objUnsignedUnit, assocPrivatePayloads, address, signing_path, handleSignature) {
var buf_to_sign = objectHash.getUnitHashToSign(objUnsignedUnit);
var item = getConfEntryByAddress(address);
var derivedPrivateKey = getDerivedKey(
item["mnemonic_phrase"],
item["passphrase"],
0,
item["is_change"],
item["address_index"]
);
handleSignature(null, ecdsaSig.sign(buf_to_sign, derivedPrivateKey));
}
};
function createGenesisUnit(witnesses, onDone) {
var composer = require('trustnote-common/composer.js');
var network = require('trustnote-common/network.js');
var savingCallbacks = composer.getSavingCallbacks({
ifNotEnoughFunds: onError,
ifError: onError,
ifOk: function (objJoint) {
network.broadcastJoint(objJoint);
onDone(objJoint.unit.unit);
}
});
composer.setGenesis(true);
var genesisUnitInput = {
witnesses: witnesses,
paying_addresses: witnesses,
outputs: arrOutputs,
signer: signer,
callbacks: {
ifNotEnoughFunds: onError,
ifError: onError,
ifOk: function (objJoint, assocPrivatePayloads, composer_unlock) {
constants.GENESIS_UNIT = objJoint.unit.unit;
savingCallbacks.ifOk(objJoint, assocPrivatePayloads, composer_unlock);
}
},
messages: [{
app: "text",
payload_location: "inline",
payload_hash: objectHash.getBase64Hash(creation_message),
payload: creation_message
}]
};
composer.composeJoint(genesisUnitInput);
}
eventBus.once('headless_wallet_ready', function () {
console.log("创建创世单元");
rungen();
}
);