-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.ts
79 lines (71 loc) · 1.94 KB
/
proxy.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
import axios, { AxiosRequestConfig } from "axios";
import Web3 = require("web3");
const web3 = new Web3();
interface Config {
network: string;
contractAddress: string;
apiKey: string;
}
const conf: Config = {
network: "https://api-ropsten.etherscan.io/",
contractAddress: "0xe7cbb89e1649047035833dee5426662a4c0c363b",
apiKey: "UK1C9AYRMU7BG3ZA2F87MZZ4P3F84VKEXG"
};
const axiosConfig: AxiosRequestConfig = {
baseURL: conf.network,
params: {
module: "proxy",
action: "eth_call",
to: conf.contractAddress,
apikey: conf.apiKey
},
timeout: 10000
};
const target = axios.create(axiosConfig);
function encodingFunc(funcName: string, hashedCertificationId: string): string {
return web3.eth.abi.encodeFunctionCall(
{
name: funcName,
type: "function",
inputs: [{ name: "hashedCertificationId", type: "uint256" }]
},
[hashedCertificationId]
);
}
interface Certification {
subject: string;
firstName: string;
lastName: string;
issueDate: string;
expireDate: string;
additionalData: any;
}
export const getCertification: (
hashedCertificationId: string
) => Promise<Certification> = async (hashedCertificationId: string) => {
console.log(`Get certification by ${hashedCertificationId}`);
const res = await target
.get("/api", {
params: {
data: encodingFunc("certifications", hashedCertificationId)
}
});
const cert = web3.eth.abi.decodeParameters([
{ name: "subject", type: "string" },
{ name: "firstName", type: "string" },
{ name: "lastName", type: "string" },
{ name: "issueDate", type: "uint64" },
{ name: "expireDate", type: "uint64" },
{ name: "additionalData", type: "string" }
], res.data.result);
let { subject, firstName, lastName, issueDate, expireDate, additionalData } = cert;
additionalData = JSON.parse(additionalData);
return {
subject,
firstName,
lastName,
issueDate,
expireDate,
additionalData
};
};