@didcid/cipher 0.1.3

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.
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var bip39 = require('bip39');
6
+ var cipherBase = require('./cipher-base-D2GtcXrx.cjs');
7
+ var crypto = require('crypto');
8
+ var HDKeyNode = require('hdkey');
9
+ require('@noble/secp256k1');
10
+ require('canonicalize');
11
+
12
+ function _interopNamespaceDefault(e) {
13
+ var n = Object.create(null);
14
+ if (e) {
15
+ Object.keys(e).forEach(function (k) {
16
+ if (k !== 'default') {
17
+ var d = Object.getOwnPropertyDescriptor(e, k);
18
+ Object.defineProperty(n, k, d.get ? d : {
19
+ enumerable: true,
20
+ get: function () { return e[k]; }
21
+ });
22
+ }
23
+ });
24
+ }
25
+ n.default = e;
26
+ return Object.freeze(n);
27
+ }
28
+
29
+ var bip39__namespace = /*#__PURE__*/_interopNamespaceDefault(bip39);
30
+
31
+ class CipherNode extends cipherBase.CipherBase {
32
+ generateHDKey(mnemonic) {
33
+ const seed = bip39__namespace.mnemonicToSeedSync(mnemonic);
34
+ return HDKeyNode.fromMasterSeed(seed);
35
+ }
36
+ generateHDKeyJSON(json) {
37
+ return HDKeyNode.fromJSON(json);
38
+ }
39
+ generateRandomSalt() {
40
+ return cipherBase.base64url.encode(crypto.randomBytes(32));
41
+ }
42
+ }
43
+
44
+ exports.default = CipherNode;
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var bip39 = require('bip39');
6
+ var cipherBase = require('./cipher-base-D2GtcXrx.cjs');
7
+ var HDKeyBrowser = require('@didcid/browser-hdkey');
8
+ require('@noble/secp256k1');
9
+ require('canonicalize');
10
+
11
+ function _interopNamespaceDefault(e) {
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var bip39__namespace = /*#__PURE__*/_interopNamespaceDefault(bip39);
29
+
30
+ class CipherWeb extends cipherBase.CipherBase {
31
+ generateHDKey(mnemonic) {
32
+ const seed = bip39__namespace.mnemonicToSeedSync(mnemonic);
33
+ return HDKeyBrowser.fromMasterSeed(seed);
34
+ }
35
+ generateHDKeyJSON(json) {
36
+ return HDKeyBrowser.fromJSON(json);
37
+ }
38
+ generateRandomSalt() {
39
+ const array = new Uint8Array(32);
40
+ if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
41
+ window.crypto.getRandomValues(array);
42
+ }
43
+ else if (typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.getRandomValues) {
44
+ globalThis.crypto.getRandomValues(array);
45
+ }
46
+ else {
47
+ throw new Error('No secure random number generator available.');
48
+ }
49
+ return cipherBase.base64url.encode(array);
50
+ }
51
+ }
52
+
53
+ exports.default = CipherWeb;
@@ -0,0 +1,127 @@
1
+ import * as bip39 from 'bip39';
2
+ import * as secp from '@noble/secp256k1';
3
+ import { hmac } from '@noble/hashes/hmac';
4
+ import { sha256 } from '@noble/hashes/sha256';
5
+ import { xchacha20poly1305 } from '@noble/ciphers/chacha';
6
+ import { managedNonce } from '@noble/ciphers/webcrypto/utils';
7
+ import { bytesToUtf8, utf8ToBytes } from '@noble/ciphers/utils';
8
+ import { base64url } from 'multiformats/bases/base64';
9
+ import canonicalizeModule from 'canonicalize';
10
+ const canonicalize = canonicalizeModule;
11
+ // Polyfill for synchronous signatures
12
+ secp.etc.hmacSha256Sync = (k, ...m) => hmac(sha256, k, secp.etc.concatBytes(...m));
13
+ export default class CipherBase {
14
+ generateMnemonic() {
15
+ return bip39.generateMnemonic();
16
+ }
17
+ generateJwk(privateKeyBytes) {
18
+ const compressedPublicKeyBytes = secp.getPublicKey(privateKeyBytes);
19
+ const compressedPublicKeyHex = secp.etc.bytesToHex(compressedPublicKeyBytes);
20
+ const curvePoints = secp.ProjectivePoint.fromHex(compressedPublicKeyHex);
21
+ const uncompressedPublicKeyBytes = curvePoints.toRawBytes(false);
22
+ const d = base64url.baseEncode(privateKeyBytes);
23
+ const x = base64url.baseEncode(uncompressedPublicKeyBytes.subarray(1, 33));
24
+ const y = base64url.baseEncode(uncompressedPublicKeyBytes.subarray(33, 65));
25
+ const publicJwk = {
26
+ kty: 'EC',
27
+ crv: 'secp256k1',
28
+ x,
29
+ y
30
+ };
31
+ const privateJwk = { ...publicJwk, d };
32
+ return { publicJwk, privateJwk };
33
+ }
34
+ generateRandomJwk() {
35
+ const privKey = secp.utils.randomPrivateKey();
36
+ return this.generateJwk(privKey);
37
+ }
38
+ convertJwkToCompressedBytes(jwk) {
39
+ const xBytes = base64url.baseDecode(jwk.x);
40
+ const yBytes = base64url.baseDecode(jwk.y);
41
+ const prefix = yBytes[yBytes.length - 1] % 2 === 0 ? 0x02 : 0x03;
42
+ return new Uint8Array([prefix, ...xBytes]);
43
+ }
44
+ hashMessage(msg) {
45
+ const hash = sha256(msg);
46
+ return Buffer.from(hash).toString('hex');
47
+ }
48
+ canonicalizeJSON(json) {
49
+ return canonicalize(json);
50
+ }
51
+ hashJSON(json) {
52
+ return this.hashMessage(this.canonicalizeJSON(json));
53
+ }
54
+ signHash(msgHash, privateJwk) {
55
+ const privKey = base64url.baseDecode(privateJwk.d);
56
+ const signature = secp.sign(msgHash, privKey);
57
+ return signature.toCompactHex();
58
+ }
59
+ verifySig(msgHash, sigHex, publicJwk) {
60
+ const compressedPublicKeyBytes = this.convertJwkToCompressedBytes(publicJwk);
61
+ const signature = secp.Signature.fromCompact(sigHex);
62
+ return secp.verify(signature, msgHash, compressedPublicKeyBytes);
63
+ }
64
+ encryptBytes(pubKey, privKey, data) {
65
+ const priv = base64url.baseDecode(privKey.d);
66
+ const pub = this.convertJwkToCompressedBytes(pubKey);
67
+ const ss = secp.getSharedSecret(priv, pub);
68
+ const key = ss.slice(0, 32);
69
+ const chacha = managedNonce(xchacha20poly1305)(key);
70
+ const ciphertext = chacha.encrypt(data);
71
+ return base64url.baseEncode(ciphertext);
72
+ }
73
+ decryptBytes(pubKey, privKey, ciphertext) {
74
+ const priv = base64url.baseDecode(privKey.d);
75
+ const pub = this.convertJwkToCompressedBytes(pubKey);
76
+ const ss = secp.getSharedSecret(priv, pub);
77
+ const key = ss.slice(0, 32);
78
+ const chacha = managedNonce(xchacha20poly1305)(key);
79
+ const cipherdata = base64url.baseDecode(ciphertext);
80
+ return chacha.decrypt(cipherdata);
81
+ }
82
+ encryptMessage(pubKey, privKey, message) {
83
+ const data = utf8ToBytes(message);
84
+ return this.encryptBytes(pubKey, privKey, data);
85
+ }
86
+ decryptMessage(pubKey, privKey, ciphertext) {
87
+ const data = this.decryptBytes(pubKey, privKey, ciphertext);
88
+ return bytesToUtf8(data);
89
+ }
90
+ hasLeadingZeroBits(hexHash, bits) {
91
+ const binary = BigInt('0x' + hexHash).toString(2).padStart(hexHash.length * 4, '0');
92
+ return binary.startsWith('0'.repeat(bits));
93
+ }
94
+ addProofOfWork(obj, difficulty) {
95
+ if (!Number.isInteger(difficulty) || difficulty < 0 || difficulty > 256) {
96
+ throw new Error('Invalid difficulty: must be an integer between 0 and 256.');
97
+ }
98
+ let nonce = 0;
99
+ while (true) {
100
+ const candidate = {
101
+ ...obj,
102
+ pow: {
103
+ nonce: nonce.toString(16),
104
+ difficulty,
105
+ }
106
+ };
107
+ const hash = this.hashJSON(candidate);
108
+ if (this.hasLeadingZeroBits(hash, difficulty)) {
109
+ return candidate;
110
+ }
111
+ nonce++;
112
+ }
113
+ }
114
+ checkProofOfWork(obj) {
115
+ if (!obj ||
116
+ typeof obj !== 'object' ||
117
+ !('pow' in obj) ||
118
+ typeof obj.pow !== 'object' ||
119
+ typeof obj.pow.nonce !== 'string' ||
120
+ typeof obj.pow.difficulty !== 'number') {
121
+ return false;
122
+ }
123
+ const hash = this.hashJSON(obj);
124
+ return this.hasLeadingZeroBits(hash, obj.pow.difficulty);
125
+ }
126
+ }
127
+ //# sourceMappingURL=cipher-base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cipher-base.js","sourceRoot":"","sources":["../../src/cipher-base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEtD,OAAO,kBAAkB,MAAM,cAAc,CAAC;AAC9C,MAAM,YAAY,GAAG,kBAA2D,CAAC;AAEjF,sCAAsC;AACtC,IAAI,CAAC,GAAG,CAAC,cAAc,GAAG,CAAC,CAAa,EAAE,GAAG,CAAe,EAAc,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AAEzH,MAAM,CAAC,OAAO,OAAgB,UAAU;IAKpC,gBAAgB;QACZ,OAAO,KAAK,CAAC,gBAAgB,EAAE,CAAC;IACpC,CAAC;IAED,WAAW,CAAC,eAA2B;QACnC,MAAM,wBAAwB,GAAG,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;QACpE,MAAM,sBAAsB,GAAG,IAAI,CAAC,GAAG,CAAC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,CAAC,GAAG,CAAC,UAAU,KAAK,QAAQ,EACjD,CAAC;YACC,OAAO,KAAK,CAAC;QACjB,CAAC;QAED,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAEhC,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAI,EAAG,GAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACtE,CAAC;CACJ"}
@@ -0,0 +1,18 @@
1
+ import * as bip39 from 'bip39';
2
+ import { base64url } from 'multiformats/bases/base64';
3
+ import { randomBytes } from 'crypto';
4
+ import HDKeyNode from 'hdkey';
5
+ import CipherBase from './cipher-base.js';
6
+ export default class CipherNode extends CipherBase {
7
+ generateHDKey(mnemonic) {
8
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
9
+ return HDKeyNode.fromMasterSeed(seed);
10
+ }
11
+ generateHDKeyJSON(json) {
12
+ return HDKeyNode.fromJSON(json);
13
+ }
14
+ generateRandomSalt() {
15
+ return base64url.encode(randomBytes(32));
16
+ }
17
+ }
18
+ //# sourceMappingURL=cipher-node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cipher-node.js","sourceRoot":"","sources":["../../src/cipher-node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,SAAS,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAG1C,MAAM,CAAC,OAAO,OAAO,UAAW,SAAQ,UAAU;IAC9C,aAAa,CAAC,QAAgB;QAC1B,MAAM,IAAI,GAAG,KAAK,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QAChD,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED,iBAAiB,CAAC,IAAe;QAC7B,OAAO,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,kBAAkB;QACd,OAAO,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IAC7C,CAAC;CACJ"}
@@ -0,0 +1,27 @@
1
+ import * as bip39 from 'bip39';
2
+ import { base64url } from 'multiformats/bases/base64';
3
+ import CipherBase from './cipher-base.js';
4
+ import HDKeyBrowser from '@didcid/browser-hdkey';
5
+ export default class CipherWeb extends CipherBase {
6
+ generateHDKey(mnemonic) {
7
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
8
+ return HDKeyBrowser.fromMasterSeed(seed);
9
+ }
10
+ generateHDKeyJSON(json) {
11
+ return HDKeyBrowser.fromJSON(json);
12
+ }
13
+ generateRandomSalt() {
14
+ const array = new Uint8Array(32);
15
+ if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
16
+ window.crypto.getRandomValues(array);
17
+ }
18
+ else if (typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.getRandomValues) {
19
+ globalThis.crypto.getRandomValues(array);
20
+ }
21
+ else {
22
+ throw new Error('No secure random number generator available.');
23
+ }
24
+ return base64url.encode(array);
25
+ }
26
+ }
27
+ //# sourceMappingURL=cipher-web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cipher-web.js","sourceRoot":"","sources":["../../src/cipher-web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAE1C,OAAO,YAAY,MAAM,uBAAuB,CAAC;AAEjD,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,UAAU;IAC7C,aAAa,CAAC,QAAgB;QAC1B,MAAM,IAAI,GAAG,KAAK,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QAChD,OAAO,YAAY,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,iBAAiB,CAAC,IAAe;QAC7B,OAAO,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACvC,CAAC;IAED,kBAAkB;QACd,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;QACjC,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;YAClF,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QACzC,CAAC;aAAM,IAAI,OAAO,UAAU,KAAK,WAAW,IAAI,UAAU,CAAC,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;YACrG,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAC7C,CAAC;aAAM,CAAC;YACJ,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QACpE,CAAC;QACD,OAAO,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,22 @@
1
+ import { Cipher, HDKeyJSON, EcdsaJwkPublic, EcdsaJwkPrivate, EcdsaJwkPair } from './types.js';
2
+ export default abstract class CipherBase implements Cipher {
3
+ abstract generateHDKey(mnemonic: string): any;
4
+ abstract generateHDKeyJSON(json: HDKeyJSON): any;
5
+ abstract generateRandomSalt(): string;
6
+ generateMnemonic(): string;
7
+ generateJwk(privateKeyBytes: Uint8Array): EcdsaJwkPair;
8
+ generateRandomJwk(): EcdsaJwkPair;
9
+ convertJwkToCompressedBytes(jwk: EcdsaJwkPublic): Uint8Array;
10
+ hashMessage(msg: string | Uint8Array): string;
11
+ canonicalizeJSON(json: unknown): string;
12
+ hashJSON(json: unknown): string;
13
+ signHash(msgHash: string, privateJwk: EcdsaJwkPrivate): string;
14
+ verifySig(msgHash: string, sigHex: string, publicJwk: EcdsaJwkPublic): boolean;
15
+ encryptBytes(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, data: Uint8Array): string;
16
+ decryptBytes(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, ciphertext: string): Uint8Array;
17
+ encryptMessage(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, message: string): string;
18
+ decryptMessage(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, ciphertext: string): string;
19
+ hasLeadingZeroBits(hexHash: string, bits: number): boolean;
20
+ addProofOfWork(obj: object, difficulty: number): object;
21
+ checkProofOfWork(obj: object): boolean;
22
+ }
@@ -0,0 +1,8 @@
1
+ import HDKeyNode from 'hdkey';
2
+ import CipherBase from './cipher-base.js';
3
+ import { Cipher, HDKeyJSON } from './types.js';
4
+ export default class CipherNode extends CipherBase implements Cipher {
5
+ generateHDKey(mnemonic: string): HDKeyNode;
6
+ generateHDKeyJSON(json: HDKeyJSON): HDKeyNode;
7
+ generateRandomSalt(): string;
8
+ }
@@ -0,0 +1,8 @@
1
+ import CipherBase from './cipher-base.js';
2
+ import { Cipher, HDKeyJSON } from './types.js';
3
+ import HDKeyBrowser from '@didcid/browser-hdkey';
4
+ export default class CipherWeb extends CipherBase implements Cipher {
5
+ generateHDKey(mnemonic: string): HDKeyBrowser;
6
+ generateHDKeyJSON(json: HDKeyJSON): HDKeyBrowser;
7
+ generateRandomSalt(): string;
8
+ }
@@ -0,0 +1,46 @@
1
+ import type HDKeyNode from 'hdkey';
2
+ import type HDKeyBrowser from '@didcid/browser-hdkey';
3
+ export interface HDKeyJSON {
4
+ xpriv: string;
5
+ xpub: string;
6
+ chainCode?: string;
7
+ depth?: number;
8
+ index?: number;
9
+ parentFingerprint?: number;
10
+ }
11
+ export interface EcdsaJwkPublic {
12
+ kty: 'EC';
13
+ crv: 'secp256k1';
14
+ x: string;
15
+ y: string;
16
+ }
17
+ export interface EcdsaJwkPrivate extends EcdsaJwkPublic {
18
+ d: string;
19
+ }
20
+ export interface EcdsaJwkPair {
21
+ publicJwk: EcdsaJwkPublic;
22
+ privateJwk: EcdsaJwkPrivate;
23
+ }
24
+ export interface ProofOfWork {
25
+ difficulty: number;
26
+ nonce: string;
27
+ }
28
+ export interface Cipher {
29
+ generateMnemonic(): string;
30
+ generateHDKey(mnemonic: string): HDKeyNode | HDKeyBrowser;
31
+ generateHDKeyJSON(json: HDKeyJSON): HDKeyNode | HDKeyBrowser;
32
+ generateJwk(privateKeyBytes: Uint8Array): EcdsaJwkPair;
33
+ generateRandomJwk(): EcdsaJwkPair;
34
+ convertJwkToCompressedBytes(jwk: EcdsaJwkPublic): Uint8Array;
35
+ hashMessage(msg: string | Uint8Array): string;
36
+ hashJSON(obj: unknown): string;
37
+ signHash(msgHash: string, privateJwk: EcdsaJwkPrivate): string;
38
+ verifySig(msgHash: string, sigHex: string, publicJwk: EcdsaJwkPublic): boolean;
39
+ encryptBytes(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, data: Uint8Array): string;
40
+ decryptBytes(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, ciphertext: string): Uint8Array;
41
+ encryptMessage(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, message: string): string;
42
+ decryptMessage(pubKey: EcdsaJwkPublic, privKey: EcdsaJwkPrivate, ciphertext: string): string;
43
+ generateRandomSalt(): string;
44
+ addProofOfWork(obj: unknown, difficulty: number): unknown;
45
+ checkProofOfWork(obj: unknown): boolean;
46
+ }
package/package.json ADDED
@@ -0,0 +1,85 @@
1
+ {
2
+ "name": "@didcid/cipher",
3
+ "version": "0.1.3",
4
+ "description": "Archon cipher lib",
5
+ "type": "module",
6
+ "main": "./dist/cjs/cipher-web.cjs",
7
+ "module": "./dist/esm/cipher-web.js",
8
+ "types": "./dist/types/cipher-web.d.ts",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "publishConfig": {
13
+ "registry": "https://registry.npmjs.org",
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "browser": {
19
+ "import": "./dist/esm/cipher-web.js",
20
+ "require": "./dist/cjs/cipher-web.cjs",
21
+ "types": "./dist/types/cipher-web.d.ts"
22
+ },
23
+ "node": {
24
+ "import": "./dist/esm/cipher-node.js",
25
+ "require": "./dist/cjs/cipher-node.cjs",
26
+ "types": "./dist/types/cipher-node.d.ts"
27
+ },
28
+ "default": {
29
+ "import": "./dist/esm/cipher-node.js",
30
+ "types": "./dist/types/cipher-node.d.ts"
31
+ }
32
+ },
33
+ "./node": {
34
+ "import": "./dist/esm/cipher-node.js",
35
+ "require": "./dist/cjs/cipher-node.cjs",
36
+ "types": "./dist/types/cipher-node.d.ts"
37
+ },
38
+ "./web": {
39
+ "import": "./dist/esm/cipher-web.js",
40
+ "require": "./dist/cjs/cipher-web.cjs",
41
+ "types": "./dist/types/cipher-web.d.ts"
42
+ },
43
+ "./types": {
44
+ "types": "./dist/types/types.d.ts"
45
+ }
46
+ },
47
+ "typesVersions": {
48
+ "*": {
49
+ "node": [
50
+ "./dist/types/cipher-node.d.ts"
51
+ ],
52
+ "web": [
53
+ "./dist/types/cipher-web.d.ts"
54
+ ],
55
+ "types": [
56
+ "./dist/types/types.d.ts"
57
+ ]
58
+ }
59
+ },
60
+ "scripts": {
61
+ "clean": "rm -rf dist",
62
+ "build:esm": "tsc -p tsconfig.json",
63
+ "bundle:cjs": "rollup -c rollup.cjs.config.js",
64
+ "build": "npm run clean && npm run build:esm && npm run bundle:cjs"
65
+ },
66
+ "keywords": [],
67
+ "author": "David McFadzean <davidmc@gmail.com>",
68
+ "license": "MIT",
69
+ "dependencies": {
70
+ "@didcid/browser-hdkey": "^0.1.11",
71
+ "@noble/ciphers": "^0.4.1",
72
+ "@noble/hashes": "^1.3.3",
73
+ "@noble/secp256k1": "^2.0.0",
74
+ "bip39": "^3.1.0",
75
+ "buffer": "^6.0.3",
76
+ "canonicalize": "^2.0.0",
77
+ "hdkey": "^2.1.0",
78
+ "multiformats": "^13.0.0"
79
+ },
80
+ "repository": {
81
+ "type": "git",
82
+ "url": "git+https://github.com/archetech/archon.git"
83
+ },
84
+ "gitHead": "dd45ee46bf0bad26d796e210f7837f36a1505960"
85
+ }