@ckb-ccc/nip07 0.0.9-alpha.0 → 0.0.10-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/signer.d.ts CHANGED
@@ -1,12 +1,9 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import { ConnectionsRepo } from "./connectionsRepo";
3
2
  import { Provider } from "./nip07.advanced";
4
3
  export declare class Signer extends ccc.SignerNostr {
5
4
  readonly provider: Provider;
6
- private readonly connectionsRepo;
7
- private publicKey;
8
- constructor(client: ccc.Client, provider: Provider, connectionsRepo?: ConnectionsRepo);
9
- replaceClient(client: ccc.Client): Promise<boolean>;
5
+ private publicKeyCache?;
6
+ constructor(client: ccc.Client, provider: Provider);
10
7
  getNostrPublicKey(): Promise<ccc.Hex>;
11
8
  signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
12
9
  connect(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EACL,eAAe,EAEhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,MAAO,SAAQ,GAAG,CAAC,WAAW;aAKvB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,eAAe;IALlC,OAAO,CAAC,SAAS,CAAkC;gBAGjD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInD,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAarC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAI9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAKxB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAQtC"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,MAAO,SAAQ,GAAG,CAAC,WAAW;aAKvB,QAAQ,EAAE,QAAQ;IAJpC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ;IAK9B,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAQrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
package/dist/signer.js CHANGED
@@ -1,37 +1,26 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import { ConnectionsRepoLocalStorage, } from "./connectionsRepo";
3
2
  export class Signer extends ccc.SignerNostr {
4
- constructor(client, provider, connectionsRepo = new ConnectionsRepoLocalStorage()) {
3
+ constructor(client, provider) {
5
4
  super(client);
6
5
  this.provider = provider;
7
- this.connectionsRepo = connectionsRepo;
8
- this.publicKey = undefined;
9
- }
10
- async replaceClient(client) {
11
- return super.replaceClient(client);
6
+ this.publicKeyCache = undefined;
12
7
  }
13
8
  async getNostrPublicKey() {
14
- if (this.publicKey) {
15
- return this.publicKey;
16
- }
17
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
18
- if (!this.publicKey) {
19
- throw new Error("Not connected");
9
+ if (!this.publicKeyCache) {
10
+ this.publicKeyCache = this.provider.getPublicKey();
20
11
  }
21
- return this.publicKey;
12
+ return ccc.hexFrom(await this.publicKeyCache);
22
13
  }
23
14
  async signNostrEvent(event) {
24
- return this.provider.signEvent(event);
15
+ return this.provider.signEvent({
16
+ ...event,
17
+ pubkey: await this.publicKeyCache,
18
+ });
25
19
  }
26
20
  async connect() {
27
- this.publicKey = ccc.hexFrom(await this.provider.getPublicKey());
28
- await this.connectionsRepo.set({ publicKey: this.publicKey });
21
+ return;
29
22
  }
30
23
  async isConnected() {
31
- if (this.publicKey) {
32
- return true;
33
- }
34
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
35
- return this.publicKey !== undefined;
24
+ return true;
36
25
  }
37
26
  }
@@ -1,12 +1,9 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import { ConnectionsRepo } from "./connectionsRepo";
3
2
  import { Provider } from "./nip07.advanced";
4
3
  export declare class Signer extends ccc.SignerNostr {
5
4
  readonly provider: Provider;
6
- private readonly connectionsRepo;
7
- private publicKey;
8
- constructor(client: ccc.Client, provider: Provider, connectionsRepo?: ConnectionsRepo);
9
- replaceClient(client: ccc.Client): Promise<boolean>;
5
+ private publicKeyCache?;
6
+ constructor(client: ccc.Client, provider: Provider);
10
7
  getNostrPublicKey(): Promise<ccc.Hex>;
11
8
  signNostrEvent(event: ccc.NostrEvent): Promise<Required<ccc.NostrEvent>>;
12
9
  connect(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EACL,eAAe,EAEhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,MAAO,SAAQ,GAAG,CAAC,WAAW;aAKvB,QAAQ,EAAE,QAAQ;IAClC,OAAO,CAAC,QAAQ,CAAC,eAAe;IALlC,OAAO,CAAC,SAAS,CAAkC;gBAGjD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ,EACjB,eAAe,GAAE,eAAmD;IAKjF,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAInD,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAarC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAI9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAKxB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAQtC"}
1
+ {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,qBAAa,MAAO,SAAQ,GAAG,CAAC,WAAW;aAKvB,QAAQ,EAAE,QAAQ;IAJpC,OAAO,CAAC,cAAc,CAAC,CAA8B;gBAGnD,MAAM,EAAE,GAAG,CAAC,MAAM,EACF,QAAQ,EAAE,QAAQ;IAK9B,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAQrC,cAAc,CAClB,KAAK,EAAE,GAAG,CAAC,UAAU,GACpB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAO9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
@@ -2,40 +2,29 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Signer = void 0;
4
4
  const core_1 = require("@ckb-ccc/core");
5
- const connectionsRepo_1 = require("./connectionsRepo");
6
5
  class Signer extends core_1.ccc.SignerNostr {
7
- constructor(client, provider, connectionsRepo = new connectionsRepo_1.ConnectionsRepoLocalStorage()) {
6
+ constructor(client, provider) {
8
7
  super(client);
9
8
  this.provider = provider;
10
- this.connectionsRepo = connectionsRepo;
11
- this.publicKey = undefined;
12
- }
13
- async replaceClient(client) {
14
- return super.replaceClient(client);
9
+ this.publicKeyCache = undefined;
15
10
  }
16
11
  async getNostrPublicKey() {
17
- if (this.publicKey) {
18
- return this.publicKey;
19
- }
20
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
21
- if (!this.publicKey) {
22
- throw new Error("Not connected");
12
+ if (!this.publicKeyCache) {
13
+ this.publicKeyCache = this.provider.getPublicKey();
23
14
  }
24
- return this.publicKey;
15
+ return core_1.ccc.hexFrom(await this.publicKeyCache);
25
16
  }
26
17
  async signNostrEvent(event) {
27
- return this.provider.signEvent(event);
18
+ return this.provider.signEvent({
19
+ ...event,
20
+ pubkey: await this.publicKeyCache,
21
+ });
28
22
  }
29
23
  async connect() {
30
- this.publicKey = core_1.ccc.hexFrom(await this.provider.getPublicKey());
31
- await this.connectionsRepo.set({ publicKey: this.publicKey });
24
+ return;
32
25
  }
33
26
  async isConnected() {
34
- if (this.publicKey) {
35
- return true;
36
- }
37
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
38
- return this.publicKey !== undefined;
27
+ return true;
39
28
  }
40
29
  }
41
30
  exports.Signer = Signer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckb-ccc/nip07",
3
- "version": "0.0.9-alpha.0",
3
+ "version": "0.0.10-alpha.0",
4
4
  "description": "CCC - CKBer's Codebase. Common Chains Connector's support for NIP07",
5
5
  "author": "Hanssen0 <hanssen0@hanssen0.com>",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  "access": "public"
43
43
  },
44
44
  "dependencies": {
45
- "@ckb-ccc/core": "0.0.9-alpha.0"
45
+ "@ckb-ccc/core": "0.0.10-alpha.0"
46
46
  },
47
- "gitHead": "ac4435006da9c9e48b3b66fc329e6f539156b252"
47
+ "gitHead": "8a4c789419f5e12e1f230cead213c59373834a1a"
48
48
  }
package/src/signer.ts CHANGED
@@ -1,55 +1,38 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import {
3
- ConnectionsRepo,
4
- ConnectionsRepoLocalStorage,
5
- } from "./connectionsRepo";
6
2
  import { Provider } from "./nip07.advanced";
7
3
 
8
4
  export class Signer extends ccc.SignerNostr {
9
- private publicKey: ccc.Hex | undefined = undefined;
5
+ private publicKeyCache?: Promise<string> = undefined;
10
6
 
11
7
  constructor(
12
8
  client: ccc.Client,
13
9
  public readonly provider: Provider,
14
- private readonly connectionsRepo: ConnectionsRepo = new ConnectionsRepoLocalStorage(),
15
10
  ) {
16
11
  super(client);
17
12
  }
18
13
 
19
- async replaceClient(client: ccc.Client): Promise<boolean> {
20
- return super.replaceClient(client);
21
- }
22
-
23
14
  async getNostrPublicKey(): Promise<ccc.Hex> {
24
- if (this.publicKey) {
25
- return this.publicKey;
26
- }
27
-
28
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
29
- if (!this.publicKey) {
30
- throw new Error("Not connected");
15
+ if (!this.publicKeyCache) {
16
+ this.publicKeyCache = this.provider.getPublicKey();
31
17
  }
32
18
 
33
- return this.publicKey;
19
+ return ccc.hexFrom(await this.publicKeyCache);
34
20
  }
35
21
 
36
22
  async signNostrEvent(
37
23
  event: ccc.NostrEvent,
38
24
  ): Promise<Required<ccc.NostrEvent>> {
39
- return this.provider.signEvent(event);
25
+ return this.provider.signEvent({
26
+ ...event,
27
+ pubkey: await this.publicKeyCache,
28
+ });
40
29
  }
41
30
 
42
31
  async connect(): Promise<void> {
43
- this.publicKey = ccc.hexFrom(await this.provider.getPublicKey());
44
- await this.connectionsRepo.set({ publicKey: this.publicKey });
32
+ return;
45
33
  }
46
34
 
47
35
  async isConnected(): Promise<boolean> {
48
- if (this.publicKey) {
49
- return true;
50
- }
51
-
52
- this.publicKey = (await this.connectionsRepo.get())?.publicKey;
53
- return this.publicKey !== undefined;
36
+ return true;
54
37
  }
55
38
  }
@@ -1,51 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- /**
3
- * Type representing a connection with public key.
4
- * @typedef {Object} Connection
5
- * @property {ccc.Hex} publicKey - The public key of the connection.
6
- */
7
- export type Connection = {
8
- readonly publicKey: ccc.Hex;
9
- };
10
- /**
11
- * Interface representing a repository for managing connections.
12
- * @interface
13
- */
14
- export interface ConnectionsRepo {
15
- /**
16
- * Gets a connection.
17
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
18
- */
19
- get(): Promise<Connection | undefined>;
20
- /**
21
- * Sets a connection.
22
- * @param {Connection | undefined} connection - The connection to set.
23
- * @returns {Promise<void>} A promise that resolves when the connection is set.
24
- */
25
- set(connection: Connection | undefined): Promise<void>;
26
- }
27
- /**
28
- * Class representing a local storage-based repository for managing connections.
29
- * @class
30
- * @implements {ConnectionsRepo}
31
- */
32
- export declare class ConnectionsRepoLocalStorage implements ConnectionsRepo {
33
- private readonly storageKey;
34
- /**
35
- * Creates an instance of ConnectionsRepoLocalStorage.
36
- * @param {string} [storageKey="ccc-nip07-signer"] - The local storage key.
37
- */
38
- constructor(storageKey?: string);
39
- /**
40
- * Gets a connection.
41
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
42
- */
43
- get(): Promise<Connection | undefined>;
44
- /**
45
- * Sets a connection.
46
- * @param {Connection | undefined} connection - The connection to set.
47
- * @returns {Promise<void>}
48
- */
49
- set(connection: Connection | undefined): Promise<void>;
50
- }
51
- //# sourceMappingURL=connectionsRepo.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"connectionsRepo.d.ts","sourceRoot":"","sources":["../src/connectionsRepo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,CAAC;CAC7B,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,GAAG,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAEvC;;;;OAIG;IACH,GAAG,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxD;AAED;;;;GAIG;AACH,qBAAa,2BAA4B,YAAW,eAAe;IAKrD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAJvC;;;OAGG;gBAC0B,UAAU,SAAqB;IAE5D;;;OAGG;IACG,GAAG,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAK5C;;;;OAIG;IACG,GAAG,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ7D"}
@@ -1,34 +0,0 @@
1
- /**
2
- * Class representing a local storage-based repository for managing connections.
3
- * @class
4
- * @implements {ConnectionsRepo}
5
- */
6
- export class ConnectionsRepoLocalStorage {
7
- /**
8
- * Creates an instance of ConnectionsRepoLocalStorage.
9
- * @param {string} [storageKey="ccc-nip07-signer"] - The local storage key.
10
- */
11
- constructor(storageKey = "ccc-nip07-signer") {
12
- this.storageKey = storageKey;
13
- }
14
- /**
15
- * Gets a connection.
16
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
17
- */
18
- async get() {
19
- const got = window.localStorage.getItem(this.storageKey);
20
- return got ? JSON.parse(got) : undefined;
21
- }
22
- /**
23
- * Sets a connection.
24
- * @param {Connection | undefined} connection - The connection to set.
25
- * @returns {Promise<void>}
26
- */
27
- async set(connection) {
28
- if (connection === undefined) {
29
- window.localStorage.removeItem(this.storageKey);
30
- return;
31
- }
32
- window.localStorage.setItem(this.storageKey, JSON.stringify(connection));
33
- }
34
- }
@@ -1,51 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- /**
3
- * Type representing a connection with public key.
4
- * @typedef {Object} Connection
5
- * @property {ccc.Hex} publicKey - The public key of the connection.
6
- */
7
- export type Connection = {
8
- readonly publicKey: ccc.Hex;
9
- };
10
- /**
11
- * Interface representing a repository for managing connections.
12
- * @interface
13
- */
14
- export interface ConnectionsRepo {
15
- /**
16
- * Gets a connection.
17
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
18
- */
19
- get(): Promise<Connection | undefined>;
20
- /**
21
- * Sets a connection.
22
- * @param {Connection | undefined} connection - The connection to set.
23
- * @returns {Promise<void>} A promise that resolves when the connection is set.
24
- */
25
- set(connection: Connection | undefined): Promise<void>;
26
- }
27
- /**
28
- * Class representing a local storage-based repository for managing connections.
29
- * @class
30
- * @implements {ConnectionsRepo}
31
- */
32
- export declare class ConnectionsRepoLocalStorage implements ConnectionsRepo {
33
- private readonly storageKey;
34
- /**
35
- * Creates an instance of ConnectionsRepoLocalStorage.
36
- * @param {string} [storageKey="ccc-nip07-signer"] - The local storage key.
37
- */
38
- constructor(storageKey?: string);
39
- /**
40
- * Gets a connection.
41
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
42
- */
43
- get(): Promise<Connection | undefined>;
44
- /**
45
- * Sets a connection.
46
- * @param {Connection | undefined} connection - The connection to set.
47
- * @returns {Promise<void>}
48
- */
49
- set(connection: Connection | undefined): Promise<void>;
50
- }
51
- //# sourceMappingURL=connectionsRepo.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"connectionsRepo.d.ts","sourceRoot":"","sources":["../src/connectionsRepo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,CAAC;CAC7B,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,GAAG,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAEvC;;;;OAIG;IACH,GAAG,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxD;AAED;;;;GAIG;AACH,qBAAa,2BAA4B,YAAW,eAAe;IAKrD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAJvC;;;OAGG;gBAC0B,UAAU,SAAqB;IAE5D;;;OAGG;IACG,GAAG,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAK5C;;;;OAIG;IACG,GAAG,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ7D"}
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectionsRepoLocalStorage = void 0;
4
- /**
5
- * Class representing a local storage-based repository for managing connections.
6
- * @class
7
- * @implements {ConnectionsRepo}
8
- */
9
- class ConnectionsRepoLocalStorage {
10
- /**
11
- * Creates an instance of ConnectionsRepoLocalStorage.
12
- * @param {string} [storageKey="ccc-nip07-signer"] - The local storage key.
13
- */
14
- constructor(storageKey = "ccc-nip07-signer") {
15
- this.storageKey = storageKey;
16
- }
17
- /**
18
- * Gets a connection.
19
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
20
- */
21
- async get() {
22
- const got = window.localStorage.getItem(this.storageKey);
23
- return got ? JSON.parse(got) : undefined;
24
- }
25
- /**
26
- * Sets a connection.
27
- * @param {Connection | undefined} connection - The connection to set.
28
- * @returns {Promise<void>}
29
- */
30
- async set(connection) {
31
- if (connection === undefined) {
32
- window.localStorage.removeItem(this.storageKey);
33
- return;
34
- }
35
- window.localStorage.setItem(this.storageKey, JSON.stringify(connection));
36
- }
37
- }
38
- exports.ConnectionsRepoLocalStorage = ConnectionsRepoLocalStorage;
@@ -1,65 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
-
3
- /**
4
- * Type representing a connection with public key.
5
- * @typedef {Object} Connection
6
- * @property {ccc.Hex} publicKey - The public key of the connection.
7
- */
8
- export type Connection = {
9
- readonly publicKey: ccc.Hex;
10
- };
11
-
12
- /**
13
- * Interface representing a repository for managing connections.
14
- * @interface
15
- */
16
- export interface ConnectionsRepo {
17
- /**
18
- * Gets a connection.
19
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
20
- */
21
- get(): Promise<Connection | undefined>;
22
-
23
- /**
24
- * Sets a connection.
25
- * @param {Connection | undefined} connection - The connection to set.
26
- * @returns {Promise<void>} A promise that resolves when the connection is set.
27
- */
28
- set(connection: Connection | undefined): Promise<void>;
29
- }
30
-
31
- /**
32
- * Class representing a local storage-based repository for managing connections.
33
- * @class
34
- * @implements {ConnectionsRepo}
35
- */
36
- export class ConnectionsRepoLocalStorage implements ConnectionsRepo {
37
- /**
38
- * Creates an instance of ConnectionsRepoLocalStorage.
39
- * @param {string} [storageKey="ccc-nip07-signer"] - The local storage key.
40
- */
41
- constructor(private readonly storageKey = "ccc-nip07-signer") {}
42
-
43
- /**
44
- * Gets a connection.
45
- * @returns {Promise<Connection | undefined>} A promise that resolves to the connection, if found.
46
- */
47
- async get(): Promise<Connection | undefined> {
48
- const got = window.localStorage.getItem(this.storageKey);
49
- return got ? JSON.parse(got) : undefined;
50
- }
51
-
52
- /**
53
- * Sets a connection.
54
- * @param {Connection | undefined} connection - The connection to set.
55
- * @returns {Promise<void>}
56
- */
57
- async set(connection: Connection | undefined): Promise<void> {
58
- if (connection === undefined) {
59
- window.localStorage.removeItem(this.storageKey);
60
- return;
61
- }
62
-
63
- window.localStorage.setItem(this.storageKey, JSON.stringify(connection));
64
- }
65
- }