1- /*
1+ /**
22 * Copyright 2023 Fluence Labs Limited
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
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=" ;
2525const keyBytes = toUint8Array ( key ) ;
2626
2727const 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