@@ -2967,11 +2967,146 @@ describe(`${NodeManager.name}`, () => {
29672967 node1 . nodeManager . claimNetwork ( seedNodeId , network ) ,
29682968 ) . rejects . toThrow ( claimsErrors . ErrorEmptyStream ) ;
29692969 } ) ;
2970- // verify the claim exists
2971- test . todo (
2972- 'node should automatically request NetworkAccessClaim if it does not exist' ,
2973- ) ;
2974- // verify the claim is unmodified
2975- test . todo ( 'node should not request new NetworkAccessClaim if it exist' ) ;
2970+ test ( 'node should automatically request a claim if it does not exist' , async ( ) => {
2971+ // Creating network credentials
2972+ const networkKeyPair = keysUtils . generateKeyPair ( ) ;
2973+ const networkNodeId = keysUtils . publicKeyToNodeId (
2974+ networkKeyPair . publicKey ,
2975+ ) ;
2976+ const network = 'public.network.com' ;
2977+
2978+ // Setting up seed nodes claims
2979+ const seedNode = await createPeerNode ( ) ;
2980+ const [ , seedNodeClaimNetworkAuthority ] =
2981+ await seedNode . nodeManager . createClaimNetworkAuthority (
2982+ networkNodeId ,
2983+ network ,
2984+ false ,
2985+ async ( claim ) => {
2986+ claim . signWithPrivateKey ( networkKeyPair . privateKey ) ;
2987+ return claim ;
2988+ } ,
2989+ ) ;
2990+ await seedNode . nodeManager . createSelfSignedClaimNetworkAccess (
2991+ seedNodeClaimNetworkAuthority ,
2992+ ) ;
2993+ const seedNodeId = seedNode . keyRing . getNodeId ( ) ;
2994+
2995+ // Setting up the new node entering the network
2996+ const node1 = await createPeerNode ( ) ;
2997+ // We intentionally do not claim the network manually
2998+ const node1Id = node1 . keyRing . getNodeId ( ) ;
2999+ await allowNodeToJoin ( seedNode . gestaltGraph , node1Id ) ;
3000+ // Connect to the seed node
3001+ await node1 . nodeConnectionManager . createConnection (
3002+ [ seedNodeId ] ,
3003+ localHost ,
3004+ seedNode . nodeConnectionManager . port ,
3005+ ) ;
3006+
3007+ await node1 . nodeManager . syncNodeGraph (
3008+ network ,
3009+ [
3010+ [
3011+ seedNode . keyRing . getNodeId ( ) ,
3012+ [ localHost , seedNode . nodeConnectionManager . port ] ,
3013+ ] ,
3014+ ] ,
3015+ 1000 ,
3016+ true ,
3017+ ) ;
3018+
3019+ // We have now proved that a node can request access to the network from a node with network authority.
3020+ // Now We should be able to connect while authenticated to the seed node.
3021+
3022+ // Re-initiate authentication
3023+ await seedNode . nodeConnectionManager . destroyConnection ( node1Id , true ) ;
3024+ await node1 . nodeConnectionManager . destroyConnection ( seedNodeId , true ) ;
3025+ await node1 . nodeConnectionManager . createConnection (
3026+ [ seedNodeId ] ,
3027+ localHost ,
3028+ seedNode . nodeConnectionManager . port ,
3029+ ) ;
3030+
3031+ const networkAccess =
3032+ await node1 . nodeManager . getClaimNetworkAccess ( network ) ;
3033+ if ( networkAccess == null ) {
3034+ throw new Error ( 'network access claim not found' ) ;
3035+ }
3036+ claimNetworkAccessUtils . verifyClaimNetworkAccess (
3037+ networkNodeId ,
3038+ node1Id ,
3039+ network ,
3040+ networkAccess ,
3041+ ) ;
3042+
3043+ await node1 . nodeManager . withConnF ( seedNodeId , undefined , async ( ) => {
3044+ // Do nothing
3045+ } ) ;
3046+ } ) ;
3047+ test ( 'node should not request new claim if it already exists' , async ( ) => {
3048+ // Creating network credentials
3049+ const networkKeyPair = keysUtils . generateKeyPair ( ) ;
3050+ const networkNodeId = keysUtils . publicKeyToNodeId (
3051+ networkKeyPair . publicKey ,
3052+ ) ;
3053+ const network = 'test.network.com' ;
3054+
3055+ // Setting up seed nodes claims
3056+ const seedNode = await createPeerNode ( ) ;
3057+ const [ , seedNodeClaimNetworkAuthority ] =
3058+ await seedNode . nodeManager . createClaimNetworkAuthority (
3059+ networkNodeId ,
3060+ network ,
3061+ true ,
3062+ async ( claim ) => {
3063+ claim . signWithPrivateKey ( networkKeyPair . privateKey ) ;
3064+ return claim ;
3065+ } ,
3066+ ) ;
3067+ await seedNode . nodeManager . createSelfSignedClaimNetworkAccess (
3068+ seedNodeClaimNetworkAuthority ,
3069+ ) ;
3070+ const seedNodeId = seedNode . keyRing . getNodeId ( ) ;
3071+
3072+ // Setting up the new node entering the network
3073+ const node1 = await createPeerNode ( ) ;
3074+
3075+ const node1Id = node1 . keyRing . getNodeId ( ) ;
3076+ await allowNodeToJoin ( seedNode . gestaltGraph , node1Id ) ;
3077+
3078+ // Connect to the seednode
3079+ await node1 . nodeConnectionManager . createConnection (
3080+ [ seedNodeId ] ,
3081+ localHost ,
3082+ seedNode . nodeConnectionManager . port ,
3083+ ) ;
3084+
3085+ // Create a network access claim
3086+ await node1 . nodeManager . claimNetwork ( seedNodeId , network ) ;
3087+
3088+ // Re-initiate authentication
3089+ await seedNode . nodeConnectionManager . destroyConnection ( node1Id , true ) ;
3090+ await node1 . nodeConnectionManager . destroyConnection ( seedNodeId , true ) ;
3091+ await node1 . nodeConnectionManager . createConnection (
3092+ [ seedNodeId ] ,
3093+ localHost ,
3094+ seedNode . nodeConnectionManager . port ,
3095+ ) ;
3096+
3097+ // Check the claim once we have re-authenticated
3098+ const token1 = await node1 . nodeManager . getClaimNetworkAccess ( network ) ;
3099+ if ( token1 == null ) throw new Error ( 'network access claim not found' ) ;
3100+ const token1Id = token1 . payload . jti ;
3101+
3102+ // Try claiming again
3103+ await expect ( node1 . nodeManager . claimNetwork ( seedNodeId , network ) ) . toReject ( ) ;
3104+
3105+ // The token should not have changed
3106+ const token2 = await node1 . nodeManager . getClaimNetworkAccess ( network ) ;
3107+ if ( token2 == null ) throw new Error ( 'network access claim not found' ) ;
3108+ const token2Id = token2 . payload . jti ;
3109+ expect ( token1Id ) . toBe ( token2Id ) ;
3110+ } ) ;
29763111 } ) ;
29773112} ) ;
0 commit comments