@owlmeans/basic-keys 0.1.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/LICENSE +21 -0
- package/README.md +474 -0
- package/build/.gitkeep +0 -0
- package/build/auth.d.ts +5 -0
- package/build/auth.d.ts.map +1 -0
- package/build/auth.js +45 -0
- package/build/auth.js.map +1 -0
- package/build/bin.d.ts +2 -0
- package/build/bin.d.ts.map +1 -0
- package/build/bin.js +9 -0
- package/build/bin.js.map +1 -0
- package/build/consts.d.ts +5 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +6 -0
- package/build/consts.js.map +1 -0
- package/build/helper.d.ts +4 -0
- package/build/helper.d.ts.map +1 -0
- package/build/helper.js +22 -0
- package/build/helper.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/index.js.map +1 -0
- package/build/keypair.d.ts +3 -0
- package/build/keypair.d.ts.map +1 -0
- package/build/keypair.js +46 -0
- package/build/keypair.js.map +1 -0
- package/build/model.d.ts +3 -0
- package/build/model.d.ts.map +1 -0
- package/build/model.js +70 -0
- package/build/model.js.map +1 -0
- package/build/plugins/ed25519.d.ts +3 -0
- package/build/plugins/ed25519.d.ts.map +1 -0
- package/build/plugins/ed25519.js +15 -0
- package/build/plugins/ed25519.js.map +1 -0
- package/build/plugins/export.d.ts +5 -0
- package/build/plugins/export.d.ts.map +1 -0
- package/build/plugins/export.js +4 -0
- package/build/plugins/export.js.map +1 -0
- package/build/plugins/index.d.ts +3 -0
- package/build/plugins/index.d.ts.map +1 -0
- package/build/plugins/index.js +6 -0
- package/build/plugins/index.js.map +1 -0
- package/build/plugins/types.d.ts +13 -0
- package/build/plugins/types.d.ts.map +1 -0
- package/build/plugins/types.js +2 -0
- package/build/plugins/types.js.map +1 -0
- package/build/plugins/xchacha.d.ts +3 -0
- package/build/plugins/xchacha.d.ts.map +1 -0
- package/build/plugins/xchacha.js +14 -0
- package/build/plugins/xchacha.js.map +1 -0
- package/build/types.d.ts +35 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/utils.d.ts +5 -0
- package/build/utils.d.ts.map +1 -0
- package/build/utils.js +29 -0
- package/build/utils.js.map +1 -0
- package/package.json +56 -0
- package/src/auth.ts +60 -0
- package/src/bin.ts +13 -0
- package/src/consts.ts +5 -0
- package/src/helper.ts +27 -0
- package/src/index.ts +8 -0
- package/src/keypair.ts +44 -0
- package/src/model.ts +113 -0
- package/src/plugins/ed25519.ts +23 -0
- package/src/plugins/export.ts +5 -0
- package/src/plugins/index.ts +8 -0
- package/src/plugins/types.ts +13 -0
- package/src/plugins/xchacha.ts +23 -0
- package/src/types.ts +41 -0
- package/src/utils.ts +35 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +1 -0
package/build/keypair.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { base64 } from '@scure/base';
|
|
2
|
+
import { KeyType } from './consts.js';
|
|
3
|
+
import { plugins } from './plugins/index.js';
|
|
4
|
+
import { assertType } from './utils.js';
|
|
5
|
+
export const inputToKeyPair = (input) => {
|
|
6
|
+
let type = KeyType.ED25519;
|
|
7
|
+
let keyPair = null;
|
|
8
|
+
let primaryKey = null;
|
|
9
|
+
if (typeof input === 'object') {
|
|
10
|
+
keyPair = input;
|
|
11
|
+
}
|
|
12
|
+
else if (typeof input === 'string') {
|
|
13
|
+
if (input in plugins) { // TODO: plugins need to be checked also
|
|
14
|
+
type = input;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const [keyType, key] = input.split(':', 2);
|
|
18
|
+
if (key == null && keyType != null) {
|
|
19
|
+
type = KeyType.ED25519;
|
|
20
|
+
primaryKey = base64.decode(keyType);
|
|
21
|
+
}
|
|
22
|
+
else if (key != null) {
|
|
23
|
+
type = keyType;
|
|
24
|
+
primaryKey = base64.decode(key);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error('basic.keys:string-type-or-key');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (keyPair == null) {
|
|
32
|
+
assertType(type);
|
|
33
|
+
if (primaryKey == null) {
|
|
34
|
+
primaryKey = plugins[type].random();
|
|
35
|
+
}
|
|
36
|
+
const publicKey = plugins[type].toPublic(primaryKey);
|
|
37
|
+
keyPair = {
|
|
38
|
+
privateKey: base64.encode(primaryKey),
|
|
39
|
+
publicKey: base64.encode(publicKey),
|
|
40
|
+
address: plugins[type].toAdress(publicKey),
|
|
41
|
+
type,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return keyPair;
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=keypair.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keypair.js","sourceRoot":"","sources":["../src/keypair.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAwB,EAAW,EAAE;IAClE,IAAI,IAAI,GAAW,OAAO,CAAC,OAAO,CAAA;IAClC,IAAI,OAAO,GAAmB,IAAI,CAAA;IAClC,IAAI,UAAU,GAAsB,IAAI,CAAA;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,KAAK,CAAA;IACjB,CAAC;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC,CAAC,wCAAwC;YAC9D,IAAI,GAAG,KAAK,CAAA;QACd,CAAC;aAAM,CAAC;YACN,MAAM,CAA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M,YAAY,CAAA;AAMjE,eAAO,MAAM,gBAAgB,EAAE,iBA0G9B,CAAA"}
|
package/build/model.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { plugins } from './plugins/index.js';
|
|
2
|
+
import { base64urlnopad, utf8 } from '@scure/base';
|
|
3
|
+
import { assertType, prepareData, prepareKey } from './utils.js';
|
|
4
|
+
import { inputToKeyPair } from './keypair.js';
|
|
5
|
+
export const makeKeyPairModel = input => {
|
|
6
|
+
const keyPair = inputToKeyPair(input);
|
|
7
|
+
const _model = {
|
|
8
|
+
keyPair,
|
|
9
|
+
sign: async (data) => {
|
|
10
|
+
data = prepareData(data);
|
|
11
|
+
assertType(_model.keyPair?.type);
|
|
12
|
+
if (_model.keyPair == null) {
|
|
13
|
+
throw new Error('basic.keys:missing-keypair');
|
|
14
|
+
}
|
|
15
|
+
if (_model.keyPair.privateKey == null) {
|
|
16
|
+
throw new Error('basic.keys:missing-pk');
|
|
17
|
+
}
|
|
18
|
+
return base64urlnopad.encode(plugins[_model.keyPair.type].sign(data, prepareKey(_model.keyPair.privateKey)));
|
|
19
|
+
},
|
|
20
|
+
verify: async (data, signature) => {
|
|
21
|
+
data = prepareData(data);
|
|
22
|
+
assertType(_model.keyPair?.type);
|
|
23
|
+
const sig = base64urlnopad.decode(signature);
|
|
24
|
+
if (_model.keyPair == null) {
|
|
25
|
+
throw new Error('basic.keys:missing-keypair');
|
|
26
|
+
}
|
|
27
|
+
return plugins[_model.keyPair.type].verify(data, sig, prepareKey(_model.keyPair.publicKey));
|
|
28
|
+
},
|
|
29
|
+
export: () => {
|
|
30
|
+
assertType(_model.keyPair?.type);
|
|
31
|
+
if (_model.keyPair == null) {
|
|
32
|
+
throw new Error('basic.keys:missing-keypair');
|
|
33
|
+
}
|
|
34
|
+
return `${_model.keyPair.type}:${_model.keyPair.privateKey}`;
|
|
35
|
+
},
|
|
36
|
+
exportPublic: () => {
|
|
37
|
+
assertType(_model.keyPair?.type);
|
|
38
|
+
if (_model.keyPair == null) {
|
|
39
|
+
throw new Error('basic.keys:missing-keypair');
|
|
40
|
+
}
|
|
41
|
+
return `${_model.keyPair.type}:${_model.keyPair.publicKey}`;
|
|
42
|
+
},
|
|
43
|
+
exportAddress: () => {
|
|
44
|
+
assertType(_model.keyPair?.type);
|
|
45
|
+
if (_model.keyPair == null) {
|
|
46
|
+
throw new Error('basic.keys:missing-keypair');
|
|
47
|
+
}
|
|
48
|
+
return `${_model.keyPair.type}:${_model.keyPair.address}`;
|
|
49
|
+
},
|
|
50
|
+
encrypt: async (data) => {
|
|
51
|
+
data = prepareData(data);
|
|
52
|
+
assertType(_model.keyPair?.type);
|
|
53
|
+
if (_model.keyPair == null) {
|
|
54
|
+
throw new Error('basic.keys:missing-keypair');
|
|
55
|
+
}
|
|
56
|
+
return base64urlnopad.encode(plugins[_model.keyPair.type].encrypt(data, prepareKey(_model.keyPair.publicKey)));
|
|
57
|
+
},
|
|
58
|
+
decrypt: async (data) => utf8.encode(await _model.dcrpt(data)),
|
|
59
|
+
dcrpt: async (data) => {
|
|
60
|
+
data = data instanceof Uint8Array ? data : base64urlnopad.decode(data);
|
|
61
|
+
assertType(_model.keyPair?.type);
|
|
62
|
+
if (_model.keyPair == null) {
|
|
63
|
+
throw new Error('basic.keys:missing-keypair');
|
|
64
|
+
}
|
|
65
|
+
return plugins[_model.keyPair.type].decrypt(data, prepareKey(_model.keyPair.privateKey));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return _model;
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,MA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|
1
|
+
import { KeyType } from '../consts.js';
|
|
2
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
3
|
+
import { base58 } from '@scure/base';
|
|
4
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
5
|
+
export const ed25519Plugin = {
|
|
6
|
+
type: KeyType.ED25519,
|
|
7
|
+
random: () => ed25519.utils.randomPrivateKey(),
|
|
8
|
+
sign: (data, pk) => ed25519.sign(data, pk),
|
|
9
|
+
verify: (data, signature, pub) => ed25519.verify(signature, data, pub),
|
|
10
|
+
toPublic: pk => ed25519.getPublicKey(pk),
|
|
11
|
+
toAdress: pub => base58.encode(keccak_256(pub.slice(4)).slice(-20)),
|
|
12
|
+
encrypt: () => { throw new Error(`${ed25519Plugin.type}:encryption-support`); },
|
|
13
|
+
decrypt: () => { throw new Error(`${ed25519Plugin.type}:encryption-support`); }
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=ed25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../src/plugins/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAE/C,MAAM,CAAC,MAAM,aAAa,GAAc;IACtC,IAAI,EAAE,OAAO,CAAC,OAAO;IAErB,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE;IAE9C,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;IAE1C,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,GAAG,CAAC;IAEtE,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;IAExC,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;IAEnE,OAAO,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,GAAG,aAAa,CAAC,IAAI,qBAAqB,CAAC,CAAA,CAAC,CAAC;IAE9E,OAAO,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,GAAG,aAAa,CAAC,IAAI,qBAAqB,CAAC,CAAA,CAAC,CAAC;CAC/E,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../src/plugins/export.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"export.js","sourceRoot":"","sources":["../../src/plugins/export.ts"],"names":[],"mappings":"AAEA,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAI3C,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAM,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,MAAM,CAAC,MAAM,OAAO,GAA8B,EAAE,CAAA;AAEpD,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAA;AAC3C,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,YAAY,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface KeyPlugin {
|
|
2
|
+
type: string;
|
|
3
|
+
random: () => Uint8Array;
|
|
4
|
+
fromSeed?: (seed: Uint8Array) => Uint8Array;
|
|
5
|
+
derive?: (pk: Uint8Array, path: string) => Uint8Array;
|
|
6
|
+
sign: (data: Uint8Array, pk: Uint8Array) => Uint8Array;
|
|
7
|
+
verify: (data: Uint8Array, signature: Uint8Array, pub: Uint8Array) => boolean;
|
|
8
|
+
toPublic: (pk: Uint8Array) => Uint8Array;
|
|
9
|
+
toAdress: (pub: Uint8Array) => string;
|
|
10
|
+
encrypt: (data: Uint8Array, pk: Uint8Array) => Uint8Array;
|
|
11
|
+
decrypt: (data: Uint8Array, pk: Uint8Array) => Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/plugins/types.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,UAAU,CAAA;IACxB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,UAAU,CAAA;IAC3C,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,KAAK,UAAU,CAAA;IACrD,IAAI,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,KAAK,UAAU,CAAA;IACtD,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,KAAK,OAAO,CAAA;IAC7E,QAAQ,EAAE,CAAC,EAAE,EAAE,UAAU,KAAK,UAAU,CAAA;IACxC,QAAQ,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,MAAM,CAAA;IACrC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,KAAK,UAAU,CAAA;IACzD,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,KAAK,UAAU,CAAA;CAC1D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/plugins/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xchacha.d.ts","sourceRoot":"","sources":["../../src/plugins/xchacha.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAK3C,eAAO,MAAM,YAAY,EAAE,SAgB1B,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { KeyType } from '../consts.js';
|
|
2
|
+
import { xchacha20poly1305 } from '@noble/ciphers/chacha';
|
|
3
|
+
import { randomBytes, managedNonce } from '@noble/ciphers/webcrypto';
|
|
4
|
+
export const xChahaPlugin = {
|
|
5
|
+
type: KeyType.XCHACHA,
|
|
6
|
+
random: () => randomBytes(32),
|
|
7
|
+
sign: () => { throw new Error(`${xChahaPlugin.type}:signing`); },
|
|
8
|
+
verify: () => { throw new Error(`${xChahaPlugin.type}:verification`); },
|
|
9
|
+
toPublic: pk => pk,
|
|
10
|
+
toAdress: () => 'no-address',
|
|
11
|
+
encrypt: (data, pk) => managedNonce(xchacha20poly1305)(pk).encrypt(data),
|
|
12
|
+
decrypt: (data, pk) => managedNonce(xchacha20poly1305)(pk).decrypt(data),
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=xchacha.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xchacha.js","sourceRoot":"","sources":["../../src/plugins/xchacha.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAGpE,MAAM,CAAC,MAAM,YAAY,GAAc;IACrC,IAAI,EAAE,OAAO,CAAC,OAAO;IAErB,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC;IAE7B,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,GAAG,YAAY,CAAC,IAAI,UAAU,CAAC,CAAA,CAAC,CAAC;IAE/D,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,GAAG,YAAY,CAAC,IAAI,eAAe,CAAC,CAAA,CAAC,CAAC;IAEtE,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE;IAElB,QAAQ,EAAE,GAAG,EAAE,CAAC,YAAY;IAE5B,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;IAExE,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;CACzE,CAAA"}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AuthCredentials } from '@owlmeans/auth';
|
|
2
|
+
export interface KeyPair {
|
|
3
|
+
privateKey: string;
|
|
4
|
+
publicKey: string;
|
|
5
|
+
address: string;
|
|
6
|
+
type: string;
|
|
7
|
+
}
|
|
8
|
+
export interface KeyPairModel {
|
|
9
|
+
keyPair?: KeyPair;
|
|
10
|
+
sign: (data: unknown) => Promise<string>;
|
|
11
|
+
verify: (data: unknown, signature: string) => Promise<boolean>;
|
|
12
|
+
export: () => string;
|
|
13
|
+
exportPublic: () => string;
|
|
14
|
+
exportAddress: () => string;
|
|
15
|
+
encrypt: (data: unknown) => Promise<string>;
|
|
16
|
+
decrypt: (data: unknown) => Promise<string>;
|
|
17
|
+
dcrpt: (data: unknown) => Promise<Uint8Array>;
|
|
18
|
+
}
|
|
19
|
+
export interface KeyPairModelMaker {
|
|
20
|
+
(input?: KeyPair | string): KeyPairModel;
|
|
21
|
+
}
|
|
22
|
+
export interface PayloadSigner {
|
|
23
|
+
<T extends {}>(payload: T): Promise<string>;
|
|
24
|
+
}
|
|
25
|
+
export interface PayloadVerifier {
|
|
26
|
+
<T extends {}>(payload: T, signature: string): Promise<boolean>;
|
|
27
|
+
}
|
|
28
|
+
export interface UnpackedAuthCredentials<T extends {} | undefined = undefined> {
|
|
29
|
+
unsigned: AuthCredentials | Omit<AuthCredentials, 'credential'>;
|
|
30
|
+
signature: string;
|
|
31
|
+
isValid?: boolean;
|
|
32
|
+
extras: T;
|
|
33
|
+
}
|
|
34
|
+
export type UnsignedAuthCredentials = AuthCredentials | Omit<AuthCredentials, "credential">;
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAErD,MAAM,WAAW,OAAO;IACtB,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA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,uBAAuB,GAAG,eAAe,GAAG,IAAI,CAAC,eAAe,EAAE,YAAY,CAAC,CAAA"}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/build/utils.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const toAddress: (publicKey: Uint8Array) => Uint8Array;
|
|
2
|
+
export declare const prepareKey: (key: string) => Uint8Array;
|
|
3
|
+
export declare const prepareData: (data: unknown) => Uint8Array;
|
|
4
|
+
export declare const assertType: (type?: string) => void;
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,SAAS,cAAe,UAAU,KAAG,UACP,CAAA;AAE3C,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,UACrB,CAAA;AAEpB,eAAO,MAAM,WAAW,SAAU,OAAO,KAAG,UAiB3C,CAAA;AAED,eAAO,MAAM,UAAU,UAAW,MAAM,KAAG,IAI1C,CAAA"}
|
package/build/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
2
|
+
import { base64, utf8 } from '@scure/base';
|
|
3
|
+
import { plugins } from './plugins/index.js';
|
|
4
|
+
import canonicalize from 'canonicalize';
|
|
5
|
+
export const toAddress = (publicKey) => keccak_256(publicKey.slice(4)).slice(-20);
|
|
6
|
+
export const prepareKey = (key) => base64.decode(key);
|
|
7
|
+
export const prepareData = (data) => {
|
|
8
|
+
if (typeof data === 'object') {
|
|
9
|
+
if (!(data instanceof Uint8Array)) {
|
|
10
|
+
data = canonicalize(data);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
if (typeof data === 'string') {
|
|
14
|
+
data = utf8.decode(data);
|
|
15
|
+
}
|
|
16
|
+
else if (!(data instanceof Uint8Array)) {
|
|
17
|
+
throw new Error('basic.keys:sign-data-type');
|
|
18
|
+
}
|
|
19
|
+
if (!(data instanceof Uint8Array)) {
|
|
20
|
+
throw new Error('basic.keys:sign-data-type');
|
|
21
|
+
}
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
export const assertType = (type) => {
|
|
25
|
+
if (type == null || !(type in plugins)) {
|
|
26
|
+
throw new Error('basic.keys:unknown-type');
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,YAAY,MAAM,cAAc,CAAA;AAEvC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,SAAqB,EAAc,EAAE,CAC7D,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAA;AAE3C,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAW,EAAc,EAAE,CACpD,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AAEpB,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAa,EAAc,EAAE;IACvD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;YAClC,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;SAAM,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAC9C,CAAC;IAED,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAC9C,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAa,EAAQ,EAAE;IAChD,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,IAAI,OAAO,CAAC,EAAE,CAAC;QACvC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;IAC5C,CAAC;AACH,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@owlmeans/basic-keys",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -b",
|
|
7
|
+
"dev": "sleep 48 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
|
|
8
|
+
"watch": "tsc -b -w --preserveWatchOutput --pretty"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"owlkeys": "./build/bin.js"
|
|
12
|
+
},
|
|
13
|
+
"main": "build/index.js",
|
|
14
|
+
"module": "build/index.js",
|
|
15
|
+
"types": "build/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./build/index.js",
|
|
19
|
+
"require": "./build/index.js",
|
|
20
|
+
"default": "./build/index.js",
|
|
21
|
+
"module": "./build/index.js",
|
|
22
|
+
"types": "./build/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./plugins": {
|
|
25
|
+
"import": "./build/plugins/export.js",
|
|
26
|
+
"require": "./build/plugins/export.js",
|
|
27
|
+
"default": "./build/plugins/export.js",
|
|
28
|
+
"module": "./build/plugins/export.js",
|
|
29
|
+
"types": "./build/plugins/export.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"./utils": {
|
|
32
|
+
"import": "./build/utils.js",
|
|
33
|
+
"require": "./build/utils.js",
|
|
34
|
+
"default": "./build/utils.js",
|
|
35
|
+
"module": "./build/utils.js",
|
|
36
|
+
"types": "./build/utils.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"nodemon": "^3.1.7",
|
|
41
|
+
"npm-check": "^6.0.1",
|
|
42
|
+
"typescript": "5.6.3"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@noble/ciphers": "^1.2.1",
|
|
46
|
+
"@noble/curves": "^1.6.0",
|
|
47
|
+
"@noble/hashes": "^1.5.0",
|
|
48
|
+
"@owlmeans/auth": "^0.1.0",
|
|
49
|
+
"@scure/base": "^1.1.9",
|
|
50
|
+
"canonicalize": "^2.0.0"
|
|
51
|
+
},
|
|
52
|
+
"private": false,
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { AuthCredentials } from '@owlmeans/auth'
|
|
2
|
+
import type { KeyPairModel, PayloadSigner, PayloadVerifier, UnpackedAuthCredentials, UnsignedAuthCredentials } from './types.js'
|
|
3
|
+
import canonicalize from 'canonicalize'
|
|
4
|
+
|
|
5
|
+
export const packAuthCredentials = async <T extends {} | undefined>(
|
|
6
|
+
auth: UnsignedAuthCredentials,
|
|
7
|
+
extra: T,
|
|
8
|
+
signer: KeyPairModel | PayloadSigner
|
|
9
|
+
): Promise<AuthCredentials> => {
|
|
10
|
+
const unsigned: UnsignedAuthCredentials = {
|
|
11
|
+
...auth,
|
|
12
|
+
...(extra == null ? {} : { credential: canonicalize(extra) as string })
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (typeof signer !== 'function') {
|
|
16
|
+
const _signer = signer
|
|
17
|
+
signer = async payload => await _signer.sign(payload)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const signature = await signer(unsigned)
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
...auth,
|
|
24
|
+
credential: extra == null ? signature : canonicalize({ ...extra, signature }) as string,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const unpackAuthCredentials = async <T extends {} | undefined>(
|
|
29
|
+
auth: AuthCredentials,
|
|
30
|
+
verifier?: KeyPairModel | PayloadVerifier
|
|
31
|
+
): Promise<UnpackedAuthCredentials<T>> => {
|
|
32
|
+
let signature: string = ''
|
|
33
|
+
let extras: T
|
|
34
|
+
try {
|
|
35
|
+
extras = JSON.parse(auth.credential)
|
|
36
|
+
if (typeof extras === 'object' && extras != null && "signature" in extras) {
|
|
37
|
+
signature = extras.signature as string
|
|
38
|
+
delete extras.signature
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
signature = auth.credential
|
|
42
|
+
extras = undefined as T
|
|
43
|
+
}
|
|
44
|
+
const unsigned: UnsignedAuthCredentials = {
|
|
45
|
+
...auth,
|
|
46
|
+
...(extras == null ? {} : { credential: canonicalize(extras) as string })
|
|
47
|
+
}
|
|
48
|
+
let isValid: boolean | undefined = undefined
|
|
49
|
+
|
|
50
|
+
if (verifier != null) {
|
|
51
|
+
if (typeof verifier !== 'function') {
|
|
52
|
+
const _verifier = verifier
|
|
53
|
+
verifier = async (payload, signature) => await _verifier.verify(payload, signature)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
isValid = await verifier(unsigned, signature)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { unsigned, signature, isValid, extras }
|
|
60
|
+
}
|
package/src/bin.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import { KeyType } from './consts.js'
|
|
3
|
+
import { makeKeyPairModel } from './model.js'
|
|
4
|
+
|
|
5
|
+
const keyPair = makeKeyPairModel()
|
|
6
|
+
|
|
7
|
+
console.info('Private export: ', keyPair.export())
|
|
8
|
+
console.info('Public export: ', keyPair.exportPublic())
|
|
9
|
+
console.info('DID export: ', keyPair.exportAddress())
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const xChachaKey = makeKeyPairModel(KeyType.XCHACHA)
|
|
13
|
+
console.info('XChaha key export: ', xChachaKey.export())
|
package/src/consts.ts
ADDED
package/src/helper.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { KeyType } from './consts.js'
|
|
2
|
+
import { makeKeyPairModel } from './model.js'
|
|
3
|
+
import { plugins } from './plugins/index.js'
|
|
4
|
+
import type { KeyPair, KeyPairModel } from './types.js'
|
|
5
|
+
import { prepareKey } from './utils.js'
|
|
6
|
+
|
|
7
|
+
export const fromPubKey = (pubKey: string, type?: string): KeyPairModel => {
|
|
8
|
+
if (type == null) {
|
|
9
|
+
[type, pubKey] = pubKey.includes(':') ? pubKey.split(':', 2) : [KeyType.ED25519, pubKey]
|
|
10
|
+
if (pubKey == null) {
|
|
11
|
+
pubKey = type
|
|
12
|
+
type = KeyType.ED25519
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const keyPair: KeyPair = {
|
|
17
|
+
privateKey: '',
|
|
18
|
+
publicKey: pubKey,
|
|
19
|
+
type,
|
|
20
|
+
address: plugins[type].toAdress(prepareKey(pubKey))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return makeKeyPairModel(keyPair)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const matchAddress = (address: string, pubKey: string): boolean =>
|
|
27
|
+
address === fromPubKey(pubKey).exportAddress()
|
package/src/index.ts
ADDED
package/src/keypair.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { KeyPair } from './types.js'
|
|
2
|
+
import { base64 } from '@scure/base'
|
|
3
|
+
import { KeyType } from './consts.js'
|
|
4
|
+
import { plugins } from './plugins/index.js'
|
|
5
|
+
import { assertType } from './utils.js'
|
|
6
|
+
|
|
7
|
+
export const inputToKeyPair = (input?: KeyPair | string): KeyPair => {
|
|
8
|
+
let type: string = KeyType.ED25519
|
|
9
|
+
let keyPair: KeyPair | null = null
|
|
10
|
+
let primaryKey: Uint8Array | null = null
|
|
11
|
+
if (typeof input === 'object') {
|
|
12
|
+
keyPair = input
|
|
13
|
+
} else if (typeof input === 'string') {
|
|
14
|
+
if (input in plugins) { // TODO: plugins need to be checked also
|
|
15
|
+
type = input
|
|
16
|
+
} else {
|
|
17
|
+
const [keyType, key] = input.split(':', 2)
|
|
18
|
+
if (key == null && keyType != null) {
|
|
19
|
+
type = KeyType.ED25519
|
|
20
|
+
primaryKey = base64.decode(keyType)
|
|
21
|
+
} else if (key != null) {
|
|
22
|
+
type = keyType
|
|
23
|
+
primaryKey = base64.decode(key)
|
|
24
|
+
} else {
|
|
25
|
+
throw new Error('basic.keys:string-type-or-key')
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (keyPair == null) {
|
|
30
|
+
assertType(type)
|
|
31
|
+
if (primaryKey == null) {
|
|
32
|
+
primaryKey = plugins[type].random()
|
|
33
|
+
}
|
|
34
|
+
const publicKey = plugins[type].toPublic(primaryKey)
|
|
35
|
+
keyPair = {
|
|
36
|
+
privateKey: base64.encode(primaryKey),
|
|
37
|
+
publicKey: base64.encode(publicKey),
|
|
38
|
+
address: plugins[type].toAdress(publicKey),
|
|
39
|
+
type,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return keyPair
|
|
44
|
+
}
|