This repository has been archived by the owner on Oct 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
txGenerator.js
164 lines (137 loc) · 4.54 KB
/
txGenerator.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
var readline = require('readline');
const Buffer = require('buffer').Buffer;
var fs = require('fs');
const os = require('os');
var decode = require('./txDecoder');
let crypto = require('crypto')
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var numInputs;
// Stores the numbers of the num keys, keys themselves should be available in .dcoin directory
var inputPubKeyNum = [];
var numOutputs;
var outputPubKeyNum = [];
var outValue = [];
function getDataUserInputTx() {
var promise = new Promise(function(resolve, reject) {
rl.question("Please provide number of inputs: ", function(numInputsUsr) {
numInputs = numInputsUsr;
for (let i = 0; i < numInputs; i++) {
rl.question("Please provide input public key number " + (i + 1).toString() + ": ", function(pubKeyNum) {
inputPubKeyNum[i] = pubKeyNum;
resolve();
})
}
})
})
return promise;
}
function getDataUser() {
var promise = new Promise(function(resolve, reject) {
getDataUserInputTx().then(function() {
rl.question("Please provide number of outputs: ", function(numOutputsUsr) {
numOutputs = numOutputsUsr;
for (let i = 0; i < numOutputs; i++) {
rl.question("Please provide output public key number " + (i + 1).toString() + ": ", function(outAddress) {
outputPubKeyNum[i] = outAddress;
rl.question("Please provide output value " + (i + 1).toString() + ": ", function(outValueI) {
outValue[i] = outValueI;
rl.close();
resolve();
})
})
}
});
});
})
return promise;
}
async function getKeyFromFile(num, type) {
console.log("Openning file: " + os.homedir() + "/.dcoin/rsa" + type + "key" + num + ".pem");
let concatenatedKey = "";
let data = fs.readFileSync(os.homedir() + "/.dcoin/rsa" + type + "key" + num + ".pem", 'utf8')
let splitArray = data.split('\n');
// Skip the first and last sentence as they do not contain data
for(let i=1; i < splitArray.length -2; ++i) {
concatenatedKey += splitArray[i];
}
return concatenatedKey;
}
/*
* 1) 1 byte - number of Inputs (X)
*
* 2) 2 bytes - number of bytes of hash (Y)
* 3) Y bytes - Hash of input UTO
*
* 2 and 3 repeat X times
*/
async function addInputs(buffer, offset) {
buffer.writeUInt8(numInputs, offset.value);
offset.value += 1;
for (let i = 0; i < numInputs; i++) {
let pubKey = await getKeyFromFile(inputPubKeyNum[i], "pub")
const txHashBuffer = crypto.createHash('sha256').update(pubKey).digest();
buffer.writeUInt16BE(txHashBuffer.length, offset.value);
offset.value += 2;
txHashBuffer.copy(buffer, offset.value, 0, txHashBuffer.length);
offset.value += txHashBuffer.length;
buffer.writeUInt16BE(pubKey.length, offset.value);
console.log("pubkey length written as " + pubKey.length)
console.log("pubkey written as " + pubKey)
offset.value += 2;
buffer.write(pubKey, offset.value);
offset.value += pubKey.length;
}
}
async function addOutputs(buffer, offset) {
buffer.writeUInt8(numOutputs, offset.value);
offset.value += 1;
console.log("Before loop ")
for (let i = 0; i < numOutputs; i++) {
let pubKey = await getKeyFromFile(outputPubKeyNum[i], "pub")
const txHashOut = crypto.createHash('sha256').update(pubKey).digest('hex');
console.log("inside loop ");
buffer.writeUInt16BE(txHashOut.length, offset.value);
offset.value += 2;
buffer.write(txHashOut, offset.value);
offset.value += txHashOut.length;
buffer.writeUInt32BE(outValue[i], offset.value);
offset.value += 4;
}
}
async function buildString() {
// Pre-reserver a huge buffer. TODO shrink it if possible
var buffer = Buffer.alloc(10000);
var offset = { value: 0 };
await addInputs(buffer, offset)
await addOutputs(buffer, offset)
return {buffer: buffer, offset: offset};
}
getDataUser()
.then(function () {
var promise = new Promise(function(resolve, reject) {
buildString()
.then(function(result) {
resolve(result)
})
})
return promise;
})
.then(function (result) {
outMsg = {
"method": "broadcast_tx_sync",
"jsonrpc": "2.0",
"id": "dontcare2"
}
console.log("Offset at last is " + result.offset.value)
outMsg.params = [result.buffer.toString('base64', 0, result.offset.value)];
fs.writeFile("preparedMsg.txt", JSON.stringify(outMsg), function(err) {
console.log("File is written")
});
//decode.decodeTx(result.buffer);
})
/*setTimeout(function(str1, str2) {
console.log("timeout");
}, 1000);*/