-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (89 loc) · 3.92 KB
/
index.js
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
const DEFAULT_CHAIN = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906';
const DEFAULT_API = 'https://ridlapi.get-scatter.com';
// tiny sha256 -- https://geraintluff.github.io/sha256/
const sha256 = function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i};
const GET_GEN = host => (route) => fetch(`${host}/v1/${route}`).then(x => x.json());
const POST_GEN = host => (route, data) => fetch(`${host}/v1/${route}`, { method:"POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body:JSON.stringify(data) }).then(x => x.json());
const RidlJS = (signer, api_host = DEFAULT_API) => {
if(!signer) throw 'You must pass a signer into RidlJS';
const GET = GET_GEN(api_host);
const POST = POST_GEN(api_host);
const getBlockNum = chain => GET(`/block_num/${chain}`);
return {
validName(username){ return /^(?:(?=.{3,56}$)[a-zA-Z0-9]{1}(?:(?!-{2,}))[a-zA-Z0-9-]*(?:(?<!-))$)/.test(username); },
async chains(){
return GET(`chains`);
},
async hosts(chain){
return GET(`hosts/${chain}`);
},
async getTokenContracts(){
return GET(`token-contracts`);
},
async getLogicContract(chain){
return GET(`logic-contract/${chain}`);
},
async addHost(chain_id, host, options = {}){
return POST(`hosts`, {chain_id, host, options});
},
async findIdentity(username){
return GET(`identity/${username}`);
},
async findReputation(entity){
return GET(`reputation/${entity}`);
},
async getIdentityPaymentMethods(){
return GET(`pay/methods/id`);
},
async getUsagePaymentMethods(){
return GET(`pay/methods/actions`);
},
async startIdentityPayment(username, email, token){
return POST('pay/start', {
username,
email,
token,
type:0,
});
},
async startUsagePayment(username, email, token){
return POST('pay/start', {
username,
email,
token,
type:1,
});
},
async finishPayment(payment_id, txid, block_num){
return POST('pay/finish', {
payment_id,
txid,
block_num,
});
},
async getPaymentStatus(payment_id){
return GET(`pay/status/${payment_id}`);
},
async identify(username, key, chain = DEFAULT_CHAIN){
return POST(`send/identify`, {chain, username, key});
},
async changekey(identity_id, key, chain, block_num = null, sig = null){
if(block_num === null) {
block_num = await getBlockNum(chain);
if(!block_num) return {error:'Could not get block num from server.'};
sig = await signer(sha256(`${identity_id}:${block_num}:${key}`));
}
return POST(`send/changekey`, {chain, identity_id, key, block_num, sig});
},
async repute(identity_id, entity, fragments, tokens, chain, details = '', block_num = null, sig = null){
if(block_num === null) {
block_num = await getBlockNum(chain);
if(!block_num) return {error:'Could not get block num from server.'};
sig = await signer(sha256(`${identity_id}:${entity}:${block_num}:${fragments.map(x => `${x.type}${x.value}`).join('')}:${tokens}`));
}
return POST(`send/repute`, {chain, identity_id, entity, fragments, tokens, block_num, sig, details});
},
}
};
if(typeof module !== 'undefined') module.exports = RidlJS;
if(typeof window !== 'undefined') window.RidlJS = RidlJS;