-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJAVASTAMPMINTERAPP
186 lines (164 loc) · 5.88 KB
/
JAVASTAMPMINTERAPP
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
const os = require('os');
const fs = require('fs');
const path = require('path');
const base64 = require('base-64');
const fetch = require('node-fetch');
const qrcode = require('qrcode');
const { createCanvas } = require('canvas');
// Function to generate a random asset name with the given format
function generateAssetName() {
return 'A' + Math.floor(Math.random() * (10 ** 20 - 10 ** 19) + 10 ** 19);
}
// Function to check if the asset name is available using the API
async function checkAssetAvailability(assetName, url, auth) {
const payload = {
'method': 'get_asset_info',
'params': {
'assets': [assetName]
},
'jsonrpc': '2.0',
'id': 0
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + base64.encode(auth)
},
body: JSON.stringify(payload)
});
const result = await response.json();
// If the asset is not found, it's available
return 'error' in result && 'Asset not found' in result['error'];
}
// Function to generate a QR code PNG file
async function generateQRCodePNG(data, filename) {
const canvas = createCanvas(200, 200);
await qrcode.toCanvas(canvas, data);
const stream = canvas.createPNGStream();
const out = fs.createWriteStream(filename);
stream.pipe(out);
return new Promise((resolve) => {
out.on('finish', () => {
resolve();
});
});
}
// Set the URL, headers, and authentication for the API request
const url = 'https://api.counterparty.io';
const headers = { 'Content-Type': 'application/json' };
const auth = 'user:1234';
// Prompt the user for the transfer and source addresses
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
let transferAddress;
let sourceAddress;
readline.question('Enter the transfer address for the assets: ', (address) => {
transferAddress = address;
readline.question('Enter the source address for the assets: ', (address) => {
sourceAddress = address;
readline.close();
// Get the full path to the IN directory
const inDir = path.join(__dirname, 'IN');
// Check if IN directory exists
if (!fs.existsSync(inDir)) {
console.log('IN directory not found.');
return;
}
// Get list of files in the IN directory
const files = fs.readdirSync(inDir).filter(file => fs.statSync(path.join(inDir, file)).isFile());
if (!files.length) {
console.log('No files found in IN directory.');
return;
}
let totalSize = 0;
// Loop through each file in the IN directory and convert it to base64
for (const fileName of files) {
const file = fs.readFileSync(path.join(inDir, fileName));
const base64Data = base64.encode(file.toString('binary'));
totalSize += base64Data.length;
// Generate a random asset name and check its availability
let assetName = generateAssetName();
while (!(await checkAssetAvailability(assetName, url, auth))) {
assetName = generateAssetName();
}
// Calculate the price for the issuance based on the size of the data
const price = totalSize * 0.0001; // 0.0001 satoshi per byte
const commission = price * 0.2; // 20% commission
const totalCost = price + commission;
// Create a payload with the base64-encoded data in the description field
const payload = {
method: "create_issuance",
params: {
source: sourceAddress,
asset: assetName,
quantity: 1,
divisible: false,
description: `${fileName}: ${base64Data}`,
lock: true,
transfer_destination: transferAddress,
reset: false,
allow_unconfirmed_inputs: true
},
jsonrpc: "2.0",
id: 0
};
// Send the API request
fetch(url, {
method: "POST",
body: JSON.stringify(payload),
headers: headers,
auth: auth
})
.then(response => response.json())
.then(result => {
// Check if the result contains the issuance transaction ID
if (result.hasOwnProperty("result")) {
const txId = result.result;
console.log(`Issuance successful. Transaction ID: ${txId}`);
// Save issuance as a JSON file in the OUT directory
const issuance = {
asset_name: assetName,
file_name: fileName,
tx_id: txId
};
const outDir = path.join(__dirname, "venv", "OUT");
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
const jsonFilePath = path.join(outDir, `${assetName}.json`);
fs.writeFileSync(jsonFilePath, JSON.stringify(issuance));
// Send commission to specific address
const commissionPayload = {
method: "send",
params: {
source: sourceAddress,
destination: commissionAddress,
asset: "BTC",
quantity: commission
},
jsonrpc: "2.0",
id: 0
};
fetch(url, {
method: "POST",
body: JSON.stringify(commissionPayload),
headers: headers,
auth: auth
})
.then(response => response.json())
.then(result => {
if (result.hasOwnProperty("result")) {
console.log(`Commission sent successfully to ${commissionAddress}`);
} else {
console.error(`Failed to send commission. Error: ${result.error}`);
}
})
.catch(error => console.error(`Error sending commission: ${error}`));
} else {
console.error(`Failed to create issuance. Error: ${result.error}`);
}
})
.catch(error => console.error(`Error creating issuance: ${error}`));