Exampls » История » Версия 2
Mariyam Seidaliyeva, 30.04.2024 14:10
| 1 | 1 | Mariyam Seidaliyeva | h1. Примеры |
|---|---|---|---|
| 2 | |||
| 3 | 2 | Mariyam Seidaliyeva | h3. Python |
| 4 | 1 | Mariyam Seidaliyeva | |
| 5 | <pre> |
||
| 6 | from eth_keys import keys |
||
| 7 | import requests |
||
| 8 | msg = b'{}' |
||
| 9 | pk = keys.PrivateKey(b'\x01' * 32) |
||
| 10 | signature = pk.sign_msg(msg) |
||
| 11 | addr = pk.public_key.to_checksum_address() |
||
| 12 | verify = signature.verify_msg(msg, pk.public_key) |
||
| 13 | re_addr = signature.recover_public_key_from_msg(msg).to_checksum_address() |
||
| 14 | print(str(addr) + ' = ' + str(re_addr) + ' = ' + str(verify)) |
||
| 15 | |||
| 16 | URL = "https://my.h2k.me/ec/ex" |
||
| 17 | headers = { |
||
| 18 | 'x-app-ec-from': addr, |
||
| 19 | 'x-app-ec-sign-r': hex(signature.r), |
||
| 20 | 'x-app-ec-sign-s': hex(signature.s), |
||
| 21 | 'x-app-ec-sign-v': hex(signature.v), |
||
| 22 | } |
||
| 23 | print(headers) |
||
| 24 | r = requests.get(url=URL, headers=headers) |
||
| 25 | print(r.content) |
||
| 26 | data = r.json() |
||
| 27 | print(data) |
||
| 28 | </pre> |
||
| 29 | |||
| 30 | h3. nodejs + express |
||
| 31 | |||
| 32 | <pre> |
||
| 33 | const Web3 = require("web3"); |
||
| 34 | |||
| 35 | async function createECESign(req, res, next) { |
||
| 36 | const privateKey = '0xb7d9b51eda54d664366d74c1b1a4ed2ca9e1abed646732bfb83b141f22df3b39' |
||
| 37 | const rFrom = '0xA285990a1Ce696d770d578Cf4473d80e0228DF95' |
||
| 38 | req.conf.rFrom = rFrom |
||
| 39 | const msg = req.conf.data ? req.conf.data : '{}' |
||
| 40 | |||
| 41 | const web3 = new Web3(); |
||
| 42 | const sign = await web3.eth.accounts.sign(msg, privateKey) |
||
| 43 | |||
| 44 | req.conf.headers['x-app-ec-from'] = rFrom |
||
| 45 | req.conf.headers['x-app-ec-sign-r'] = sign.r |
||
| 46 | req.conf.headers['x-app-ec-sign-s'] = sign.s |
||
| 47 | req.conf.headers['x-app-ec-sign-v'] = sign.v |
||
| 48 | next(); |
||
| 49 | } |
||
| 50 | |||
| 51 | router.get('/test', |
||
| 52 | async function(req,res, next){ |
||
| 53 | const headers = { |
||
| 54 | 'accept': 'application/json', |
||
| 55 | 'content-type': 'application/json' |
||
| 56 | }; |
||
| 57 | req.conf = {} |
||
| 58 | req.conf.baseURL= 'https://my.h2k.me' |
||
| 59 | req.conf.headers = headers |
||
| 60 | req.conf.url = '/ece/ex' |
||
| 61 | req.conf.method = 'GET' |
||
| 62 | const body = {}; |
||
| 63 | req.conf.data = JSON.stringify(body); |
||
| 64 | next() |
||
| 65 | }, |
||
| 66 | createECESign, |
||
| 67 | async function(req,res){ |
||
| 68 | let resp = await axiosEC(req.conf) |
||
| 69 | let resp_data = resp.data |
||
| 70 | res.send(resp_data); |
||
| 71 | } |
||
| 72 | ) |
||
| 73 | </pre> |