-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
366 lines (332 loc) · 14.4 KB
/
index.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { serve } from "bun";
import { $ } from "bun";
import * as nanocurrency from 'nanocurrency';
import html from "./public/index.html" with { type: "text" };
import { block } from 'nanocurrency-web';
import { box } from 'nanocurrency-web'
interface AccountInfo {
frontier: string;
balance: string;
representative?: string;
}
// Helper: Generate work using the local nano work server on port 7076
async function getWork(hash: string): Promise<string> {
const workRequest = {
action: "work_generate",
hash: hash,
difficulty: "fffffff800000000" // adjust difficulty as needed
};
const response = await fetch("http://localhost:7076", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(workRequest)
});
const result = await response.json();
if (result.error) {
throw new Error(`Work generation error: ${result.error}`);
}
return result.work;
}
async function getAccountInfo(address: string): Promise<AccountInfo> {
try {
const response = await fetch('https://app.natrium.io/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'account_info', account: address })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// If account not found, return default values
if (data.error === "Account not found") {
return {
frontier: "0000000000000000000000000000000000000000000000000000000000000000",
balance: "0",
representative: "nano_1xnopayemjmbxnw7e5w769tjs8eyxb7das5mredj4fnutu544ef4pf8n3y4p"
};
}
return {
frontier: data.frontier,
balance: data.balance,
representative: data.representative
};
} catch (error) {
console.error('Error fetching account info:', error);
throw error;
}
}
// Helper: Get pending transaction for an account (if any)
async function getPendingTransaction(address: string): Promise<{ hash: string, amount: string }> {
const response = await fetch('https://app.natrium.io/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'receivable',
account: address,
count: "1",
threshold: "1"
})
});
const result = await response.json();
console.log("Receivable result:", result);
if (!result.blocks || Object.keys(result.blocks).length === 0) {
throw new Error('No pending transactions found');
}
// Use the first pending transaction
const pendingHashes = Object.keys(result.blocks);
return { hash: pendingHashes[0], amount: result.blocks[pendingHashes[0]] };
}
// Helper: simple delay
async function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getLastTransactionTime(fromGuy: string, toGuy: string): Promise<string> {
try {
// Fetch account history using Bun.fetch()
const response = await Bun.fetch("https://app.natrium.io/api", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
action: "account_history",
account: fromGuy,
count: "100"
})
});
// Parse JSON response
const data = await response.json();
if (!data.history || data.history.length === 0) {
throw new Error("No transaction history found.");
}
// Loop through history to find a matching "send" transaction
for (const tx of data.history) {
if (tx.type === "send" && tx.account === toGuy) {
if (tx.local_timestamp) {
return new Date(Number(tx.local_timestamp) * 1000).toLocaleString(); // Convert timestamp to human-readable format
} else {
return "Timestamp not available";
}
}
}
throw new Error("No matching transaction found.");
} catch (error) {
return `Error: ${error.message}`;
}
}
async function receiveFunds(privateKey: string, pendingTx: { hash: string, amount: string }): Promise<{ hash: string, block: any }> {
const publicKey = await nanocurrency.derivePublicKey(privateKey);
const accountAddress = await nanocurrency.deriveAddress(publicKey, { useNanoPrefix: true });
// Get current account info; if not opened, assume balance = 0 and genesis frontier.
let frontier = "0000000000000000000000000000000000000000000000000000000000000000";
let currentBalance = "0";
try {
const info = await getAccountInfo(accountAddress);
console.log("Account info when receiving:", JSON.stringify(info));
frontier = info.frontier;
currentBalance = info.balance;
} catch (err) {
console.log("Account not opened yet; assuming balance 0 and using genesis frontier.");
}
console.log('Current balance:', currentBalance);
console.log('Pending amount:', pendingTx.amount);
// Ensure we're working with clean string numbers without scientific notation
const cleanCurrentBalance = BigInt(currentBalance).toString();
const cleanPendingAmount = BigInt(pendingTx.amount).toString();
// Calculate new balance
const newBalance = BigInt(cleanCurrentBalance) + BigInt(cleanPendingAmount);
console.log('New balance:', newBalance.toString());
const work = await getWork(frontier === "0000000000000000000000000000000000000000000000000000000000000000" ? publicKey : frontier);
const receiveData = {
walletBalanceRaw: currentBalance.toString(),
toAddress: accountAddress,
// For simplicity, using the account's address as its representative; adjust if needed.
representativeAddress: accountAddress,
frontier: frontier,
transactionHash: pendingTx.hash,
amountRaw: pendingTx.amount,
work: work
};
const signedBlock = block.receive(receiveData, privateKey);
console.log("Signed block:", signedBlock);
const processRequest = {
action: 'process',
block: JSON.stringify(signedBlock)
};
const response = await fetch('https://app.natrium.io/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(processRequest)
});
const result = await response.json();
if (result.error) {
throw new Error(`Process error on receive: ${result.error}`);
}
return {
hash: signedBlock.hash,
block: signedBlock
};
}
async function transferAllFunds(privateKey: string, destinationAddress: string) {
try {
const DEFAULT_REPRESENTATIVE = 'nano_1natrium1o3z5519ifou7xii8crpxpk8y65qmkih8e8bpsjri651oza8imdd';
const publicKey = await nanocurrency.derivePublicKey(privateKey);
const senderAddress = await nanocurrency.deriveAddress(publicKey, { useNanoPrefix: true });
console.log('Sender address:', senderAddress);
const accountInfo = await getAccountInfo(senderAddress);
console.log('Account info:', accountInfo);
const work = await getWork(accountInfo.frontier);
console.log('Generated work:', work);
const sendBlockData = {
walletBalanceRaw: accountInfo.balance,
fromAddress: senderAddress,
toAddress: destinationAddress,
representativeAddress: accountInfo.representative || DEFAULT_REPRESENTATIVE,
frontier: accountInfo.frontier,
amountRaw: accountInfo.balance, // sending entire balance
work: work
};
const signedBlock = block.send(sendBlockData, privateKey);
const processRequest = {
action: 'process',
block: JSON.stringify(signedBlock)
};
const response = await fetch('https://app.natrium.io/api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(processRequest)
});
const result = await response.json();
if (result.error) {
throw new Error(`Process error: ${result.error}`);
}
return {
hash: signedBlock.hash,
block: signedBlock,
senderAddress,
destinationAddress,
amount: accountInfo.balance
};
} catch (error) {
console.error('Error in transfer:', error);
throw error;
}
}
serve({
port: 8000,
async fetch(req) {
if (req.method === "GET") {
return new Response(html, { headers: { "Content-Type": "text/html" } });
}
if (req.method === "POST") {
const formData = await req.formData();
// NEW: Check if a concatenated encrypted string is provided.
const concatenatedString = formData.get("concatenated")?.toString().trim() || "";
if (concatenatedString) {
try {
// Inline definition of extractStrings
function extractStrings(concatenated: string): { derivedAddress: string; baoHash: string; encrypted: string } {
const parts = concatenated.split(',');
if (parts.length !== 3) {
throw new Error('Invalid concatenated string format');
}
return {
derivedAddress: parts[0],
baoHash: parts[1],
encrypted: parts[2]
};
}
const { derivedAddress, baoHash, encrypted } = extractStrings(concatenatedString);
// Decrypt using the extracted values.
const decrypted = box.decrypt(encrypted, derivedAddress, baoHash);
// Compute the BAO-derived public address from baoHash.
const derivedPublicKeyFromBao = await nanocurrency.derivePublicKey(baoHash);
const derivedAddressFromBao = await nanocurrency.deriveAddress(derivedPublicKeyFromBao, { useNanoPrefix: true });
// Get the time of the last transaction between the BAO-derived address and the original account address.
const lastTransactionTime = await getLastTransactionTime(derivedAddressFromBao, derivedAddress);
return new Response(
`<h2>Decrypted Text: ${decrypted}<br>
Last Transaction Time: ${lastTransactionTime}</h2>
<a href="/">Go Back</a>`,
{ headers: { "Content-Type": "text/html" } }
);
} catch (error) {
console.error("Error during decryption:", error);
return new Response(
`<h2>Error during decryption. Please try again.</h2><a href="/">Go Back</a>`,
{ headers: { "Content-Type": "text/html" } }
);
}
}
// --- Original branch when concatenated string is not provided ---
const text = formData.get("text")?.toString() || "";
const nanoPrivateKey = formData.get("nano_private_key")?.toString() || "";
// Generate BAO hash using an external tool.
const processOutput = await $`${process.platform === 'win32' ? 'echo' : 'printf'} ${text} | bao hash`.text();
const baoHash = processOutput.trim();
console.log("BAO hash (from text):", baoHash);
try {
// Derive addresses:
// - derivedAddress: BAO account (from baoHash).
// - derivedAddressofnanoPrivatekey: Original account (from nanoPrivateKey).
const derivedPublicKey = await nanocurrency.derivePublicKey(baoHash);
const derivedAddress = await nanocurrency.deriveAddress(derivedPublicKey, { useNanoPrefix: true });
const derivedPublicKeyofnanoPrivatekey = await nanocurrency.derivePublicKey(nanoPrivateKey);
const derivedAddressofnanoPrivatekey = await nanocurrency.deriveAddress(derivedPublicKeyofnanoPrivatekey, { useNanoPrefix: true });
console.log("Derived BAO address:", derivedAddress);
console.log("Original account address:", derivedAddressofnanoPrivatekey);
const encrypted = box.encrypt(text, derivedAddress, nanoPrivateKey)
console.log(encrypted)
// --- First Transfer ---
// Original account sends funds to BAO account.
const firstTransferResult = await transferAllFunds(nanoPrivateKey, derivedAddress);
console.log("First transfer result:", firstTransferResult);
await delay(5000); // wait for network propagation
// --- First Receive ---
// BAO account claims pending funds.
const pendingTxForBAO = await getPendingTransaction(derivedAddress);
console.log("Pending transaction for BAO account:", pendingTxForBAO);
const firstReceiveResult = await receiveFunds(baoHash, pendingTxForBAO);
console.log("First receive result:", firstReceiveResult);
await delay(5000); // wait for network propagation
// --- Second Transfer ---
// BAO account sends funds back to original account.
const secondTransferResult = await transferAllFunds(baoHash, derivedAddressofnanoPrivatekey);
console.log("Second transfer result:", secondTransferResult);
await delay(5000); // wait for network propagation
// --- Second Receive ---
// Original account claims pending funds from second transfer.
const pendingTxForOriginal = await getPendingTransaction(derivedAddressofnanoPrivatekey);
console.log("Pending transaction for original account:", pendingTxForOriginal);
const secondReceiveResult = await receiveFunds(nanoPrivateKey, pendingTxForOriginal);
console.log("Second receive result:", secondReceiveResult);
// Concatenate the key values into one string.
function concatenateStrings(derivedAddress: string, baoHash: string, encrypted: string): string {
return `${derivedAddress},${baoHash},${encrypted}`;
}
let concatenated_encrypted_string = concatenateStrings(derivedAddressofnanoPrivatekey, baoHash, encrypted);
console.log(concatenated_encrypted_string)
return new Response(
`<h2>BAO Hash: ${baoHash}<br>
Derived BAO Address: ${derivedAddress}<br>
Original Account Address: ${derivedAddressofnanoPrivatekey}<br>
First Transfer: ${JSON.stringify(firstTransferResult)}<br>
First Receive: ${JSON.stringify(firstReceiveResult)}<br>
Second Transfer: ${JSON.stringify(secondTransferResult)}<br>
Second Receive: ${JSON.stringify(secondReceiveResult)}</h2>
<h3>Concatenated Encrypted String:</h3>
<p>${concatenated_encrypted_string}</p>
<a href="/">Go Back</a>`,
{ headers: { "Content-Type": "text/html" } }
);
// const decrypted = box.decrypt(encrypted, derivedAddressofnanoPrivatekey, baoHash)
} catch (error) {
console.error("Error during transfers:", error);
return new Response(
`<h2>Error during transfers. Please try again.</h2><a href="/">Go Back</a>`,
{ headers: { "Content-Type": "text/html" } }
);
}
}
return new Response("Method Not Allowed", { status: 405 });
}
});