-
Notifications
You must be signed in to change notification settings - Fork 0
/
novio-sdk.js
86 lines (75 loc) · 2.56 KB
/
novio-sdk.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
'use strict';
class Novio {
constructor() {
this.Connected = new Promise((resolve) => {
document.addEventListener('onNovioConnected', function (event) {
resolve();
});
});
this.Disconnected = new Promise((resolve) => {
document.addEventListener('onNovioDisconnected', function (event) {
resolve();
});
});
};
transferTo(toAddress, amount, attrs) {
return this.sendTransactionRequest('transferTo', [toAddress, amount, attrs]);
}
registerName(name, attrs) {
return this.sendTransactionRequest('registerName', [name, attrs]);
}
transferName(name, recipient, attrs) {
return this.sendTransactionRequest('transferName', [name, recipient, attrs]);
}
deleteName(name, attrs) {
return this.sendTransactionRequest('deleteName', [name, attrs]);
}
subscribe(topic, duration, identifier, meta, attrs) {
return this.sendTransactionRequest('subscribe', [topic, duration, identifier, meta, attrs]);
}
unsubscribe(topic, identifier, attrs) {
return this.sendTransactionRequest('unsubscribe', [topic, identifier, attrs]);
}
sign(message) {
return this.sendSignatureRequest(message);
}
sendTransactionRequest(type, data) {
const txRequestEvent = new CustomEvent("onNovioTxRequest", {
bubbles: true,
cancelable: false,
detail: {
type: type,
data: data
},
});
document.dispatchEvent(txRequestEvent);
return new Promise((resolve, reject) => {
window.addEventListener("onNovioTxResponse", (event) => {
if (event.detail.data.error) {
reject(event.detail.data.error);
} else {
resolve(event.detail);
}
}, false);
})
}
sendSignatureRequest(message) {
const signRequestEvent = new CustomEvent("onNovioSignRequest", {
bubbles: true,
cancelable: false,
detail: {
data: message
},
});
document.dispatchEvent(signRequestEvent);
return new Promise((resolve, reject) => {
window.addEventListener("onNovioSignResponse", (event) => {
if (event.detail.data.error) {
reject(event.detail.data.error);
} else {
resolve(event.detail);
}
}, false);
})
}
}