Exampls » История » Редакция 3
Редакция 2 (Mariyam Seidaliyeva, 30.04.2024 14:10) → Редакция 3/5 (Mariyam Seidaliyeva, 05.02.2026 12:48)
h1. Примеры h3. Python <pre><code class="python"> <pre> from eth_keys import keys import requests msg = b'{}' pk = keys.PrivateKey(b'\x01' * 32) signature = pk.sign_msg(msg) addr = pk.public_key.to_checksum_address() verify = signature.verify_msg(msg, pk.public_key) re_addr = signature.recover_public_key_from_msg(msg).to_checksum_address() print(str(addr) + ' = ' + str(re_addr) + ' = ' + str(verify)) URL = "https://my.h2k.me/ec/ex" headers = { 'x-app-ec-from': addr, 'x-app-ec-sign-r': hex(signature.r), 'x-app-ec-sign-s': hex(signature.s), 'x-app-ec-sign-v': hex(signature.v), } print(headers) r = requests.get(url=URL, headers=headers) print(r.content) data = r.json() print(data) </pre></code> </pre> h3. NodeJS nodejs + Express express *1. Создание кошелька* В значения переменных создания аккаунта и первой компоненты подписи _'x-app-ec-from'_ можно использовать тестовые значения: <pre><code class="javascript"> <pre> const account Web3 = require("web3"); async function createECESign(req, res, next) { const privateKey = '0xb7d9b51eda54d664366d74c1b1a4ed2ca9e1abed646732bfb83b141f22df3b39' const rFrom = '0xA285990a1Ce696d770d578Cf4473d80e0228DF95' </code> </pre> <pre><code class="javascript"> import Web3 from 'web3'; import axios from 'axios'; // 1. Инициализация req.conf.rFrom = rFrom const msg = req.conf.data ? req.conf.data : '{}' const web3 = new Web3(); // 2. Создание аккаунта (можно заменить значением) const account = web3.eth.accounts.create(); // 3. Тело запроса (ВАЖНО: именно это будет подписано) const body = { slist: { min_signs: "2", "0": { type: "all", email: "e@mail.com" }, "1": { type: "any", email: "e@mail.ru", sms: "+77777777777" }, "2": { type: "all", ecaddress: "0xa75db3c448bb62e208e4babf500925427837e464" } }, network: "5010", info: "Мой первый кошелёк" }; // 4. Строка для подписи const message = JSON.stringify(body); // 5. Подпись const sign = web3.eth.accounts.sign(message, account.privateKey); // 6. Заголовки const headers await web3.eth.accounts.sign(msg, privateKey) req.conf.headers['x-app-ec-from'] = { rFrom 'Content-Type': 'application/json', req.conf.headers['x-app-ec-sign-r'] = sign.r 'Accept': 'application/json', // компоненты подписи req.conf.headers['x-app-ec-sign-s'] = sign.s 'x-app-ec-from': account.address, req.conf.headers['x-app-ec-sign-v'] = sign.v 'x-app-ec-sign-r': sign.r, 'x-app-ec-sign-s': sign.s, 'x-app-ec-sign-v': sign.v next(); }; } // 7. POST-запрос const url = 'https://my.safina.pro/ece/newWallet'; (async () => { router.get('/test', try { async function(req,res, next){ const response headers = await axios.post(url, message, { headers }); console.log('API response:', response.data); } catch (err) { if (err.response) { console.error('API error:', err.response.status, err.response.data); } else { 'accept': 'application/json', console.error('Request error:', err.message); 'content-type': 'application/json' } } })(); </pre> </code> *2. Получение списка кошельков* * В значения переменных создания аккаунта и первой компоненты подписи _'x-app-ec-from'_ можно использовать тестовые значения: <pre><code class="javascript"> const account }; req.conf = '0xb7d9b51eda54d664366d74c1b1a4ed2ca9e1abed646732bfb83b141f22df3b39' const rFrom {} req.conf.baseURL= 'https://my.h2k.me' req.conf.headers = '0xA285990a1Ce696d770d578Cf4473d80e0228DF95' </code> </pre> * GET-запрос подписывается _const message headers req.conf.url = '{}';_ <pre><code class="javascript"> import Web3 from 'web3'; import axios from 'axios'; // 1. Инициализация const web3 '/ece/ex' req.conf.method = new Web3(); // 2. Тестовый аккаунт 'GET' const account body = web3.eth.accounts.create(); // 3. Текст для подписи const message {}; req.conf.data = '{}'; // 4. Подпись const sign = web3.eth.accounts.sign(message, account.privateKey); // 5. Заголовки const headers = { JSON.stringify(body); next() 'Accept': 'application/json', // компоненты подписи }, 'x-app-ec-from': account.address, createECESign, 'x-app-ec-sign-r': sign.r, 'x-app-ec-sign-s': sign.s, 'x-app-ec-sign-v': sign.v }; // 6. GET-запрос const url = 'https://my.safina.pro/ece/wallets'; (async () => { try { async function(req,res){ const response let resp = await axios.get(url, { headers }); console.log('API response:', response.data); } catch (err) { axiosEC(req.conf) if (err.response) { console.error('API error:', err.response.status, err.response.data); let resp_data = resp.data } else { console.error('Request error:', err.message); } res.send(resp_data); } })(); </code></pre> ) </pre>