-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpin.test.js
More file actions
112 lines (102 loc) · 2.95 KB
/
pin.test.js
File metadata and controls
112 lines (102 loc) · 2.95 KB
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
const Crypto = require('node:crypto');
const padId = Crypto.randomBytes(16).toString('hex');
const driveId = Crypto.randomBytes(16).toString('hex');
const blobId = Crypto.randomBytes(24).toString('hex');
const hk = '0123456789abcdef';
const {
connectUser,
createUserRpc,
hashChannelList,
getRandomKeys,
getPinPath
} = require('./common/utils.js');
const keys = getRandomKeys();
const log = (require.main === module) ? console.log : function () {};
log('Pin log path', getPinPath(keys.edPublic));
const channels = [
padId,
blobId,
driveId+'#drive'
];
const checkHash = hashChannelList(channels.sort());
const channels2 = [
padId,
driveId+'#drive'
];
const checkHash2 = hashChannelList(channels2.sort());
const initPin = args => {
return new Promise((resolve, reject) => {
const { network, rpc } = args;
rpc.send('PIN', channels, err => {
if (err) { return reject(err); }
resolve({ network, rpc });
});
});
};
const checkPin = args => {
return new Promise((resolve, reject) => {
const { network, rpc } = args;
rpc.send('GET_HASH', keys.edPublic, (e, hash) => {
if (!(hash && hash[0])) {
return reject('NO_HASH_RETURNED');
}
if (e) { return reject(e); }
let h = hash[0];
if (h !== checkHash) { return reject('WRONG_HASH_PIN'); }
resolve({ network, rpc });
});
});
};
const initUnpin = args => {
return new Promise((resolve, reject) => {
const { network, rpc } = args;
rpc.send('UNPIN', [blobId], err => {
if (err) { return reject(err); }
resolve({ network, rpc });
});
});
};
const checkUnpin = args => {
return new Promise((resolve, reject) => {
const { network, rpc } = args;
rpc.send('GET_HASH', keys.edPublic, (e, hash) => {
if (!(hash && hash[0])) {
return reject('NO_HASH_RETURNED_AFTER_UNPIN');
}
if (e) { return reject(e); }
let h = hash[0];
if (h !== checkHash2) { return reject('WRONG_HASH_UNPIN'); }
resolve({ network, rpc });
});
});
};
const initUser = () => {
return new Promise((resolve, reject) => {
connectUser(0)
.then(network => {
network.historyKeeper = hk;
return createUserRpc({ network, keys });
})
.then(initPin)
.then(checkPin)
.then(initUnpin)
.then(checkUnpin)
.then(() => {
resolve();
}).catch(e => {
console.error(e);
reject(e);
});
});
};
initUser()
.then(() => {
console.log('PIN: success');
if (require.main === module) { process.exit(0); }
global?.onTestEnd?.(true);
}).catch(e => {
console.log('PIN: failure');
console.log(e);
if (require.main === module) { process.exit(1); }
global?.onTestEnd?.(false);
});