Skip to content

Commit

Permalink
feat: update signature type (#325)
Browse files Browse the repository at this point in the history
Co-authored-by: Oak <[email protected]>
  • Loading branch information
trungnotchung and d-roak authored Jan 12, 2025
1 parent 3473e12 commit c62d3c9
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 53 deletions.
4 changes: 2 additions & 2 deletions packages/blueprints/tests/AddWinsSetWithACL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ describe("AccessControl tests with RevokeWins resolution", () => {
peerId: "peer1",
operation: { type: "grant", value: "peer3" },
dependencies: [],
signature: "",
signature: new Uint8Array(),
timestamp: 0,
},
{
hash: "",
peerId: "peer2",
operation: { type: "revoke", value: "peer3" },
dependencies: [],
signature: "",
signature: new Uint8Array(),
timestamp: 0,
},
];
Expand Down
9 changes: 4 additions & 5 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import { webTransport } from "@libp2p/webtransport";
import { multiaddr } from "@multiformats/multiaddr";
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { type Libp2p, createLibp2p } from "libp2p";
import { toString as uint8ArrayToString } from "uint8arrays";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { Message } from "./proto/drp/network/v1/messages_pb.js";
import { uint8ArrayToStream } from "./stream.js";

Expand Down Expand Up @@ -306,13 +306,12 @@ export class DRPNetworkNode {
this._node?.handle(protocol, handler);
}

async sign(data: string): Promise<string> {
async sign(data: string): Promise<Uint8Array> {
if (!this._privateKey) {
log.error("::signVertexOperation: Private key not found");
return "";
throw new Error("Private key not initialized");
}

const signature = await this._privateKey.sign(uint8ArrayFromString(data));
return uint8ArrayToString(signature, "base64");
return signature;
}
}
2 changes: 1 addition & 1 deletion packages/network/src/proto/drp/network/v1/messages_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions packages/network/src/proto/drp/object/v1/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/network/src/proto/google/protobuf/struct_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,21 @@ export function drpObjectChangesHandler(

export async function signGeneratedVertices(node: DRPNode, vertices: Vertex[]) {
const signPromises = vertices.map(async (vertex) => {
if (vertex.peerId !== node.networkNode.peerId || vertex.signature !== "") {
if (
vertex.peerId !== node.networkNode.peerId ||
vertex.signature.length !== 0
) {
return;
}

await node.signVertex(vertex);
try {
await node.signVertex(vertex);
} catch (error) {
log.error(
"::signGeneratedVertices: Error signing vertex:",
vertex.hash,
error,
);
}
});

await Promise.all(signPromises);
Expand Down Expand Up @@ -253,12 +263,10 @@ export async function verifyIncomingVertices(
}
const acl = drp.acl;
const verificationPromises = vertices.map(async (vertex) => {
if (vertex.signature === "") {
if (vertex.signature.length === 0) {
return null;
}

const signature = uint8ArrayFromString(vertex.signature, "base64");

const publicKey = acl.query_getPeerKey(vertex.peerId);
if (!publicKey) {
return null;
Expand All @@ -279,7 +287,7 @@ export async function verifyIncomingVertices(
const isValid = await crypto.subtle.verify(
{ name: "Ed25519" },
cryptoKey,
signature,
vertex.signature,
data,
);

Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export class DRPNode {

async signVertex(vertex: Vertex) {
if (vertex.peerId !== this.networkNode.peerId) {
log.error("::signVertexOperation: Invalid peer id");
return "";
throw new Error("Invalid peerId");
}

vertex.signature = await this.networkNode.sign(vertex.hash);
}
}
17 changes: 11 additions & 6 deletions packages/node/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ describe("DPRNode with verify and sign signature", () => {
value: "value",
},
dependencies: [],
signature: "",
timestamp: Date.now(),
signature: new Uint8Array(),
},
];
await signGeneratedVertices(drpNode, vertices);
expect(vertices[0].signature).toBe("");
expect(vertices[0].signature.length).toBe(0);
});

test("Node will sign vertex if it is the creator", async () => {
Expand All @@ -53,7 +54,8 @@ describe("DPRNode with verify and sign signature", () => {
value: 1,
},
dependencies: [],
signature: "",
timestamp: Date.now(),
signature: new Uint8Array(),
},
];
await signGeneratedVertices(drpNode, vertices);
Expand All @@ -70,7 +72,8 @@ describe("DPRNode with verify and sign signature", () => {
value: 1,
},
dependencies: [],
signature: "",
timestamp: Date.now(),
signature: new Uint8Array(),
},
];
await signGeneratedVertices(drpNode, vertices);
Expand All @@ -88,7 +91,8 @@ describe("DPRNode with verify and sign signature", () => {
value: 1,
},
dependencies: [],
signature: "",
timestamp: Date.now(),
signature: new Uint8Array(),
},
];

Expand All @@ -108,7 +112,8 @@ describe("DPRNode with verify and sign signature", () => {
value: 1,
},
dependencies: [],
signature: "",
timestamp: Date.now(),
signature: new Uint8Array(),
},
];
const verifiedVertices = await verifyIncomingVertices(drpObject, vertices);
Expand Down
6 changes: 3 additions & 3 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class HashGraph {
},
dependencies: [],
timestamp: -1,
signature: "",
signature: new Uint8Array(),
};
this.vertices.set(HashGraph.rootHash, rootVertex);
this.frontier.push(HashGraph.rootHash);
Expand All @@ -106,7 +106,7 @@ export class HashGraph {
operation: operation ?? { type: OperationType.NOP },
dependencies: deps,
timestamp: currentTimestamp,
signature: "",
signature: new Uint8Array(),
};

this.vertices.set(hash, vertex);
Expand Down Expand Up @@ -150,7 +150,7 @@ export class HashGraph {
deps: Hash[],
peerId: string,
timestamp: number,
signature: string,
signature: Uint8Array,
): Hash {
const hash = computeHash(peerId, operation, deps, timestamp);
if (this.vertices.has(hash)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/object/src/proto/drp/object/v1/object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ message Vertex {
Operation operation = 3;
repeated string dependencies = 4;
int64 timestamp = 5;
string signature = 6;
bytes signature = 6;
}

message DRPObjectBase {
Expand Down
20 changes: 10 additions & 10 deletions packages/object/src/proto/drp/object/v1/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/object/src/proto/google/protobuf/struct_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c62d3c9

Please sign in to comment.