Trying to update to v37.0

This commit is contained in:
Pavel Murygin 2022-05-17 22:33:50 +03:00
parent 2b752492c1
commit c22ac12af2
9 changed files with 3735 additions and 604 deletions

View File

@ -2,4 +2,5 @@ module.exports = {
preset: 'ts-jest', preset: 'ts-jest',
testEnvironment: 'node', testEnvironment: 'node',
testTimeout: 10000, testTimeout: 10000,
testPathIgnorePatterns: ['/node_modules/', 'dist'],
}; };

4225
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,8 +2,12 @@
"name": "@fluencelabs/fluence", "name": "@fluencelabs/fluence",
"version": "0.0.0", "version": "0.0.0",
"description": "TypeScript implementation of Fluence Peer", "description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js", "exports": "./dist/index.js",
"typings": "./dist/index.d.ts", "typings": "./dist/index.d.ts",
"type": "module",
"engines": {
"node": ">=16.0"
},
"scripts": { "scripts": {
"compile-aqua": "npm run compile-aqua:src && npm run compile-aqua:test", "compile-aqua": "npm run compile-aqua:src && npm run compile-aqua:test",
"compile-aqua:src": "aqua -i ./aqua/src/ -o ./src/internal/_aqua", "compile-aqua:src": "aqua -i ./aqua/src/ -o ./src/internal/_aqua",
@ -22,9 +26,13 @@
"copy-marine": "dist/tools/copyMarine.js" "copy-marine": "dist/tools/copyMarine.js"
}, },
"dependencies": { "dependencies": {
"@chainsafe/libp2p-noise": "^4.1.1", "@chainsafe/libp2p-noise": "^6.1.1",
"@fluencelabs/avm": "0.24.2", "@fluencelabs/avm": "0.24.2",
"@fluencelabs/marine-js": "0.2.5", "@fluencelabs/marine-js": "0.2.5",
"@libp2p/mplex": "^1.0.5",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/websockets": "^1.0.8",
"@multiformats/multiaddr": "^10.1.8",
"async": "3.2.3", "async": "3.2.3",
"browser-or-node": "^2.0.0", "browser-or-node": "^2.0.0",
"bs58": "5.0.0", "bs58": "5.0.0",
@ -32,14 +40,9 @@
"cids": "1.1.9", "cids": "1.1.9",
"it-length-prefixed": "5.0.3", "it-length-prefixed": "5.0.3",
"it-pipe": "1.1.0", "it-pipe": "1.1.0",
"libp2p": "0.36.2", "libp2p": "0.37.0",
"libp2p-crypto": "0.21.2", "libp2p-crypto": "0.21.2",
"libp2p-mplex": "0.10.7",
"libp2p-websockets": "0.16.2",
"loglevel": "1.8.0", "loglevel": "1.8.0",
"multiaddr": "10.0.1",
"multiformats": "9.6.5",
"peer-id": "0.16.0",
"platform": "^1.3.6", "platform": "^1.3.6",
"rxjs": "^7.5.5", "rxjs": "^7.5.5",
"ts-pattern": "3.3.3", "ts-pattern": "3.3.3",

View File

@ -1,4 +1,4 @@
import { Multiaddr } from 'multiaddr'; import { Multiaddr } from '@multiformats/multiaddr';
import { nodes } from '../connection'; import { nodes } from '../connection';
import { FluencePeer } from '../../index'; import { FluencePeer } from '../../index';

View File

@ -26,7 +26,7 @@ describe('KeyPair tests', () => {
// assert // assert
const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp'; const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp';
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId); expect(keyPair.libp2pPeerId.toString()).toStrictEqual(expectedPeerId);
}); });
it('create keypair from a seed phrase', async function () { it('create keypair from a seed phrase', async function () {
@ -38,6 +38,6 @@ describe('KeyPair tests', () => {
// assert // assert
const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5'; const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5';
expect(keyPair.Libp2pPeerId.toB58String()).toStrictEqual(expectedPeerId); expect(keyPair.libp2pPeerId.toString()).toStrictEqual(expectedPeerId);
}); });
}); });

View File

@ -177,7 +177,7 @@ const context = (async () => {
const kp = await KeyPair.fromEd25519SK(keyBytes); const kp = await KeyPair.fromEd25519SK(keyBytes);
const res = { const res = {
peerKeyPair: kp, peerKeyPair: kp,
peerId: kp.Libp2pPeerId.toB58String(), peerId: kp.libp2pPeerId.toString(),
}; };
return res; return res;
})(); })();

