summaryrefslogtreecommitdiff
path: root/src/lib/pgp/sign.test.ts
blob: 1f9c4dbec55a5b331b282de2123d0bb0904ecfbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
  assert,
  assertAlmostEquals,
  assertArrayIncludes,
  assertEquals,
  assertExists,
} from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import { createMessage, enums, readSignature, sign } from "openpgp";
import { Signature } from "./sign.ts";
import { get, instanciate } from "../../utils/anonymous.ts";
import { bufferToBase } from "../../utils/bases.ts";
import { generateKeyPair } from "../../../tests/fixtures/setup.ts";

describe("Signature wrapper", () => {
  const now = new Date();
  const aliceKeyPair = generateKeyPair("Alice");
  const signature = Promise.all([
    aliceKeyPair.then(get("privateKey")),
    createMessage({ text: "Hello world" }),
  ]).then(([privateKey, message]) =>
    sign({
      message,
      signingKeys: privateKey,
      detached: true,
      format: "object",
    })
  ).then((x) => readSignature({ armoredSignature: x.armor() })).then(
    instanciate(Signature),
  );

  describe("Single signer", () => {
    it("signingKeyIDs", async () => {
      const { publicKey } = await aliceKeyPair;
      const sig = await signature;

      assertEquals(sig.signingKeyIDs.length, 1);
      assert(sig.signingKeyIDs[0].equals(publicKey.getKeyID()));
    });

    it("getPackets", async () => {
      const sig = await signature;

      assertEquals(sig.getPackets().length, 1);
      assertEquals(sig.getPackets(sig.signingKeyIDs[0]).length, 1);
    });

    describe("Packet wrapper", () => {
      const packet = signature.then((x) => x.getPackets()[0]);

      it("created", async () => {
        const p = await packet;

        assertExists(p.created);
        assertAlmostEquals(p.created.getTime(), now.getTime());
      });

      it("issuerKeyID and issuerFingerprint", async () => {
        const { privateKey } = await aliceKeyPair;
        const p = await packet;

        assertEquals(p.issuerKeyID, privateKey.getKeyID());
        assertExists(p.issuerFingerprint);
        assertEquals(
          bufferToBase(p.issuerFingerprint, 16),
          privateKey.getFingerprint(),
        );
      });

      it("signatureType", async () => {
        const p = await packet;

        assertEquals(p.signatureType, enums.signature.text);
      });
    });
  });

  const bobKeyPair = generateKeyPair("Bob");
  const multiSignature = Promise.all([
    Promise.all([
      aliceKeyPair.then(get("privateKey")),
      bobKeyPair.then(get("privateKey")),
    ]),
    createMessage({ text: "Hello world" }),
  ]).then(([signingKeys, message]) =>
    sign({
      message,
      signingKeys,
      detached: true,
      format: "object",
    })
  ).then((x) => readSignature({ armoredSignature: x.armor() })).then(
    instanciate(Signature),
  );

  describe("with multiple signers", () => {
    it("signingKeyIDs", async () => {
      const { publicKey: alice } = await aliceKeyPair;
      const { publicKey: bob } = await bobKeyPair;
      const sig = await multiSignature;

      assertEquals(sig.signingKeyIDs.length, 2);
      assertArrayIncludes(sig.signingKeyIDs, [
        alice.getKeyID(),
        bob.getKeyID(),
      ]);
    });

    it("getPackets", async () => {
      const sig = await multiSignature;
      const { publicKey: alice } = await aliceKeyPair;
      const { publicKey: bob } = await bobKeyPair;

      assertEquals(sig.getPackets().length, 2);

      assertEquals(sig.getPackets(alice.getKeyID()).length, 1);

      assertEquals(sig.getPackets(bob.getKeyID()).length, 1);
    });
  });
});