-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.js
182 lines (151 loc) · 5.84 KB
/
example.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Latch NodeJS SDK Example
* Copyright (C) 2024 Telefonica Innovación Digital
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// To run an example just fill in the value of the constants and run the example.
const latch = require('../index.js');
const util = require('util');
// Mandatory
const MY_APPID = "<MY_APPID>";
const MY_SECRETKEY = "<MY_SECRETKEY>";
// Mandatory depending on the method
const MY_ACCOUNTID = "<MY_ACCOUNTID>"
const MY_ACCOUNT_NAME = "<MY_ACCOUNT_NAME>"
const PAIRING_CODE = "<PAIRING_CODE>"
const OPERATIONID = "<OPERATIONID>"
// Mandatory for web3
const WEB3WALLET = "<WEB3WALLET>"
const WEB3SIGNATURE = "<WEB3SIGNATURE>"
latch.init({ appId: MY_APPID, secretKey: MY_SECRETKEY });
// USING THE SDK IN NODEJS
// PAIR WITH ID (FOR TESTING)
function example_pair_with_id() {
let response = latch.pairWithId(MY_ACCOUNT_NAME, function(err, result) {
if (err) {
console.log(util.inspect(err, { showHidden: true, depth: null, colors:true }));
} else {
console.log(util.inspect(result, { showHidden: true, depth: null, colors: true }));
}
});
}
// PAIR WITH PAIRING CODE
function example_pair_with_paring_code() {
let response = latch.pair(PAIRING_CODE, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// USING THE SDK IN NODEJS FOR WEB3 SERVICES
// PAIR WITH ID (WEB3)
function example_pair_with_id_web3() {
let response = latch.pairWithId(MY_ACCOUNT_NAME, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
}, WEB3WALLET, WEB3SIGNATURE);
}
// PAIR WITH PAIRING CODE (WEB3)
function example_pair_with_paring_code_web3() {
let response = latch.pair(PAIRING_CODE, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
}, WEB3WALLET, WEB3SIGNATURE);
}
// USING THE SDK IN NODEJS FOR ALL (WEB3 AND NOT WEB3)
// GET STATUS
function example_get_status() {
let response = latch.status(MY_ACCOUNTID, null, null, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
function example_get_status_operation() {
operationStatus = latch.operationStatus(MY_ACCOUNTID, OPERATIONID, null, null, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// LOCK STATUS
function example_lock() {
let response = latch.lock(MY_ACCOUNTID, null, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// UNLOCK STATUS
function example_unlock() {
let response = latch.unlock(MY_ACCOUNTID, null, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// LOCK FOR OPERATION
function example_lock_for_operation() {
let response = latch.lock(MY_ACCOUNTID, OPERATIONID, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// UNLOCK FOR OPERATION
function example_unlock_for_operation() {
let response = latch.unlock(MY_ACCOUNTID, OPERATIONID, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// UNPAIR
function example_unpair() {
let response = latch.unpair(MY_ACCOUNTID, function (err, result) {
if (err) {
console.log(util.inspect(err, {showHidden: true, depth: null, colors: true}));
} else {
console.log(util.inspect(result, {showHidden: true, depth: null, colors: true}));
}
});
}
// TO RUN EXAMPLE
//example_pair_with_id_web3()
//example_get_status()
//example_get_status_operation()
//example_lock()
//example_lock_for_operation()
//example_unlock()
//example_unlock_for_operation()
//example_unpair()