View File

@ -13,22 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
// @ts-ignore
import Websockets from 'libp2p-websockets'; import { WebSockets } from '@libp2p/websockets';
// @ts-ignore import { Mplex } from '@libp2p/mplex';
import Mplex from 'libp2p-mplex'; import { Libp2p as Lib2p2Peer, createLibp2p } from 'libp2p';
import Lib2p2Peer from 'libp2p';
import { decode, encode } from 'it-length-prefixed'; import { decode, encode } from 'it-length-prefixed';
import { pipe } from 'it-pipe'; import { pipe } from 'it-pipe';
import * as log from 'loglevel';
import { Noise } from '@chainsafe/libp2p-noise'; import { Noise } from '@chainsafe/libp2p-noise';
import { Particle } from './Particle'; import { Particle } from './Particle';
import PeerId from 'peer-id'; import { Multiaddr } from '@multiformats/multiaddr';
import { Multiaddr } from 'multiaddr'; import { PeerId } from '@libp2p/interfaces/peer-id/';
// @ts-ignore import { Connection } from '@libp2p/interfaces/connection/';
import { all as allow_all } from 'libp2p-websockets/src/filters';
import { Connection } from 'libp2p-interfaces/src/topology';
import Buffer from './Buffer'; import Buffer from './Buffer';
import * as log from 'loglevel';
export const PROTOCOL_NAME = '/fluence/particle/2.0.0'; export const PROTOCOL_NAME = '/fluence/particle/2.0.0';
@ -60,25 +57,13 @@ export interface FluenceConnectionOptions {
export class FluenceConnection { export class FluenceConnection {
constructor(private _lib2p2Peer: Lib2p2Peer, private _relayAddress: Multiaddr) {} constructor(private _lib2p2Peer: Lib2p2Peer, private _relayAddress: Multiaddr) {}
private _connection?: Connection;
static async createConnection(options: FluenceConnectionOptions): Promise<FluenceConnection> { static async createConnection(options: FluenceConnectionOptions): Promise<FluenceConnection> {
const transportKey = Websockets.prototype[Symbol.toStringTag]; const lib2p2Peer = await createLibp2p({
const lib2p2Peer = await Lib2p2Peer.create({
peerId: options.peerId, peerId: options.peerId,
modules: { transports: [new WebSockets()],
transport: [Websockets], streamMuxers: [new Mplex()],
streamMuxer: [Mplex], connectionEncryption: [new Noise()],
connEncryption: [new Noise()], connectionManager: {
},
config: {
transport: {
[transportKey]: {
filter: allow_all,
},
},
},
dialer: {
dialTimeout: options?.dialTimeoutMs, dialTimeout: options?.dialTimeoutMs,
}, },
}); });
@ -140,7 +125,7 @@ export class FluenceConnection {
async connect() { async connect() {
await this._lib2p2Peer.start(); await this._lib2p2Peer.start();
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toB58String()); log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId);
try { try {
this._connection = await this._lib2p2Peer.dial(this._relayAddress); this._connection = await this._lib2p2Peer.dial(this._relayAddress);
@ -153,4 +138,6 @@ export class FluenceConnection {
} }
} }
} }
private _connection: Connection | undefined;
} }

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Multiaddr } from 'multiaddr'; import { Multiaddr } from '@multiformats/multiaddr';
import { CallServiceData, CallServiceResult, GenericCallServiceHandler, ResultCodes } from './commonTypes'; import { CallServiceData, CallServiceResult, GenericCallServiceHandler, ResultCodes } from './commonTypes';
import { PeerIdB58 } from './commonTypes'; import { PeerIdB58 } from './commonTypes';
import { FluenceConnection } from './FluenceConnection'; import { FluenceConnection } from './FluenceConnection';
@ -200,7 +200,7 @@ export class FluencePeer {
if (this._connection === undefined || this._relayPeerId === null) { if (this._connection === undefined || this._relayPeerId === null) {
return { return {
isInitialized: true, isInitialized: true,
peerId: this._keyPair.Libp2pPeerId.toB58String(), peerId: this._keyPair.libp2pPeerId.toString(),
isConnected: false, isConnected: false,
relayPeerId: null, relayPeerId: null,
}; };
@ -208,7 +208,7 @@ export class FluencePeer {
return { return {
isInitialized: true, isInitialized: true,
peerId: this._keyPair.Libp2pPeerId.toB58String(), peerId: this._keyPair.libp2pPeerId.toString(),
isConnected: true, isConnected: true,
relayPeerId: this._relayPeerId, relayPeerId: this._relayPeerId,
}; };
@ -225,7 +225,7 @@ export class FluencePeer {
const keyPair = config?.KeyPair ?? (await KeyPair.randomEd25519()); const keyPair = config?.KeyPair ?? (await KeyPair.randomEd25519());
this._keyPair = keyPair; this._keyPair = keyPair;
const peerId = keyPair.Libp2pPeerId.toB58String(); const peerId = keyPair.libp2pPeerId.toString();
if (config?.debug?.printParticleId) { if (config?.debug?.printParticleId) {
this._printParticleId = true; this._printParticleId = true;
@ -262,7 +262,7 @@ export class FluencePeer {
} }
this._connection = await FluenceConnection.createConnection({ this._connection = await FluenceConnection.createConnection({
peerId: this._keyPair.Libp2pPeerId, peerId: this._keyPair.libp2pPeerId,
relayAddress: connectToMultiAddr, relayAddress: connectToMultiAddr,
dialTimeoutMs: config.dialTimeoutMs, dialTimeoutMs: config.dialTimeoutMs,
onIncomingParticle: (p) => this._incomingParticles.next({ particle: p, onStageChange: () => {} }), onIncomingParticle: (p) => this._incomingParticles.next({ particle: p, onStageChange: () => {} }),

View File

@ -14,17 +14,17 @@
* limitations under the License. * limitations under the License.
*/ */
import * as PeerId from 'peer-id'; import { PeerId } from '@libp2p/interfaces/peer-id';
import { peerIdFromBytes } from '@libp2p/peer-id';
import { keys } from 'libp2p-crypto'; import { keys } from 'libp2p-crypto';
type SK = keys.supportedKeys.ed25519.Ed25519PrivateKey;
export class KeyPair { export class KeyPair {
/** constructor(public libp2pPeerId: PeerId, private secretKey: SK) {}
* Key pair in libp2p format. Used for backward compatibility with the current FluencePeer implementation
*/
public Libp2pPeerId: PeerId;
constructor(libp2pPeerId: PeerId) { private static async fromSk(sk: SK) {
this.Libp2pPeerId = libp2pPeerId; const lib2p2Pid = await peerIdFromBytes(sk.bytes);
return new KeyPair(lib2p2Pid, sk);
} }
/** /**
@ -34,9 +34,8 @@ export class KeyPair {
*/ */
static async fromEd25519SK(arr: Uint8Array): Promise<KeyPair> { static async fromEd25519SK(arr: Uint8Array): Promise<KeyPair> {
// generateKeyPairFromSeed takes seed and copies it to private key as is // generateKeyPairFromSeed takes seed and copies it to private key as is
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256); const sk = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes); return await this.fromSk(sk);
return new KeyPair(lib2p2Pid);
} }
/** /**
@ -44,26 +43,26 @@ export class KeyPair {
* @returns - Promise with the created KeyPair * @returns - Promise with the created KeyPair
*/ */
static async randomEd25519(): Promise<KeyPair> { static async randomEd25519(): Promise<KeyPair> {
const lib2p2Pid = await PeerId.create({ keyType: 'Ed25519' }); const sk = await keys.generateKeyPair('Ed25519');
return new KeyPair(lib2p2Pid); return await this.fromSk(sk);
} }
toB58String(): string { toB58String(): string {
return this.Libp2pPeerId.toB58String(); return this.libp2pPeerId.toString();
} }
/** /**
* @returns 32 byte private key * @returns 32 byte private key
*/ */
toEd25519PrivateKey(): Uint8Array { toEd25519PrivateKey(): Uint8Array {
return this.Libp2pPeerId.privKey.marshal().subarray(0, 32); return this.secretKey.bytes;
} }
signBytes(data: Uint8Array): Promise<Uint8Array> { signBytes(data: Uint8Array): Promise<Uint8Array> {
return this.Libp2pPeerId.privKey.sign(data); return this.secretKey.sign(data);
} }
verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> { verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
return this.Libp2pPeerId.privKey.public.verify(data, signature); return this.secretKey.public.verify(data, signature);
} }
} }