1
0
Fork 0
forked from lthn/blockchain

added secp256k1_ecdsa example in JS/ethers

This commit is contained in:
cryptozoidberg 2024-10-01 15:53:14 +04:00
parent 890b3155b8
commit 74b0e6d70f
No known key found for this signature in database
GPG key ID: 2E10CC61CAC8F36D
8 changed files with 538 additions and 1 deletions

View file

@ -1433,7 +1433,7 @@ namespace tools
res.status = e.what();
return true;
}
res.status = API_RETURN_CODE_OK;
return true;
WALLET_RPC_CATCH_TRY_ENTRY();
}

2
utils/JS/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/node_modules*
.vscode

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

192
utils/JS/package-lock.json generated Normal file
View file

@ -0,0 +1,192 @@
{
"name": "JS",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"axios": "^1.7.7",
"ethers": "^6.13.2"
}
},
"node_modules/@adraffy/ens-normalize": {
"version": "1.10.1",
"resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.10.1.tgz",
"integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw=="
},
"node_modules/@noble/curves": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz",
"integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==",
"dependencies": {
"@noble/hashes": "1.3.2"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@noble/hashes": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz",
"integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==",
"engines": {
"node": ">= 16"
},
"funding": {
"url": "https://paulmillr.com/funding/"
}
},
"node_modules/@types/node": {
"version": "18.15.13",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz",
"integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q=="
},
"node_modules/aes-js": {
"version": "4.0.0-beta.5",
"resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.5.tgz",
"integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q=="
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "1.7.7",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz",
"integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/ethers": {
"version": "6.13.2",
"resolved": "https://registry.npmjs.org/ethers/-/ethers-6.13.2.tgz",
"integrity": "sha512-9VkriTTed+/27BGuY1s0hf441kqwHJ1wtN2edksEtiRvXx+soxRX3iSXTfFqq2+YwrOqbDoTHjIhQnjJRlzKmg==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/ethers-io/"
},
{
"type": "individual",
"url": "https://www.buymeacoffee.com/ricmoo"
}
],
"dependencies": {
"@adraffy/ens-normalize": "1.10.1",
"@noble/curves": "1.2.0",
"@noble/hashes": "1.3.2",
"@types/node": "18.15.13",
"aes-js": "4.0.0-beta.5",
"tslib": "2.4.0",
"ws": "8.17.1"
},
"engines": {
"node": ">=14.0.0"
}
},
"node_modules/follow-redirects": {
"version": "1.15.9",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz",
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/tslib": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
"integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ=="
},
"node_modules/ws": {
"version": "8.17.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.17.1.tgz",
"integrity": "sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

6
utils/JS/package.json Normal file
View file

@ -0,0 +1,6 @@
{
"dependencies": {
"axios": "^1.7.7",
"ethers": "^6.13.2"
}
}

View file

@ -0,0 +1,8 @@
{
"id": 0,
"jsonrpc": "2.0",
"result": {
"status": "",
"transfers_were_unlocked": false
}
}

227
utils/JS/test_eth_sig.js Normal file
View file

@ -0,0 +1,227 @@
const axios = require('axios');
const { ethers } = require("ethers");
const { exit } = require('process');
const fs = require('fs');
/// Define an async function that takes method name and parameters
async function callJsonRpc(requestData, port = 22222) {
try {
const response = await axios.post('http://127.0.0.1:' + port +'/json_rpc', requestData, {
headers: {
'Content-Type': 'application/json',
},
});
// Return the result from the JSON-RPC response
return response.data;
} catch (error) {
console.error('Error:', error);
throw error; // Optionally rethrow the error to handle it in the calling function
}
}
function reverseHexBytes(hexString) {
// Ensure the hex string length is even
if (hexString.length % 2 !== 0) {
throw new Error("Invalid hex string length");
}
// Split the hex string into chunks of 2 characters (1 byte)
const bytes = hexString.match(/.{1,2}/g);
// Reverse the array of bytes and join them back into a string
const reversedHex = bytes.reverse().join('');
return reversedHex;
}
async function deploy_asset()
{
try {
//Generated Private Key: 0x17a938099954cee510d7fc9eb2366f0762b093d9be547acabf8be85f774ef154
//Generated Address: 0x0886bA9F5b117D2A3C1ce18106F2Ce759f5D34C8
const loadedWallet = new ethers.Wallet("0x17a938099954cee510d7fc9eb2366f0762b093d9be547acabf8be85f774ef154");
console.log("Loaded Address:", loadedWallet.address);
console.log("Public key:", loadedWallet.signingKey.compressedPublicKey);
const owner_eth_pub_key = loadedWallet.signingKey.compressedPublicKey.substring(2);
console.log("Generated Public key HEX:", owner_eth_pub_key);
const jsonObject = {
id: 0,
jsonrpc: "2.0",
method: "deploy_asset",
params: {
asset_descriptor: {
//current_supply: 1000000000000000,
decimal_point: 12,
full_name: "Zano wrapped ABC",
hidden_supply: false,
meta_info: "Stable and private",
owner: "",
ticker: "ZABC",
total_max_supply: 1000000000000000000,
owner_eth_pub_key: owner_eth_pub_key
},
destinations: [
{
address: "ZxC1U6hoCRM9PBSwrBTrWD8XgcHqLNJN9NWqXs9o994eZuHHBvSAyBpQ4TbWSNoabUDPdD8iEM5ZjPoMM7jE48mp2iKcVHLSK",
amount: 1000000000000000,
asset_id: ""
},
{
address: "ZxC1U6hoCRM9PBSwrBTrWD8XgcHqLNJN9NWqXs9o994eZuHHBvSAyBpQ4TbWSNoabUDPdD8iEM5ZjPoMM7jE48mp2iKcVHLSK",
amount: 1000000000000000,
asset_id: ""
}
],
do_not_split_destinations: false
}
};
const res = await callJsonRpc(jsonObject);
console.log("deploy_asset response: " + JSON.stringify(res, null, 2));
/*
deploy_asset response:
{
"id": 0,
"jsonrpc": "2.0",
"result": {
"new_asset_id": "7d51ecaad2e3458e0d62b146f33079c6ea307841b09a44b777e0c01eb11b98bf",
"tx_id": "73ff52bf4d85153f2b25033dd76e9e92e63214ed983682182e6e2b2ce0ecf46c"
}
}
*/
}
catch (error) {
console.error('Error occurred:', error);
}
}
async function emmit_asset()
{
try {
//Generated Private Key: 0x17a938099954cee510d7fc9eb2366f0762b093d9be547acabf8be85f774ef154
//Generated Address: 0x0886bA9F5b117D2A3C1ce18106F2Ce759f5D34C8
// asset_id 7d51ecaad2e3458e0d62b146f33079c6ea307841b09a44b777e0c01eb11b98bf
//var use_pregenerated_files = false;
const loadedWallet = new ethers.Wallet("0x17a938099954cee510d7fc9eb2366f0762b093d9be547acabf8be85f774ef154");
console.log("Loaded Address:", loadedWallet.address);
console.log("Public key:", loadedWallet.signingKey.compressedPublicKey);
const owner_eth_pub_key = loadedWallet.signingKey.compressedPublicKey.substring(2);
console.log("Generated Public key HEX:", owner_eth_pub_key);
var res_emmit;
//if(!use_pregenerated_files)
//{
const requestDataEmit = {
id: 0,
jsonrpc: "2.0",
method: "emit_asset",
params: {
asset_id: "7d51ecaad2e3458e0d62b146f33079c6ea307841b09a44b777e0c01eb11b98bf",
destinations: [{
address: "ZxC1U6hoCRM9PBSwrBTrWD8XgcHqLNJN9NWqXs9o994eZuHHBvSAyBpQ4TbWSNoabUDPdD8iEM5ZjPoMM7jE48mp2iKcVHLSK",
amount: 100000000000,
asset_id: ""
}],
do_not_split_destinations: false
}
};
res_emmit = await callJsonRpc(requestDataEmit);
fs.writeFileSync('emmit_response.json', JSON.stringify(res_emmit, null, 2));
console.log("emmit_response response: " + JSON.stringify(res_emmit, null, 2));
//}else
//{
// const data = fs.readFileSync('emmit_response.json', 'utf8');
// res_emmit = JSON.parse(data);
//}
var res_decrypt;
//if(!use_pregenerated_files)
//{
const requestDataDecrypt = {
id: 0,
jsonrpc: "2.0",
method: "decrypt_tx_details",
params: {
outputs_addresses: res_emmit.result.data_for_external_signing.outputs_addresses,
tx_blob: res_emmit.result.data_for_external_signing.unsigned_tx,
tx_id: "",
tx_secret_key: res_emmit.result.data_for_external_signing.tx_secret_key
}
};
res_decrypt = await callJsonRpc(requestDataDecrypt, 12111); //request to daemon
fs.writeFileSync('decrypt_response.json', JSON.stringify(res_decrypt, null, 2));
console.log("decrypt_response : " + JSON.stringify(res_decrypt, null, 2));
//}else
//{
// const data = fs.readFileSync('decrypt_response.json', 'utf8');
// res_decrypt = JSON.parse(data);
//}
const bytesToSign = ethers.getBytes('0x' + res_decrypt.result.verified_tx_id);
const signature = loadedWallet.signingKey.sign(bytesToSign).serialized;
const signature_without_0x = signature.substring(2);
console.log("Generated signature: " + signature_without_0x);
// Strip the last byte (recovery parameter) to get 64 bytes
const strippedSignature = signature_without_0x.slice(0, -2); // Remove the last byte (2 hex chars)
const requestSendSigned = {
id: 0,
jsonrpc: "2.0",
method: "send_ext_signed_asset_tx",
params: {
eth_sig: strippedSignature,
expected_tx_id: res_decrypt.result.verified_tx_id,
finalized_tx: res_emmit.result.data_for_external_signing.finalized_tx,
unlock_transfers_on_fail: false,
unsigned_tx: res_emmit.result.data_for_external_signing.unsigned_tx
}
}
const res_sign = await callJsonRpc(requestSendSigned);
fs.writeFileSync('sign_response.json', JSON.stringify(res_sign, null, 2));
console.log("sign_response response: " + JSON.stringify(res_sign, null, 2));
}
catch (error) {
console.error('Error occurred:', error);
}
}
async function main()
{
try {
/*
await deploy_asset();
TODO: wait for 10 confirmations
//wait for 10 confirmations
//asset id 7d51ecaad2e3458e0d62b146f33079c6ea307841b09a44b777e0c01eb11b98bf
*/
await emmit_asset();
} catch (error) {
console.error('Error occurred:', error);
}
}
main();