Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Jul 22, 2024
1 parent 40cfc83 commit 97a48e6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
31 changes: 31 additions & 0 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,37 @@ class NodeManager {
});
}

public async handleClaimNetworkNode(
requestingNodeId: NodeId,
input: AgentRPCRequestParams<AgentClaimMessage>,
tran?: DBTransaction,
): Promise<AgentRPCResponseResult<AgentClaimMessage>> {
if (tran == null) {
return this.db.withTransactionF((tran) => this.handleClaimNetworkNode(requestingNodeId, input, tran));
}
const signedClaim = claimsUtils.parseSignedClaim(
input.signedTokenEncoded,
);
const token = Token.fromSigned(signedClaim);
// Verify if the token is signed
if (
!token.verifyWithPublicKey(
keysUtils.publicKeyFromNodeId(requestingNodeId),
)
) {
throw new claimsErrors.ErrorSinglySignedClaimVerificationFailed();
}
// If verified, add your own signature to the received claim
token.signWithPrivateKey(this.keyRing.keyPair);
// Return the signed claim
const doublySignedClaim = token.toSigned();
const halfSignedClaimEncoded =
claimsUtils.generateSignedClaim(doublySignedClaim);
return {
signedTokenEncoded: halfSignedClaimEncoded,
};
}

/**
* Adds a node to the node graph. This assumes that you have already authenticated the node
* Updates the node if the node already exists
Expand Down
38 changes: 38 additions & 0 deletions src/nodes/agent/handlers/NodesNetworkClaimSign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {
AgentRPCRequestParams,
AgentRPCResponseResult,
HolePunchSignalMessage,
AddressMessage,
NetworkEntryRequestMessage,
AgentClaimMessage,
} from '../types';
import type NodeManager from '../../../nodes/NodeManager';
import type { JSONValue } from '../../../types';
import { UnaryHandler } from '@matrixai/rpc';
import * as agentErrors from '../errors';
import * as agentUtils from '../utils';

class NodesNetworkClaimVerify extends UnaryHandler<
{
nodeManager: NodeManager;
},
AgentRPCRequestParams<AgentClaimMessage>,
AgentRPCResponseResult<{}>
> {
public handle = async (
input: AgentRPCRequestParams<AgentClaimMessage>,
_cancel,
meta: Record<string, JSONValue> | undefined,
): Promise<AgentRPCResponseResult<{}>> => {
const { nodeManager } = this.container;
// Connections should always be validated
const requestingNodeId = agentUtils.nodeIdFromMeta(meta);
if (requestingNodeId == null) {
throw new agentErrors.ErrorAgentNodeIdMissing();
}
nodeManager.handleClaimNetworkNode(requestingNodeId, input);
return {};
};
}

export default NodesNetworkClaimVerify;
35 changes: 0 additions & 35 deletions src/nodes/agent/handlers/NodesNetworkEntryFinal.ts

This file was deleted.

0 comments on commit 97a48e6

Please sign in to comment.