@@ -140,9 +140,16 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
140140 */
141141 protected claimNetworkAuthority : Token < ClaimNetworkAuthority > | undefined =
142142 undefined ;
143+ /**
144+ * If a node has joined a network then it's `ClaimNetworkAccess` is tracked here
145+ */
143146 protected claimNetworkAccess : Token < ClaimNetworkAccess > | undefined =
144147 undefined ;
145148
149+ /**
150+ * These are the level paths for mapping the ClaimNetworkAccess and ClaimNetworkAuthority claims for each network it has joined.
151+ * Used to look up and switch between networks as needed.
152+ */
146153 protected nodeManagerDbPath : LevelPath = [ this . constructor . name ] ;
147154 protected nodeManagerClaimNetworkAuthorityPath : LevelPath = [
148155 ...this . nodeManagerDbPath ,
@@ -1963,26 +1970,30 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
19631970 }
19641971 const receivedClaim = readStatus . value ;
19651972 // We need to re-construct the token from the message
1966- const signedClaim = claimsUtils . parseSignedClaim (
1967- receivedClaim . signedTokenEncoded ,
1968- ) ;
1973+ const signedClaim =
1974+ claimNetworkAccessUtils . parseSignedClaimNetworkAccess (
1975+ receivedClaim . signedTokenEncoded ,
1976+ ) ;
19691977 const fullySignedToken = Token . fromSigned ( signedClaim ) ;
19701978 // Check that the signatures are correct
1971- const requestingNodePublicKey =
1972- keysUtils . publicKeyFromNodeId ( requestingNodeId ) ;
1973- if (
1974- ! fullySignedToken . verifyWithPublicKey (
1975- this . keyRing . keyPair . publicKey ,
1976- ) ||
1977- ! fullySignedToken . verifyWithPublicKey ( requestingNodePublicKey )
1978- ) {
1979- throw new claimsErrors . ErrorDoublySignedClaimVerificationFailed ( ) ;
1979+ const networkNodeId = nodesUtils . decodeNodeId (
1980+ this . claimNetworkAuthority ! . payload . iss ,
1981+ ) ;
1982+ if ( networkNodeId == null ) {
1983+ utils . never ( 'failed to decode networkNodeId' ) ;
19801984 }
1981- // TODO: verify network and authority claim is correct
1985+
1986+ claimNetworkAccessUtils . verifyClaimNetworkAccess (
1987+ networkNodeId ,
1988+ requestingNodeId ,
1989+ this . claimNetworkAuthority ! . payload . network ,
1990+ fullySignedToken ,
1991+ ) ;
19821992 // Ending the stream
19831993 return fullySignedToken ;
19841994 } ,
19851995 ) ;
1996+ // Prevent async promise handling leak
19861997 void claimP . catch ( ( ) => { } ) ;
19871998 yield {
19881999 signedTokenEncoded : await halfSignedClaimP ,
@@ -2040,105 +2051,6 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
20402051 ) ;
20412052 }
20422053
2043- public async handleVerifyClaimNetwork (
2044- requestingNodeId : NodeId ,
2045- input : AgentRPCRequestParams < AgentClaimMessage > ,
2046- tran ?: DBTransaction ,
2047- ) : Promise < AgentRPCResponseResult < { success : true } > > {
2048- if ( tran == null ) {
2049- return this . db . withTransactionF ( ( tran ) =>
2050- this . handleVerifyClaimNetwork ( requestingNodeId , input , tran ) ,
2051- ) ;
2052- }
2053- const signedClaim = claimsUtils . parseSignedClaim ( input . signedTokenEncoded ) ;
2054- const token = Token . fromSigned ( signedClaim ) ;
2055- claimNetworkAccessUtils . assertClaimNetworkAccess ( token . payload ) ;
2056- // Verify if the token is signed
2057- if (
2058- ! token . verifyWithPublicKey (
2059- keysUtils . publicKeyFromNodeId ( requestingNodeId ) ,
2060- ) ||
2061- ! token . verifyWithPublicKey (
2062- keysUtils . publicKeyFromNodeId (
2063- nodesUtils . decodeNodeId ( token . payload . iss ) ! ,
2064- ) ,
2065- )
2066- ) {
2067- throw new claimsErrors . ErrorDoublySignedClaimVerificationFailed ( ) ;
2068- }
2069- if (
2070- token . payload . network === 'testnet.polykey.com' ||
2071- token . payload . network === 'mainnet.polykey.com'
2072- ) {
2073- return { success : true } ;
2074- }
2075- if ( token . payload . signedClaimNetworkAuthorityEncoded == null ) {
2076- throw new claimsErrors . ErrorDoublySignedClaimVerificationFailed ( ) ;
2077- }
2078- const authorityToken = Token . fromEncoded (
2079- token . payload . signedClaimNetworkAuthorityEncoded ,
2080- ) ;
2081- // Verify if the token is signed
2082- if (
2083- token . payload . iss !== authorityToken . payload . sub ||
2084- ! authorityToken . verifyWithPublicKey (
2085- keysUtils . publicKeyFromNodeId (
2086- nodesUtils . decodeNodeId ( authorityToken . payload . sub ) ! ,
2087- ) ,
2088- ) ||
2089- ! authorityToken . verifyWithPublicKey (
2090- keysUtils . publicKeyFromNodeId (
2091- nodesUtils . decodeNodeId ( authorityToken . payload . iss ) ! ,
2092- ) ,
2093- )
2094- ) {
2095- throw new claimsErrors . ErrorDoublySignedClaimVerificationFailed ( ) ;
2096- }
2097-
2098- let success = false ;
2099- for await ( const [ _ , claim ] of this . sigchain . getSignedClaims ( { } ) ) {
2100- try {
2101- claimNetworkAccessUtils . assertClaimNetworkAccess ( claim . payload ) ;
2102- } catch {
2103- // FIXME: check error type.
2104- continue ;
2105- }
2106- if ( claim . payload . signedClaimNetworkAuthorityEncoded == null ) {
2107- throw new claimsErrors . ErrorDoublySignedClaimVerificationFailed ( ) ;
2108- }
2109- const tokenNetworkAuthority = Token . fromEncoded (
2110- claim . payload . signedClaimNetworkAuthorityEncoded ,
2111- ) ;
2112- try {
2113- claimNetworkAuthorityUtils . assertClaimNetworkAuthority (
2114- tokenNetworkAuthority . payload ,
2115- ) ;
2116- } catch {
2117- // FIXME: check error type.
2118- continue ;
2119- }
2120- // No need to check if local claims are correctly signed by a Network Authority.
2121- if (
2122- authorityToken . verifyWithPublicKey (
2123- keysUtils . publicKeyFromNodeId (
2124- nodesUtils . decodeNodeId ( claim . payload . iss ) ! ,
2125- ) ,
2126- )
2127- ) {
2128- success = true ;
2129- break ;
2130- }
2131- }
2132-
2133- if ( ! success ) {
2134- throw new nodesErrors . ErrorNodeClaimNetworkVerificationFailed ( ) ;
2135- }
2136-
2137- return {
2138- success : true ,
2139- } ;
2140- }
2141-
21422054 /**
21432055 * Adds a node to the node graph. This assumes that you have already authenticated the node
21442056 * Updates the node if the node already exists
0 commit comments