2021-01-19 15:47:49 +03:00
|
|
|
import { encode } from 'bs58';
|
2021-02-14 00:35:02 +03:00
|
|
|
import { certificateFromString, certificateToString, issue } from '../../internal/trust/certificate';
|
|
|
|
import { TrustGraph } from '../../internal/trust/trust_graph';
|
|
|
|
import { nodeRootCert } from '../../internal/trust/misc';
|
|
|
|
import { generatePeerId, peerIdToSeed, seedToPeerId } from '../../internal/peerIdUtils';
|
|
|
|
import { FluenceClientImpl } from '../../internal/FluenceClientImpl';
|
|
|
|
import { createConnectedClient, createLocalClient } from '../util';
|
2021-01-19 15:47:49 +03:00
|
|
|
import log from 'loglevel';
|
2021-02-14 00:35:02 +03:00
|
|
|
import { createClient } from '../../api';
|
2021-01-19 15:47:49 +03:00
|
|
|
import Multiaddr from 'multiaddr';
|
2021-01-29 16:48:27 +03:00
|
|
|
|
|
|
|
const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
|
|
|
|
const devNodePeerId = '12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
describe('Typescript usage suite', () => {
|
|
|
|
it('should create private key from seed and back', async function () {
|
|
|
|
// prettier-ignore
|
|
|
|
let seed = [46, 188, 245, 171, 145, 73, 40, 24, 52, 233, 215, 163, 54, 26, 31, 221, 159, 179, 126, 106, 27, 199, 189, 194, 80, 133, 235, 42, 42, 247, 80, 201];
|
|
|
|
let seedStr = encode(seed);
|
|
|
|
log.trace('SEED STR: ' + seedStr);
|
|
|
|
let pid = await seedToPeerId(seedStr);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(peerIdToSeed(pid)).toEqual(seedStr);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should serialize and deserialize certificate correctly', async function () {
|
|
|
|
let cert = `11
|
|
|
|
1111
|
|
|
|
5566Dn4ZXXbBK5LJdUsE7L3pG9qdAzdPY47adjzkhEx9
|
|
|
|
3HNXpW2cLdqXzf4jz5EhsGEBFkWzuVdBCyxzJUZu2WPVU7kpzPjatcqvdJMjTtcycVAdaV5qh2fCGphSmw8UMBkr
|
|
|
|
158981172690500
|
|
|
|
1589974723504
|
|
|
|
2EvoZAZaGjKWFVdr36F1jphQ5cW7eK3yM16mqEHwQyr7
|
|
|
|
4UAJQWzB3nTchBtwARHAhsn7wjdYtqUHojps9xV6JkuLENV8KRiWM3BhQByx5KijumkaNjr7MhHjouLawmiN1A4d
|
|
|
|
1590061123504
|
|
|
|
1589974723504`;
|
|
|
|
|
|
|
|
let deser = await certificateFromString(cert);
|
|
|
|
let ser = certificateToString(deser);
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(ser).toEqual(cert);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.skip('should perform tests on certs', async function () {
|
|
|
|
await testCerts();
|
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
describe('should make connection to network', function () {
|
2021-01-29 16:48:27 +03:00
|
|
|
const testProcedure = async (client: FluenceClientImpl) => {
|
2021-01-19 15:47:49 +03:00
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'test', (args, _) => {
|
|
|
|
resolve(args);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client.relayPeerId}" ("op" "identity") [])
|
|
|
|
(call "${client.selfPeerId}" ("test" "test") [hello])
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('hello', 'world');
|
|
|
|
|
|
|
|
await client.sendScript(script, data);
|
|
|
|
|
|
|
|
const res = await resMakingPromise;
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
it('address as string', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('address as multiaddr', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = new Multiaddr(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('address as node', async function () {
|
|
|
|
// arrange
|
|
|
|
const addr = {
|
2021-01-29 16:48:27 +03:00
|
|
|
multiaddr: devNodeAddress,
|
|
|
|
peerId: devNodePeerId,
|
2021-01-19 15:47:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('peerid as peer id', async function () {
|
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
const pid = await generatePeerId();
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr, pid)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
it('peerid as seed', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const addr = devNodeAddress;
|
2021-01-19 15:47:49 +03:00
|
|
|
const pid = peerIdToSeed(await generatePeerId());
|
|
|
|
|
|
|
|
// act
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = (await createClient(addr, pid)) as FluenceClientImpl;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await testProcedure(client);
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['world']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('should make a call through the network', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-02-14 00:35:02 +03:00
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
client.registerCallback('test', 'test', (args, _) => {
|
|
|
|
log.trace('should make a call through the network, called "test" "test" with args', args);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'reverse_args', (args, _) => {
|
|
|
|
resolve([...args].reverse());
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client.relayPeerId}" ("op" "identity") [])
|
|
|
|
(seq
|
|
|
|
(call "${client.selfPeerId}" ("test" "test") [a b c d] result)
|
|
|
|
(call "${client.selfPeerId}" ("test" "reverse_args") [a b c d])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('a', 'some a');
|
|
|
|
data.set('b', 'some b');
|
|
|
|
data.set('c', 'some c');
|
|
|
|
data.set('d', 'some d');
|
|
|
|
|
|
|
|
await client.sendScript(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some d', 'some c', 'some b', 'some a']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('fireAndForget should work', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
2021-01-29 16:48:27 +03:00
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client.registerCallback('test', 'reverse_args', (args, _) => {
|
|
|
|
resolve([...args].reverse());
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(call "${client.selfPeerId}" ("test" "reverse_args") [a b c d])
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('a', 'some a');
|
|
|
|
data.set('b', 'some b');
|
|
|
|
data.set('c', 'some c');
|
|
|
|
data.set('d', 'some d');
|
|
|
|
|
|
|
|
await client.fireAndForget(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
const res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some d', 'some c', 'some b', 'some a']);
|
2021-01-29 16:48:27 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('fetch should work', async function () {
|
2021-01-29 16:48:27 +03:00
|
|
|
// arrange
|
|
|
|
const client = await createConnectedClient(devNodeAddress);
|
|
|
|
|
2021-01-19 15:47:49 +03:00
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(call "${client.relayPeerId}" ("op" "identify") [] result)
|
|
|
|
`;
|
|
|
|
const data = new Map();
|
|
|
|
data.set('__relay', client.relayPeerId);
|
|
|
|
|
|
|
|
const [res] = await client.fetch(script, ['result'], data);
|
|
|
|
|
|
|
|
// assert
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res.external_addresses).not.toBeUndefined;
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('two clients should work inside the same time browser', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
|
|
|
const pid1 = await generatePeerId();
|
2021-01-29 16:48:27 +03:00
|
|
|
const client1 = new FluenceClientImpl(pid1);
|
|
|
|
await client1.connect(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
const pid2 = await generatePeerId();
|
2021-01-29 16:48:27 +03:00
|
|
|
const client2 = new FluenceClientImpl(pid2);
|
|
|
|
await client2.connect(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
let resMakingPromise = new Promise((resolve) => {
|
|
|
|
client2.registerCallback('test', 'test', (args, _) => {
|
|
|
|
resolve([...args]);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let script = `
|
|
|
|
(seq
|
|
|
|
(call "${client1.relayPeerId}" ("op" "identity") [])
|
|
|
|
(call "${pid2.toB58String()}" ("test" "test") [a b c d])
|
|
|
|
)
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('a', 'some a');
|
|
|
|
data.set('b', 'some b');
|
|
|
|
data.set('c', 'some c');
|
|
|
|
data.set('d', 'some d');
|
|
|
|
|
|
|
|
await client1.sendScript(script, data);
|
|
|
|
|
|
|
|
let res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
|
2021-01-19 15:47:49 +03:00
|
|
|
});
|
|
|
|
|
2021-02-14 00:35:02 +03:00
|
|
|
it('event registration should work', async function () {
|
2021-01-19 15:47:49 +03:00
|
|
|
// arrange
|
|
|
|
const pid1 = await generatePeerId();
|
2021-01-29 16:48:27 +03:00
|
|
|
const client1 = new FluenceClientImpl(pid1);
|
|
|
|
await client1.connect(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
const pid2 = await generatePeerId();
|
2021-01-29 16:48:27 +03:00
|
|
|
const client2 = new FluenceClientImpl(pid2);
|
|
|
|
await client2.connect(devNodeAddress);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
client2.registerEvent('event_stream', 'test');
|
|
|
|
const resMakingPromise = new Promise((resolve) => {
|
|
|
|
client2.subscribe('event_stream', resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
// act
|
|
|
|
let script = `
|
|
|
|
(call "${pid2.toB58String()}" ("event_stream" "test") [hello])
|
|
|
|
`;
|
|
|
|
|
|
|
|
let data: Map<string, any> = new Map();
|
|
|
|
data.set('hello', 'world');
|
|
|
|
|
|
|
|
await client1.fireAndForget(script, data);
|
|
|
|
|
|
|
|
// assert
|
|
|
|
let res = await resMakingPromise;
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(res).toEqual({
|
2021-01-19 15:47:49 +03:00
|
|
|
type: 'test',
|
|
|
|
args: ['world'],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
export async function testCerts() {
|
|
|
|
const key1 = await generatePeerId();
|
|
|
|
const key2 = await generatePeerId();
|
|
|
|
|
|
|
|
// connect to two different nodes
|
2021-01-29 16:48:27 +03:00
|
|
|
const cl1 = new FluenceClientImpl(key1);
|
|
|
|
const cl2 = new FluenceClientImpl(key2);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
await cl1.connect('/dns4/134.209.186.43/tcp/9003/ws/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb');
|
|
|
|
await cl2.connect('/ip4/134.209.186.43/tcp/9002/ws/p2p/12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er');
|
|
|
|
|
|
|
|
let trustGraph1 = new TrustGraph(/* cl1 */);
|
|
|
|
let trustGraph2 = new TrustGraph(/* cl2 */);
|
|
|
|
|
|
|
|
let issuedAt = new Date();
|
|
|
|
let expiresAt = new Date();
|
|
|
|
// certificate expires after one day
|
|
|
|
expiresAt.setDate(new Date().getDate() + 1);
|
|
|
|
|
|
|
|
// create root certificate for key1 and extend it with key2
|
|
|
|
let rootCert = await nodeRootCert(key1);
|
|
|
|
let extended = await issue(key1, key2, rootCert, expiresAt.getTime(), issuedAt.getTime());
|
|
|
|
|
|
|
|
// publish certificates to Fluence network
|
|
|
|
await trustGraph1.publishCertificates(key2.toB58String(), [extended]);
|
|
|
|
|
|
|
|
// get certificates from network
|
|
|
|
let certs = await trustGraph2.getCertificates(key2.toB58String());
|
|
|
|
|
|
|
|
// root certificate could be different because nodes save trusts with bigger `expiresAt` date and less `issuedAt` date
|
2021-02-14 00:35:02 +03:00
|
|
|
expect(certs[0].chain[1].issuedFor.toB58String()).toEqual(extended.chain[1].issuedFor.toB58String());
|
|
|
|
expect(certs[0].chain[1].signature).toEqual(extended.chain[1].signature);
|
|
|
|
expect(certs[0].chain[1].expiresAt).toEqual(extended.chain[1].expiresAt);
|
|
|
|
expect(certs[0].chain[1].issuedAt).toEqual(extended.chain[1].issuedAt);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
await cl1.disconnect();
|
|
|
|
await cl2.disconnect();
|
|
|
|
}
|