Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 230f47d

Browse files
Akimjustprosh
andauthored
fix(chore): Additional test for particle signature (#356)
* Additional test for particle signature * remove .only * Fix bs58 * Fix bs58 #2 * update test * update test * fix test * remove only * refactor * refactor --------- Co-authored-by: Aleksey Proshutisnkiy <[email protected]> Co-authored-by: Alexey Proshutinskiy <[email protected]>
1 parent c0b73fe commit 230f47d

File tree

5 files changed

+293
-224
lines changed

5 files changed

+293
-224
lines changed

packages/core/js-client/src/connection/RelayConnection.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { Subject } from 'rxjs';
3434
import { throwIfHasNoPeerId } from '../util/libp2pUtils.js';
3535
import { IConnection } from './interfaces.js';
3636
import { IParticle } from '../particle/interfaces.js';
37-
import { Particle, serializeToString, verifySignature } from '../particle/Particle.js';
37+
import { buildParticleMessage, Particle, serializeToString, verifySignature } from '../particle/Particle.js';
3838
import { identifyService } from 'libp2p/identify';
3939
import { pingService } from 'libp2p/ping';
4040
import { unmarshalPublicKey } from '@libp2p/crypto/keys';
@@ -186,7 +186,9 @@ export class RelayConnection implements IConnection {
186186
return;
187187
}
188188

189-
const isVerified = await verifySignature(particle, initPeerId.publicKey);
189+
// TODO: uncomment this after nox rolls out signature verification
190+
// const isVerified = await KeyPair.verifyWithPublicKey(initPeerId.publicKey, buildParticleMessage(particle), particle.signature);
191+
const isVerified = true;
190192
if (isVerified) {
191193
this.particleSource.next(particle);
192194
} else {
Lines changed: 106 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Copyright 2023 Fluence Labs Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,15 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { it, describe, expect } from 'vitest';
17-
import { toUint8Array } from 'js-base64';
18-
import * as bs58 from 'bs58';
19-
import { KeyPair } from '../index.js';
2016

21-
// @ts-ignore
22-
const { decode } = bs58.default;
17+
import bs58 from "bs58";
18+
import { fromUint8Array, toUint8Array } from 'js-base64';
19+
import { it, describe, expect } from "vitest";
20+
import { fromBase64Sk, KeyPair } from '../index.js';
2321

24-
const key = '+cmeYlZKj+MfSa9dpHV+BmLPm6wq4inGlsPlQ1GvtPk=';
22+
import { Particle, serializeToString, buildParticleMessage } from '../../particle/Particle.js';
23+
24+
const key = "+cmeYlZKj+MfSa9dpHV+BmLPm6wq4inGlsPlQ1GvtPk=";
2525
const keyBytes = toUint8Array(key);
2626

2727
const testData = Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 9, 10]);
@@ -34,76 +34,102 @@ const testDataSig = Uint8Array.from([
3434

3535
// signature produced by KeyPair created from some random KeyPair
3636

37-
describe('KeyPair tests', () => {
38-
it('generate keypair from seed', async function () {
39-
// arrange
40-
const random = await KeyPair.randomEd25519();
41-
const privateKey = random.toEd25519PrivateKey();
42-
43-
// act
44-
const keyPair = await KeyPair.fromEd25519SK(privateKey);
45-
const privateKey2 = keyPair.toEd25519PrivateKey();
46-
47-
// assert
48-
expect(privateKey).toStrictEqual(privateKey2);
49-
});
50-
51-
it('create keypair from ed25519 private key', async function () {
52-
// arrange
53-
const rustSK = 'jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH';
54-
const sk = decode(rustSK);
55-
56-
// act
57-
const keyPair = await KeyPair.fromEd25519SK(sk);
58-
59-
// assert
60-
const expectedPeerId = '12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp';
61-
expect(keyPair.getPeerId()).toStrictEqual(expectedPeerId);
62-
});
63-
64-
it('create keypair from a seed phrase', async function () {
65-
// arrange
66-
const seedArray = new Uint8Array(32).fill(1);
67-
68-
// act
69-
const keyPair = await KeyPair.fromEd25519SK(seedArray);
70-
71-
// assert
72-
const expectedPeerId = '12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5';
73-
expect(keyPair.getPeerId()).toStrictEqual(expectedPeerId);
74-
});
75-
76-
it('sign', async function () {
77-
// arrange
78-
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
79-
80-
// act
81-
const res = await keyPair.signBytes(testData);
82-
// assert
83-
expect(new Uint8Array(res)).toStrictEqual(testDataSig);
84-
});
85-
86-
it('verify', async function () {
87-
// arrange
88-
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
89-
90-
// act
91-
const res = await keyPair.verify(testData, testDataSig);
92-
93-
// assert
94-
expect(res).toBe(true);
95-
});
96-
97-
it('sign-verify', async function () {
98-
// arrange
99-
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
100-
101-
// act
102-
const data = new Uint8Array(32).fill(1);
103-
const sig = await keyPair.signBytes(data);
104-
const res = await keyPair.verify(data, sig);
105-
106-
// assert
107-
expect(res).toBe(true);
108-
});
37+
describe("KeyPair tests", () => {
38+
it("generate keypair from seed", async function () {
39+
// arrange
40+
const random = await KeyPair.randomEd25519();
41+
const privateKey = random.toEd25519PrivateKey();
42+
43+
// act
44+
const keyPair = await KeyPair.fromEd25519SK(privateKey);
45+
const privateKey2 = keyPair.toEd25519PrivateKey();
46+
47+
// assert
48+
expect(privateKey).toStrictEqual(privateKey2);
49+
});
50+
51+
it("create keypair from ed25519 private key", async function () {
52+
// arrange
53+
const rustSK = "jDaxLJzYtzgwTMrELJCAqavtmx85ktQNfB2rLcK7MhH";
54+
const sk = bs58.decode(rustSK);
55+
56+
// act
57+
const keyPair = await KeyPair.fromEd25519SK(sk);
58+
59+
// assert
60+
const expectedPeerId =
61+
"12D3KooWH1W3VznVZ87JH4FwABK4mkntcspTVWJDta6c2xg9Pzbp";
62+
63+
expect(keyPair.getPeerId()).toStrictEqual(expectedPeerId);
64+
});
65+
66+
it("create keypair from a seed phrase", async function () {
67+
// arrange
68+
const seedArray = new Uint8Array(32).fill(1);
69+
70+
// act
71+
const keyPair = await KeyPair.fromEd25519SK(seedArray);
72+
73+
// assert
74+
const expectedPeerId =
75+
"12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5";
76+
77+
expect(keyPair.getPeerId()).toStrictEqual(expectedPeerId);
78+
});
79+
80+
it("sign", async function () {
81+
// arrange
82+
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
83+
84+
// act
85+
const res = await keyPair.signBytes(testData);
86+
// assert
87+
expect(new Uint8Array(res)).toStrictEqual(testDataSig);
88+
});
89+
90+
it("verify", async function () {
91+
// arrange
92+
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
93+
94+
// act
95+
const res = await keyPair.verify(testData, testDataSig);
96+
97+
// assert
98+
expect(res).toBe(true);
99+
});
100+
101+
it("sign-verify", async function () {
102+
// arrange
103+
const keyPair = await KeyPair.fromEd25519SK(keyBytes);
104+
105+
// act
106+
const data = new Uint8Array(32).fill(1);
107+
const sig = await keyPair.signBytes(data);
108+
const res = await keyPair.verify(data, sig);
109+
110+
// assert
111+
expect(res).toBe(true);
112+
});
113+
114+
it("validates particle signature checks", async function () {
115+
const keyPair = await fromBase64Sk("7h48PQ/f1rS9TxacmgODxbD42Il9B3KC117jvOPppPE=");
116+
expect(bs58.encode(keyPair.getLibp2pPeerId().toBytes())).toBe("12D3KooWANqfCDrV79MZdMnMqTvDdqSAPSxdgFY1L6DCq2DVGB4D");
117+
const message = toUint8Array(btoa("message"));
118+
const signature = await keyPair.signBytes(message);
119+
120+
const verified = await keyPair.verify(message, signature);
121+
expect(verified).toBe(true);
122+
expect(fromUint8Array(signature)).toBe("sBW7H6/1fwAwF86ldwVm9BDu0YH3w30oFQjTWX0Tiu9yTVZHmxkV2OX4GL5jn0Iz0CrasGcOfozzkZwtJBPMBg==");
123+
124+
const particle = await Particle.createNew("abc", keyPair.getPeerId(), 7000, keyPair, "2883f959-e9e7-4843-8c37-205d393ca372", 1696934545662);
125+
126+
const particle_bytes = buildParticleMessage(particle);
127+
expect(fromUint8Array(particle_bytes)).toBe("Mjg4M2Y5NTktZTllNy00ODQzLThjMzctMjA1ZDM5M2NhMzcy/kguGYsBAABYGwAAYWJj");
128+
129+
const isParticleVerified = await KeyPair.verifyWithPublicKey(keyPair.getPublicKey(), particle_bytes, particle.signature);
130+
131+
expect(isParticleVerified).toBe(true);
132+
133+
expect(fromUint8Array(particle.signature)).toBe("KceXDnOfqe0dOnAxiDsyWBIvUq6WHoT0ge+VMHXOZsjZvCNH7/10oufdlYfcPomfv28On6E87ZhDcHGBZcb7Bw==");
134+
});
109135
});

0 commit comments

Comments
 (0)