moeralib 0.15.3 → 0.15.5
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/lib/crypto/crypto.js +218 -0
- package/lib/crypto/fingerprint.js +86 -0
- package/lib/crypto/index.js +14 -0
- package/lib/index.js +2 -0
- package/lib/naming/fingeprints.js +18 -0
- package/lib/naming/index.js +3 -1
- package/lib/naming/naming.js +0 -12
- package/lib/node/cartes.js +56 -7
- package/lib/node/fingerprints.js +191 -0
- package/lib/node/index.js +4 -1
- package/lib/node/node.js +102 -16
- package/lib/node/types.js +37 -0
- package/lib/node/validators.js +2704 -1753
- package/package.json +18 -2
- package/typings/crypto/crypto.d.ts +84 -0
- package/typings/crypto/crypto.d.ts.map +1 -0
- package/typings/crypto/fingerprint.d.ts +17 -0
- package/typings/crypto/fingerprint.d.ts.map +1 -0
- package/typings/crypto/index.d.ts +2 -0
- package/typings/crypto/index.d.ts.map +1 -0
- package/typings/index.d.ts +2 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/naming/fingeprints.d.ts +3 -0
- package/typings/naming/fingeprints.d.ts.map +1 -0
- package/typings/naming/index.d.ts +1 -0
- package/typings/naming/index.d.ts.map +1 -1
- package/typings/naming/naming.d.ts +4 -0
- package/typings/naming/naming.d.ts.map +1 -1
- package/typings/node/cartes.d.ts +39 -2
- package/typings/node/cartes.d.ts.map +1 -1
- package/typings/node/fingerprints.d.ts +15 -0
- package/typings/node/fingerprints.d.ts.map +1 -0
- package/typings/node/index.d.ts +2 -1
- package/typings/node/index.d.ts.map +1 -1
- package/typings/node/node.d.ts +55 -10
- package/typings/node/node.d.ts.map +1 -1
- package/typings/node/types.d.ts +101 -17
- package/typings/node/types.d.ts.map +1 -1
- package/typings/node/validators.d.ts +21 -0
- package/typings/node/validators.d.ts.map +1 -1
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.verifyFingerprintSignature = exports.signFingerprint = exports.digestFingerprint = exports.fingerprintBytes = exports.rawToPrivateKey = exports.rawPrivateKey = exports.rawToPublicKey = exports.rawPublicKey = exports.mnemonicToPrivateKey = exports.generateMnemonicKey = exports.generateKey = void 0;
|
|
39
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
40
|
+
const util_1 = require("util");
|
|
41
|
+
const bip39 = __importStar(require("@scure/bip39"));
|
|
42
|
+
const english_1 = require("@scure/bip39/wordlists/english");
|
|
43
|
+
const fingerprint_1 = require("./fingerprint");
|
|
44
|
+
const EMPTY_KEY = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
|
45
|
+
const CURVE_FIELD = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
|
|
46
|
+
/**
|
|
47
|
+
* Generate a pair of cryptographic keys.
|
|
48
|
+
*
|
|
49
|
+
* @return {Promise<crypto.KeyPairKeyObjectResult>} the keys
|
|
50
|
+
*/
|
|
51
|
+
function generateKey() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
return (0, util_1.promisify)(crypto_1.default.generateKeyPair)("ec", { namedCurve: "secp256k1" });
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
exports.generateKey = generateKey;
|
|
57
|
+
/**
|
|
58
|
+
* Generate a private cryptographic key with a mnemonic.
|
|
59
|
+
*
|
|
60
|
+
* @return {Promise<[string, crypto.KeyObject]>} the mnemonic and the key
|
|
61
|
+
*/
|
|
62
|
+
function generateMnemonicKey() {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const mnemonic = bip39.generateMnemonic(english_1.wordlist, 256);
|
|
65
|
+
const privateKey = yield mnemonicToPrivateKey(mnemonic);
|
|
66
|
+
return [mnemonic, privateKey];
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
exports.generateMnemonicKey = generateMnemonicKey;
|
|
70
|
+
/**
|
|
71
|
+
* Restore a private key from the given mnemonic.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} mnemonic - the mnemonic
|
|
74
|
+
* @return {Promise<crypto.KeyObject>} the private key
|
|
75
|
+
*/
|
|
76
|
+
function mnemonicToPrivateKey(mnemonic) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const seed = yield bip39.mnemonicToSeed(mnemonic);
|
|
79
|
+
let seedValue = BigInt(0);
|
|
80
|
+
for (let i = 0; i < seed.length; i++) {
|
|
81
|
+
seedValue = (seedValue << BigInt(8)) + BigInt(seed[i]);
|
|
82
|
+
}
|
|
83
|
+
const dValue = (seedValue % CURVE_FIELD).toString(16).padStart(64, "0");
|
|
84
|
+
const d = Buffer.alloc(32);
|
|
85
|
+
for (let i = 0; i < 32; i++) {
|
|
86
|
+
d.writeUint8(parseInt(dValue.substring(i * 2, 2), 16), i);
|
|
87
|
+
}
|
|
88
|
+
return crypto_1.default.createPrivateKey({
|
|
89
|
+
format: "jwk",
|
|
90
|
+
key: {
|
|
91
|
+
kty: "EC",
|
|
92
|
+
d: d.toString("base64url"),
|
|
93
|
+
crv: "secp256k1"
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
exports.mnemonicToPrivateKey = mnemonicToPrivateKey;
|
|
99
|
+
/**
|
|
100
|
+
* Convert a public key to the raw format used by the naming server.
|
|
101
|
+
*
|
|
102
|
+
* @param {crypto.KeyObject} publicKey - the public key
|
|
103
|
+
* @return {Buffer} the raw public key
|
|
104
|
+
*/
|
|
105
|
+
function rawPublicKey(publicKey) {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
const jwk = publicKey.export({ format: "jwk" });
|
|
108
|
+
return Buffer.concat([
|
|
109
|
+
Buffer.from((_a = jwk.x) !== null && _a !== void 0 ? _a : EMPTY_KEY, "base64url"),
|
|
110
|
+
Buffer.from((_b = jwk.y) !== null && _b !== void 0 ? _b : EMPTY_KEY, "base64url")
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
exports.rawPublicKey = rawPublicKey;
|
|
114
|
+
/**
|
|
115
|
+
* Restore a public key from the raw format.
|
|
116
|
+
*
|
|
117
|
+
* @param {Buffer} rawPublicKey - the raw public key
|
|
118
|
+
* @return {crypto.KeyObject} the public key
|
|
119
|
+
*/
|
|
120
|
+
function rawToPublicKey(rawPublicKey) {
|
|
121
|
+
const x = rawPublicKey.subarray(0, 32).toString("base64url");
|
|
122
|
+
const y = rawPublicKey.subarray(32, 64).toString("base64url");
|
|
123
|
+
return crypto_1.default.createPublicKey({
|
|
124
|
+
format: "jwk",
|
|
125
|
+
key: {
|
|
126
|
+
kty: "EC",
|
|
127
|
+
x,
|
|
128
|
+
y,
|
|
129
|
+
crv: "secp256k1"
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
exports.rawToPublicKey = rawToPublicKey;
|
|
134
|
+
/**
|
|
135
|
+
* Convert a private key to the raw format to pass to the client.
|
|
136
|
+
*
|
|
137
|
+
* @param {crypto.KeyObject} privateKey - the private key
|
|
138
|
+
* @return {Buffer} the raw private key
|
|
139
|
+
*/
|
|
140
|
+
function rawPrivateKey(privateKey) {
|
|
141
|
+
var _a;
|
|
142
|
+
const jwk = privateKey.export({ format: "jwk" });
|
|
143
|
+
return Buffer.from((_a = jwk.d) !== null && _a !== void 0 ? _a : EMPTY_KEY, "base64url");
|
|
144
|
+
}
|
|
145
|
+
exports.rawPrivateKey = rawPrivateKey;
|
|
146
|
+
/**
|
|
147
|
+
* Restore a private key from the raw format.
|
|
148
|
+
*
|
|
149
|
+
* @param {Buffer} rawPrivateKey - the raw private key
|
|
150
|
+
* @return {crypto.KeyObject} the private key
|
|
151
|
+
*/
|
|
152
|
+
function rawToPrivateKey(rawPrivateKey) {
|
|
153
|
+
const d = rawPrivateKey.toString("base64url");
|
|
154
|
+
return crypto_1.default.createPrivateKey({
|
|
155
|
+
format: "jwk",
|
|
156
|
+
key: {
|
|
157
|
+
kty: "EC",
|
|
158
|
+
// x and y here are placeholders
|
|
159
|
+
x: 'xJrw0U2Qb1xyoxpfwCYVwCZakhd-LbjeBvLLNGAPTEU',
|
|
160
|
+
y: 'INp-PvmYleX19zhuXbwfnIcZO9a8RSuK7r-_4jneDGM',
|
|
161
|
+
d,
|
|
162
|
+
crv: "secp256k1"
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
exports.rawToPrivateKey = rawToPrivateKey;
|
|
167
|
+
/**
|
|
168
|
+
* Encode a fingerprint in the binary form, using the given fingerprint data and schema.
|
|
169
|
+
*
|
|
170
|
+
* @param {Fingerprint} fingerprint - the fingerprint data
|
|
171
|
+
* @param {FingerprintSchema} schema - the fingerprint schema
|
|
172
|
+
* @return {Buffer} the fingerprint in the binary form
|
|
173
|
+
*/
|
|
174
|
+
function fingerprintBytes(fingerprint, schema) {
|
|
175
|
+
const fingerprintWriter = new fingerprint_1.FingerprintWriter();
|
|
176
|
+
fingerprintWriter.append(fingerprint, schema);
|
|
177
|
+
return fingerprintWriter.toBytes();
|
|
178
|
+
}
|
|
179
|
+
exports.fingerprintBytes = fingerprintBytes;
|
|
180
|
+
/**
|
|
181
|
+
* Calculate a cryptographic digest of the fingerprint.
|
|
182
|
+
*
|
|
183
|
+
* @param {Buffer} fingerprint - the fingerprint
|
|
184
|
+
* @return {Buffer} the digest
|
|
185
|
+
*/
|
|
186
|
+
function digestFingerprint(fingerprint) {
|
|
187
|
+
const digest = crypto_1.default.createHash("sha3-256");
|
|
188
|
+
digest.update(fingerprint);
|
|
189
|
+
return digest.digest();
|
|
190
|
+
}
|
|
191
|
+
exports.digestFingerprint = digestFingerprint;
|
|
192
|
+
/**
|
|
193
|
+
* Sign a fingerprint with a private key.
|
|
194
|
+
*
|
|
195
|
+
* @param {Buffer} fingerprint - the fingerprint to be signed
|
|
196
|
+
* @param {crypto.KeyObject} privateKey - the private key
|
|
197
|
+
* @return {Buffer} the signature
|
|
198
|
+
*/
|
|
199
|
+
function signFingerprint(fingerprint, privateKey) {
|
|
200
|
+
const sign = crypto_1.default.createSign("SHA3-256");
|
|
201
|
+
sign.update(fingerprint);
|
|
202
|
+
return sign.sign(privateKey);
|
|
203
|
+
}
|
|
204
|
+
exports.signFingerprint = signFingerprint;
|
|
205
|
+
/**
|
|
206
|
+
* Verify a fingerprint signature with the given public key.
|
|
207
|
+
*
|
|
208
|
+
* @param {Buffer} fingerprint - the original fingerprint
|
|
209
|
+
* @param {Buffer} signature - the signature to be verified
|
|
210
|
+
* @param {crypto.KeyObject} publicKey - the public key for verification
|
|
211
|
+
* @return {boolean} `true`, if the signature is correct, `false` otherwise
|
|
212
|
+
*/
|
|
213
|
+
function verifyFingerprintSignature(fingerprint, signature, publicKey) {
|
|
214
|
+
const verify = crypto_1.default.createVerify("SHA3-256");
|
|
215
|
+
verify.update(fingerprint);
|
|
216
|
+
return verify.verify(publicKey, signature);
|
|
217
|
+
}
|
|
218
|
+
exports.verifyFingerprintSignature = verifyFingerprintSignature;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FingerprintWriter = void 0;
|
|
4
|
+
class FingerprintWriter {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.data = [];
|
|
7
|
+
}
|
|
8
|
+
appendNull() {
|
|
9
|
+
this.data.push(0xff);
|
|
10
|
+
}
|
|
11
|
+
appendString(str) {
|
|
12
|
+
if (str == null) {
|
|
13
|
+
this.appendNull();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const buf = Buffer.from(str);
|
|
17
|
+
this.appendNumber(buf.length);
|
|
18
|
+
this.data.push(...buf.values());
|
|
19
|
+
}
|
|
20
|
+
appendBoolean(b) {
|
|
21
|
+
this.data.push(b ? 1 : 0);
|
|
22
|
+
}
|
|
23
|
+
appendNumber(l) {
|
|
24
|
+
let len;
|
|
25
|
+
if (l < 0xfc) {
|
|
26
|
+
len = 1;
|
|
27
|
+
}
|
|
28
|
+
else if (l <= 0xffff) {
|
|
29
|
+
this.data.push(0xfc);
|
|
30
|
+
len = 2;
|
|
31
|
+
}
|
|
32
|
+
else if (l <= 0xffffffff) {
|
|
33
|
+
this.data.push(0xfd);
|
|
34
|
+
len = 4;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.data.push(0xfe);
|
|
38
|
+
len = 8;
|
|
39
|
+
}
|
|
40
|
+
for (let i = 0; i < len; i++) {
|
|
41
|
+
this.data.push(l & 0xff);
|
|
42
|
+
l = l >> 8;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
appendBytes(b) {
|
|
46
|
+
this.appendNumber(b.length);
|
|
47
|
+
this.data.push(...b.values());
|
|
48
|
+
}
|
|
49
|
+
appendFingerprint(fingerprint, schema) {
|
|
50
|
+
for (const field of schema) {
|
|
51
|
+
this.append(fingerprint[field[0]], field[1]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
appendList(list, type) {
|
|
55
|
+
const writer = new FingerprintWriter();
|
|
56
|
+
list.forEach(value => writer.append(value, type));
|
|
57
|
+
this.appendBytes(writer.toBytes());
|
|
58
|
+
}
|
|
59
|
+
append(value, type) {
|
|
60
|
+
if (value == null) {
|
|
61
|
+
this.appendNull();
|
|
62
|
+
}
|
|
63
|
+
else if (Array.isArray(type)) {
|
|
64
|
+
this.appendFingerprint(value, type);
|
|
65
|
+
}
|
|
66
|
+
else if (type.endsWith("[]")) {
|
|
67
|
+
this.appendList(value, type.substring(0, type.length - 2));
|
|
68
|
+
}
|
|
69
|
+
else if (type === "string") {
|
|
70
|
+
this.appendString(value);
|
|
71
|
+
}
|
|
72
|
+
else if (type === "boolean") {
|
|
73
|
+
this.appendBoolean(value);
|
|
74
|
+
}
|
|
75
|
+
else if (type === "number") {
|
|
76
|
+
this.appendNumber(value);
|
|
77
|
+
}
|
|
78
|
+
else if (type === "bytes") {
|
|
79
|
+
this.appendBytes(value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
toBytes() {
|
|
83
|
+
return Buffer.from(this.data);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.FingerprintWriter = FingerprintWriter;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifyFingerprintSignature = exports.signFingerprint = exports.digestFingerprint = exports.rawToPublicKey = exports.rawToPrivateKey = exports.rawPublicKey = exports.rawPrivateKey = exports.mnemonicToPrivateKey = exports.generateMnemonicKey = exports.generateKey = void 0;
|
|
4
|
+
var crypto_1 = require("./crypto");
|
|
5
|
+
Object.defineProperty(exports, "generateKey", { enumerable: true, get: function () { return crypto_1.generateKey; } });
|
|
6
|
+
Object.defineProperty(exports, "generateMnemonicKey", { enumerable: true, get: function () { return crypto_1.generateMnemonicKey; } });
|
|
7
|
+
Object.defineProperty(exports, "mnemonicToPrivateKey", { enumerable: true, get: function () { return crypto_1.mnemonicToPrivateKey; } });
|
|
8
|
+
Object.defineProperty(exports, "rawPrivateKey", { enumerable: true, get: function () { return crypto_1.rawPrivateKey; } });
|
|
9
|
+
Object.defineProperty(exports, "rawPublicKey", { enumerable: true, get: function () { return crypto_1.rawPublicKey; } });
|
|
10
|
+
Object.defineProperty(exports, "rawToPrivateKey", { enumerable: true, get: function () { return crypto_1.rawToPrivateKey; } });
|
|
11
|
+
Object.defineProperty(exports, "rawToPublicKey", { enumerable: true, get: function () { return crypto_1.rawToPublicKey; } });
|
|
12
|
+
Object.defineProperty(exports, "digestFingerprint", { enumerable: true, get: function () { return crypto_1.digestFingerprint; } });
|
|
13
|
+
Object.defineProperty(exports, "signFingerprint", { enumerable: true, get: function () { return crypto_1.signFingerprint; } });
|
|
14
|
+
Object.defineProperty(exports, "verifyFingerprintSignature", { enumerable: true, get: function () { return crypto_1.verifyFingerprintSignature; } });
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPutCallFingerprint0 = void 0;
|
|
4
|
+
const crypto_1 = require("../crypto/crypto");
|
|
5
|
+
const PUT_CALL_FINGERPRINT0_SCHEMA = [
|
|
6
|
+
["version", "number"],
|
|
7
|
+
["name", "string"],
|
|
8
|
+
["generation", "number"],
|
|
9
|
+
["updating_key", "bytes"],
|
|
10
|
+
["node_uri", "string"],
|
|
11
|
+
["signing_key", "bytes"],
|
|
12
|
+
["valid_from", "number"],
|
|
13
|
+
["previous_digest", "bytes"],
|
|
14
|
+
];
|
|
15
|
+
function createPutCallFingerprint0(name, generation, updatingKey, nodeUri, signingKey, validFrom, previousDigest) {
|
|
16
|
+
return (0, crypto_1.fingerprintBytes)({ "version": 0, name, generation, updatingKey, nodeUri, signingKey, validFrom, previousDigest }, PUT_CALL_FINGERPRINT0_SCHEMA);
|
|
17
|
+
}
|
|
18
|
+
exports.createPutCallFingerprint0 = createPutCallFingerprint0;
|
package/lib/naming/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.shorten = exports.resolve = exports.expand = exports.parseNodeName = exports.DEV_NAMING_SERVER = exports.MAIN_NAMING_SERVER = exports.MoeraNamingConnectionError = exports.MoeraNamingApiError = exports.MoeraNamingError = exports.MoeraNaming = void 0;
|
|
3
|
+
exports.validateNamingSchema = exports.shorten = exports.resolve = exports.expand = exports.parseNodeName = exports.DEV_NAMING_SERVER = exports.MAIN_NAMING_SERVER = exports.MoeraNamingConnectionError = exports.MoeraNamingApiError = exports.MoeraNamingError = exports.MoeraNaming = void 0;
|
|
4
4
|
var naming_1 = require("./naming");
|
|
5
5
|
Object.defineProperty(exports, "MoeraNaming", { enumerable: true, get: function () { return naming_1.MoeraNaming; } });
|
|
6
6
|
Object.defineProperty(exports, "MoeraNamingError", { enumerable: true, get: function () { return naming_1.MoeraNamingError; } });
|
|
@@ -12,3 +12,5 @@ Object.defineProperty(exports, "parseNodeName", { enumerable: true, get: functio
|
|
|
12
12
|
Object.defineProperty(exports, "expand", { enumerable: true, get: function () { return naming_1.expand; } });
|
|
13
13
|
Object.defineProperty(exports, "resolve", { enumerable: true, get: function () { return naming_1.resolve; } });
|
|
14
14
|
Object.defineProperty(exports, "shorten", { enumerable: true, get: function () { return naming_1.shorten; } });
|
|
15
|
+
var validate_1 = require("./validate");
|
|
16
|
+
Object.defineProperty(exports, "validateNamingSchema", { enumerable: true, get: function () { return validate_1.validateSchema; } });
|
package/lib/naming/naming.js
CHANGED
|
@@ -291,12 +291,6 @@ function parseNodeName(nodeName) {
|
|
|
291
291
|
return [name, generation];
|
|
292
292
|
}
|
|
293
293
|
exports.parseNodeName = parseNodeName;
|
|
294
|
-
/**
|
|
295
|
-
* Converts the node name to the compact form, omitting generation 0.
|
|
296
|
-
*
|
|
297
|
-
* @param {string | null} nodeName - the node name in compact or full form
|
|
298
|
-
* @return {string | null} the node name in the compact form
|
|
299
|
-
*/
|
|
300
294
|
function shorten(nodeName) {
|
|
301
295
|
if (nodeName === null) {
|
|
302
296
|
return null;
|
|
@@ -310,12 +304,6 @@ function shorten(nodeName) {
|
|
|
310
304
|
}
|
|
311
305
|
}
|
|
312
306
|
exports.shorten = shorten;
|
|
313
|
-
/**
|
|
314
|
-
* Converts the node name to the full form, containing generation.
|
|
315
|
-
*
|
|
316
|
-
* @param {string | null} nodeName - the node name in compact or full form
|
|
317
|
-
* @return {string | null} the node name in the full form
|
|
318
|
-
*/
|
|
319
307
|
function expand(nodeName) {
|
|
320
308
|
if (nodeName === null) {
|
|
321
309
|
return null;
|
package/lib/node/cartes.js
CHANGED
|
@@ -8,8 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.MoeraCarteSource = exports.MoeraCartesError = void 0;
|
|
15
|
+
exports.generateCarte = exports.MoeraCarteSource = exports.MoeraCartesError = void 0;
|
|
16
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
17
|
+
const util_1 = require("util");
|
|
18
|
+
const types_1 = require("./types");
|
|
19
|
+
const fingerprints_1 = require("./fingerprints");
|
|
20
|
+
const crypto_2 = require("../crypto");
|
|
13
21
|
/**
|
|
14
22
|
* Error obtaining valid cartes.
|
|
15
23
|
*/
|
|
@@ -22,26 +30,36 @@ class MoeraCartesError extends Error {
|
|
|
22
30
|
}
|
|
23
31
|
}
|
|
24
32
|
exports.MoeraCartesError = MoeraCartesError;
|
|
25
|
-
function isAdminCarte(carte) {
|
|
26
|
-
return carte.permissions == null || carte.permissions.includes("other");
|
|
27
|
-
}
|
|
28
33
|
/**
|
|
29
34
|
* Class that gets cartes from the given node, caches them and supplies them for authentication.
|
|
30
35
|
*/
|
|
31
36
|
class MoeraCarteSource {
|
|
32
37
|
/**
|
|
33
38
|
* @param {MoeraNode} node node to get cartes from
|
|
39
|
+
* @param {Scope[] | null} clientScope permissions to be granted to the cartes; if not set, all permissions of
|
|
40
|
+
* the cartes' owner are granted
|
|
41
|
+
* @param {Scope[] | null} adminScope additional administrative permissions (of those granted to the cartes' owner
|
|
42
|
+
* by the target node) to be granted to the cartes
|
|
43
|
+
* @param {string | null} targetNodeName if set, the cartes are valid for authentication on the specified node only
|
|
34
44
|
*/
|
|
35
|
-
constructor(node) {
|
|
45
|
+
constructor(node, clientScope = null, adminScope = null, targetNodeName = null) {
|
|
36
46
|
this.cartes = [];
|
|
37
47
|
this.node = node;
|
|
48
|
+
this.clientScope = clientScope !== null && clientScope !== void 0 ? clientScope : ["all"];
|
|
49
|
+
this.adminScope = adminScope !== null && adminScope !== void 0 ? adminScope : [];
|
|
50
|
+
this.targetNodeName = targetNodeName;
|
|
38
51
|
}
|
|
39
52
|
/**
|
|
40
53
|
* Force renewing the cached list of cartes.
|
|
41
54
|
*/
|
|
42
55
|
renew() {
|
|
43
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
|
|
57
|
+
const attributes = {
|
|
58
|
+
clientScope: this.clientScope,
|
|
59
|
+
adminScope: this.adminScope,
|
|
60
|
+
nodeName: this.targetNodeName
|
|
61
|
+
};
|
|
62
|
+
this.cartes = (yield this.node.createCartes(attributes)).cartes;
|
|
45
63
|
});
|
|
46
64
|
}
|
|
47
65
|
/**
|
|
@@ -53,7 +71,7 @@ class MoeraCarteSource {
|
|
|
53
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
72
|
for (const renewed of [false, true]) {
|
|
55
73
|
const now = Math.floor(Date.now() / 1000);
|
|
56
|
-
this.cartes = this.cartes.filter(c => c.deadline > now
|
|
74
|
+
this.cartes = this.cartes.filter(c => c.deadline > now);
|
|
57
75
|
if (this.cartes.length === 0) {
|
|
58
76
|
if (renewed) {
|
|
59
77
|
throw new MoeraCartesError("Could not obtain a valid carte from the node");
|
|
@@ -73,3 +91,34 @@ class MoeraCarteSource {
|
|
|
73
91
|
}
|
|
74
92
|
}
|
|
75
93
|
exports.MoeraCarteSource = MoeraCarteSource;
|
|
94
|
+
function toScopeMask(scope) {
|
|
95
|
+
let mask = 0;
|
|
96
|
+
for (const sc of scope) {
|
|
97
|
+
mask |= types_1.SCOPE_VALUES[sc];
|
|
98
|
+
}
|
|
99
|
+
return mask;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Generate a carte with the given parameters and sign it with the provided private signing key.
|
|
103
|
+
*
|
|
104
|
+
* @param {string | null} ownerName - name of the node authenticating with the carte
|
|
105
|
+
* @param {crypto.KeyObject} signingKey - the private signing key to sign the carte
|
|
106
|
+
* @param {number} beginning - timestamp of the beginning of the carte's life
|
|
107
|
+
* @param {GenerateCarteOptions} options - carte options
|
|
108
|
+
* @return {Promise<string>} the carte
|
|
109
|
+
*/
|
|
110
|
+
function generateCarte(ownerName_1, signingKey_1, beginning_1) {
|
|
111
|
+
return __awaiter(this, arguments, void 0, function* (ownerName, signingKey, beginning, { ttl = 600, address = null, nodeName = null, clientScope = types_1.SCOPE_VALUES["all"], adminScope = 0 } = {}) {
|
|
112
|
+
if (Array.isArray(clientScope)) {
|
|
113
|
+
clientScope = toScopeMask(clientScope);
|
|
114
|
+
}
|
|
115
|
+
if (Array.isArray(adminScope)) {
|
|
116
|
+
adminScope = toScopeMask(adminScope);
|
|
117
|
+
}
|
|
118
|
+
const salt = yield (0, util_1.promisify)(crypto_1.default.randomBytes)(8);
|
|
119
|
+
const fingerprint = (0, fingerprints_1.createCarteFingerprint2)(ownerName, address, beginning, beginning + ttl, nodeName, clientScope, adminScope, salt);
|
|
120
|
+
const signature = (0, crypto_2.signFingerprint)(fingerprint, signingKey);
|
|
121
|
+
return Buffer.concat([fingerprint, signature]).toString("base64url");
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
exports.generateCarte = generateCarte;
|