@astrox/identity 0.0.16 → 0.0.24
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/package.json +2 -2
- package/lib/cjs/buffer.d.ts +0 -10
- package/lib/cjs/buffer.js +0 -21
- package/lib/cjs/buffer.js.map +0 -1
- package/lib/cjs/identity/delegation.d.ts +0 -131
- package/lib/cjs/identity/delegation.js +0 -244
- package/lib/cjs/identity/delegation.js.map +0 -1
- package/lib/cjs/identity/der.d.ts +0 -26
- package/lib/cjs/identity/der.js +0 -145
- package/lib/cjs/identity/der.js.map +0 -1
- package/lib/cjs/identity/ed25519.d.ts +0 -45
- package/lib/cjs/identity/ed25519.js +0 -134
- package/lib/cjs/identity/ed25519.js.map +0 -1
- package/lib/cjs/identity/webauthn.d.ts +0 -40
- package/lib/cjs/identity/webauthn.js +0 -205
- package/lib/cjs/identity/webauthn.js.map +0 -1
- package/lib/cjs/index.d.ts +0 -4
- package/lib/cjs/index.js +0 -17
- package/lib/cjs/index.js.map +0 -1
- package/lib/esm/buffer.d.ts +0 -10
- package/lib/esm/buffer.js +0 -16
- package/lib/esm/buffer.js.map +0 -1
- package/lib/esm/identity/delegation.d.ts +0 -131
- package/lib/esm/identity/delegation.js +0 -219
- package/lib/esm/identity/delegation.js.map +0 -1
- package/lib/esm/identity/der.d.ts +0 -35
- package/lib/esm/identity/der.js +0 -168
- package/lib/esm/identity/der.js.map +0 -1
- package/lib/esm/identity/ed25519.d.ts +0 -45
- package/lib/esm/identity/ed25519.js +0 -110
- package/lib/esm/identity/ed25519.js.map +0 -1
- package/lib/esm/identity/secp256k1.d.ts +0 -73
- package/lib/esm/identity/secp256k1.js +0 -148
- package/lib/esm/identity/secp256k1.js.map +0 -1
- package/lib/esm/identity/webauthn.d.ts +0 -40
- package/lib/esm/identity/webauthn.js +0 -178
- package/lib/esm/identity/webauthn.js.map +0 -1
- package/lib/esm/index.d.ts +0 -4
- package/lib/esm/index.js +0 -5
- package/lib/esm/index.js.map +0 -1
- package/lib/tsconfig-cjs.tsbuildinfo +0 -1744
- package/lib/tsconfig.tsbuildinfo +0 -2508
@@ -1,219 +0,0 @@
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
2
|
-
var t = {};
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
4
|
-
t[p] = s[p];
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
8
|
-
t[p[i]] = s[p[i]];
|
9
|
-
}
|
10
|
-
return t;
|
11
|
-
};
|
12
|
-
import { requestIdOf, SignIdentity, } from '@astrox/agent';
|
13
|
-
import { Principal } from '@astrox/principal';
|
14
|
-
import * as cbor from 'simple-cbor';
|
15
|
-
import { fromHexString, toHexString } from '../buffer';
|
16
|
-
const domainSeparator = new TextEncoder().encode('\x1Aic-request-auth-delegation');
|
17
|
-
const requestDomainSeparator = new TextEncoder().encode('\x0Aic-request');
|
18
|
-
function _parseBlob(value) {
|
19
|
-
if (typeof value !== 'string' || value.length < 64) {
|
20
|
-
throw new Error('Invalid public key.');
|
21
|
-
}
|
22
|
-
return fromHexString(value);
|
23
|
-
}
|
24
|
-
/**
|
25
|
-
* A single delegation object that is signed by a private key. This is constructed by
|
26
|
-
* `DelegationChain.create()`.
|
27
|
-
*
|
28
|
-
* {@see DelegationChain}
|
29
|
-
*/
|
30
|
-
export class Delegation {
|
31
|
-
constructor(pubkey, expiration, targets) {
|
32
|
-
this.pubkey = pubkey;
|
33
|
-
this.expiration = expiration;
|
34
|
-
this.targets = targets;
|
35
|
-
}
|
36
|
-
toCBOR() {
|
37
|
-
// Expiration field needs to be encoded as a u64 specifically.
|
38
|
-
return cbor.value.map(Object.assign({ pubkey: cbor.value.bytes(this.pubkey), expiration: cbor.value.u64(this.expiration.toString(16), 16) }, (this.targets && {
|
39
|
-
targets: cbor.value.array(this.targets.map(t => cbor.value.bytes(t.toUint8Array()))),
|
40
|
-
})));
|
41
|
-
}
|
42
|
-
toJSON() {
|
43
|
-
// every string should be hex and once-de-hexed,
|
44
|
-
// discoverable what it is (e.g. de-hex to get JSON with a 'type' property, or de-hex to DER
|
45
|
-
// with an OID). After de-hex, if it's not obvious what it is, it's an ArrayBuffer.
|
46
|
-
return Object.assign({ expiration: this.expiration.toString(16), pubkey: toHexString(this.pubkey) }, (this.targets && { targets: this.targets.map(p => p.toHex()) }));
|
47
|
-
}
|
48
|
-
}
|
49
|
-
/**
|
50
|
-
* Sign a single delegation object for a period of time.
|
51
|
-
*
|
52
|
-
* @param from The identity that lends its delegation.
|
53
|
-
* @param to The identity that receives the delegation.
|
54
|
-
* @param expiration An expiration date for this delegation.
|
55
|
-
* @param targets Limit this delegation to the target principals.
|
56
|
-
*/
|
57
|
-
async function _createSingleDelegation(from, to, expiration, targets) {
|
58
|
-
const delegation = new Delegation(to.toDer(), BigInt(+expiration) * BigInt(1000000), // In nanoseconds.
|
59
|
-
targets);
|
60
|
-
// The signature is calculated by signing the concatenation of the domain separator
|
61
|
-
// and the message.
|
62
|
-
// Note: To ensure Safari treats this as a user gesture, ensure to not use async methods
|
63
|
-
// besides the actualy webauthn functionality (such as `sign`). Safari will de-register
|
64
|
-
// a user gesture if you await an async call thats not fetch, xhr, or setTimeout.
|
65
|
-
const challenge = new Uint8Array([
|
66
|
-
...domainSeparator,
|
67
|
-
...new Uint8Array(requestIdOf(delegation)),
|
68
|
-
]);
|
69
|
-
const signature = await from.sign(challenge);
|
70
|
-
return {
|
71
|
-
delegation,
|
72
|
-
signature,
|
73
|
-
};
|
74
|
-
}
|
75
|
-
/**
|
76
|
-
* A chain of delegations. This is JSON Serializable.
|
77
|
-
* This is the object to serialize and pass to a DelegationIdentity. It does not keep any
|
78
|
-
* private keys.
|
79
|
-
*/
|
80
|
-
export class DelegationChain {
|
81
|
-
constructor(delegations, publicKey) {
|
82
|
-
this.delegations = delegations;
|
83
|
-
this.publicKey = publicKey;
|
84
|
-
}
|
85
|
-
/**
|
86
|
-
* Create a delegation chain between two (or more) keys. By default, the expiration time
|
87
|
-
* will be very short (15 minutes).
|
88
|
-
*
|
89
|
-
* To build a chain of more than 2 identities, this function needs to be called multiple times,
|
90
|
-
* passing the previous delegation chain into the options argument. For example:
|
91
|
-
*
|
92
|
-
* @example
|
93
|
-
* const rootKey = createKey();
|
94
|
-
* const middleKey = createKey();
|
95
|
-
* const bottomeKey = createKey();
|
96
|
-
*
|
97
|
-
* const rootToMiddle = await DelegationChain.create(
|
98
|
-
* root, middle.getPublicKey(), Date.parse('2100-01-01'),
|
99
|
-
* );
|
100
|
-
* const middleToBottom = await DelegationChain.create(
|
101
|
-
* middle, bottom.getPublicKey(), Date.parse('2100-01-01'), { previous: rootToMiddle },
|
102
|
-
* );
|
103
|
-
*
|
104
|
-
* // We can now use a delegation identity that uses the delegation above:
|
105
|
-
* const identity = DelegationIdentity.fromDelegation(bottomKey, middleToBottom);
|
106
|
-
*
|
107
|
-
* @param from The identity that will delegate.
|
108
|
-
* @param to The identity that gets delegated. It can now sign messages as if it was the
|
109
|
-
* identity above.
|
110
|
-
* @param expiration The length the delegation is valid. By default, 15 minutes from calling
|
111
|
-
* this function.
|
112
|
-
* @param options A set of options for this delegation. expiration and previous
|
113
|
-
* @param options.previous - Another DelegationChain that this chain should start with.
|
114
|
-
* @param options.targets - targets that scope the delegation (e.g. Canister Principals)
|
115
|
-
*/
|
116
|
-
static async create(from, to, expiration = new Date(Date.now() + 15 * 60 * 1000), options = {}) {
|
117
|
-
var _a, _b;
|
118
|
-
const delegation = await _createSingleDelegation(from, to, expiration, options.targets);
|
119
|
-
return new DelegationChain([...(((_a = options.previous) === null || _a === void 0 ? void 0 : _a.delegations) || []), delegation], ((_b = options.previous) === null || _b === void 0 ? void 0 : _b.publicKey) || from.getPublicKey().toDer());
|
120
|
-
}
|
121
|
-
/**
|
122
|
-
* Creates a DelegationChain object from a JSON string.
|
123
|
-
*
|
124
|
-
* @param json The JSON string to parse.
|
125
|
-
*/
|
126
|
-
static fromJSON(json) {
|
127
|
-
const { publicKey, delegations } = typeof json === 'string' ? JSON.parse(json) : json;
|
128
|
-
if (!Array.isArray(delegations)) {
|
129
|
-
throw new Error('Invalid delegations.');
|
130
|
-
}
|
131
|
-
const parsedDelegations = delegations.map(signedDelegation => {
|
132
|
-
const { delegation, signature } = signedDelegation;
|
133
|
-
const { pubkey, expiration, targets } = delegation;
|
134
|
-
if (targets !== undefined && !Array.isArray(targets)) {
|
135
|
-
throw new Error('Invalid targets.');
|
136
|
-
}
|
137
|
-
return {
|
138
|
-
delegation: new Delegation(_parseBlob(pubkey), BigInt(`0x${expiration}`), // expiration in JSON is an hexa string (See toJSON() below).
|
139
|
-
targets &&
|
140
|
-
targets.map((t) => {
|
141
|
-
if (typeof t !== 'string') {
|
142
|
-
throw new Error('Invalid target.');
|
143
|
-
}
|
144
|
-
return Principal.fromHex(t);
|
145
|
-
})),
|
146
|
-
signature: _parseBlob(signature),
|
147
|
-
};
|
148
|
-
});
|
149
|
-
return new this(parsedDelegations, _parseBlob(publicKey));
|
150
|
-
}
|
151
|
-
/**
|
152
|
-
* Creates a DelegationChain object from a list of delegations and a DER-encoded public key.
|
153
|
-
*
|
154
|
-
* @param delegations The list of delegations.
|
155
|
-
* @param publicKey The DER-encoded public key of the key-pair signing the first delegation.
|
156
|
-
*/
|
157
|
-
static fromDelegations(delegations, publicKey) {
|
158
|
-
return new this(delegations, publicKey);
|
159
|
-
}
|
160
|
-
toJSON() {
|
161
|
-
return {
|
162
|
-
delegations: this.delegations.map(signedDelegation => {
|
163
|
-
const { delegation, signature } = signedDelegation;
|
164
|
-
const { targets } = delegation;
|
165
|
-
return {
|
166
|
-
delegation: Object.assign({ expiration: delegation.expiration.toString(16), pubkey: toHexString(delegation.pubkey) }, (targets && {
|
167
|
-
targets: targets.map(t => t.toHex()),
|
168
|
-
})),
|
169
|
-
signature: toHexString(signature),
|
170
|
-
};
|
171
|
-
}),
|
172
|
-
publicKey: toHexString(this.publicKey),
|
173
|
-
};
|
174
|
-
}
|
175
|
-
}
|
176
|
-
/**
|
177
|
-
* An Identity that adds delegation to a request. Everywhere in this class, the name
|
178
|
-
* innerKey refers to the SignIdentity that is being used to sign the requests, while
|
179
|
-
* originalKey is the identity that is being borrowed. More identities can be used
|
180
|
-
* in the middle to delegate.
|
181
|
-
*/
|
182
|
-
export class DelegationIdentity extends SignIdentity {
|
183
|
-
constructor(_inner, _delegation) {
|
184
|
-
super();
|
185
|
-
this._inner = _inner;
|
186
|
-
this._delegation = _delegation;
|
187
|
-
}
|
188
|
-
/**
|
189
|
-
* Create a delegation without having access to delegateKey.
|
190
|
-
*
|
191
|
-
* @param key The key used to sign the reqyests.
|
192
|
-
* @param delegation A delegation object created using `createDelegation`.
|
193
|
-
*/
|
194
|
-
static fromDelegation(key, delegation) {
|
195
|
-
return new this(key, delegation);
|
196
|
-
}
|
197
|
-
getDelegation() {
|
198
|
-
return this._delegation;
|
199
|
-
}
|
200
|
-
getPublicKey() {
|
201
|
-
return {
|
202
|
-
toDer: () => this._delegation.publicKey,
|
203
|
-
};
|
204
|
-
}
|
205
|
-
sign(blob) {
|
206
|
-
return this._inner.sign(blob);
|
207
|
-
}
|
208
|
-
async transformRequest(request) {
|
209
|
-
const { body } = request, fields = __rest(request, ["body"]);
|
210
|
-
const requestId = await requestIdOf(body);
|
211
|
-
return Object.assign(Object.assign({}, fields), { body: {
|
212
|
-
content: body,
|
213
|
-
sender_sig: await this.sign(new Uint8Array([...requestDomainSeparator, ...new Uint8Array(requestId)])),
|
214
|
-
sender_delegation: this._delegation.delegations,
|
215
|
-
sender_pubkey: this._delegation.publicKey,
|
216
|
-
} });
|
217
|
-
}
|
218
|
-
}
|
219
|
-
//# sourceMappingURL=delegation.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":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,WAAW,CAAC,WAAW;gBAC/C,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,SAAS;aAC1C,IACD;IACJ,CAAC;CACF"}
|
@@ -1,35 +0,0 @@
|
|
1
|
-
export declare const bufEquals: (b1: ArrayBuffer, b2: ArrayBuffer) => boolean;
|
2
|
-
export declare const encodeLenBytes: (len: number) => number;
|
3
|
-
export declare const encodeLen: (buf: Uint8Array, offset: number, len: number) => number;
|
4
|
-
export declare const decodeLenBytes: (buf: Uint8Array, offset: number) => number;
|
5
|
-
export declare const decodeLen: (buf: Uint8Array, offset: number) => number;
|
6
|
-
/**
|
7
|
-
* A DER encoded `SEQUENCE(OID)` for DER-encoded-COSE
|
8
|
-
*/
|
9
|
-
export declare const DER_COSE_OID: Uint8Array;
|
10
|
-
/**
|
11
|
-
* A DER encoded `SEQUENCE(OID)` for the Ed25519 algorithm
|
12
|
-
*/
|
13
|
-
export declare const ED25519_OID: Uint8Array;
|
14
|
-
/**
|
15
|
-
* A DER encoded `SEQUENCE(OID)` for secp256k1 with the ECDSA algorithm
|
16
|
-
*/
|
17
|
-
export declare const SECP256K1_OID: Uint8Array;
|
18
|
-
/**
|
19
|
-
* Wraps the given `payload` in a DER encoding tagged with the given encoded `oid` like so:
|
20
|
-
* `SEQUENCE(oid, BITSTRING(payload))`
|
21
|
-
*
|
22
|
-
* @param payload The payload to encode as the bit string
|
23
|
-
* @param oid The DER encoded (and SEQUENCE wrapped!) OID to tag the payload with
|
24
|
-
*/
|
25
|
-
export declare function wrapDER(payload: ArrayBuffer, oid: Uint8Array): Uint8Array;
|
26
|
-
/**
|
27
|
-
* Extracts a payload from the given `derEncoded` data, and checks that it was tagged with the given `oid`.
|
28
|
-
*
|
29
|
-
* `derEncoded = SEQUENCE(oid, BITSTRING(payload))`
|
30
|
-
*
|
31
|
-
* @param derEncoded The DER encoded and tagged data
|
32
|
-
* @param oid The DER encoded (and SEQUENCE wrapped!) expected OID
|
33
|
-
* @returns The unwrapped payload
|
34
|
-
*/
|
35
|
-
export declare const unwrapDER: (derEncoded: ArrayBuffer, oid: Uint8Array) => Uint8Array;
|
package/lib/esm/identity/der.js
DELETED
@@ -1,168 +0,0 @@
|
|
1
|
-
export const bufEquals = (b1, b2) => {
|
2
|
-
if (b1.byteLength !== b2.byteLength)
|
3
|
-
return false;
|
4
|
-
const u1 = new Uint8Array(b1);
|
5
|
-
const u2 = new Uint8Array(b2);
|
6
|
-
for (let i = 0; i < u1.length; i++) {
|
7
|
-
if (u1[i] !== u2[i])
|
8
|
-
return false;
|
9
|
-
}
|
10
|
-
return true;
|
11
|
-
};
|
12
|
-
export const encodeLenBytes = (len) => {
|
13
|
-
if (len <= 0x7f) {
|
14
|
-
return 1;
|
15
|
-
}
|
16
|
-
else if (len <= 0xff) {
|
17
|
-
return 2;
|
18
|
-
}
|
19
|
-
else if (len <= 0xffff) {
|
20
|
-
return 3;
|
21
|
-
}
|
22
|
-
else if (len <= 0xffffff) {
|
23
|
-
return 4;
|
24
|
-
}
|
25
|
-
else {
|
26
|
-
throw new Error('Length too long (> 4 bytes)');
|
27
|
-
}
|
28
|
-
};
|
29
|
-
export const encodeLen = (buf, offset, len) => {
|
30
|
-
if (len <= 0x7f) {
|
31
|
-
buf[offset] = len;
|
32
|
-
return 1;
|
33
|
-
}
|
34
|
-
else if (len <= 0xff) {
|
35
|
-
buf[offset] = 0x81;
|
36
|
-
buf[offset + 1] = len;
|
37
|
-
return 2;
|
38
|
-
}
|
39
|
-
else if (len <= 0xffff) {
|
40
|
-
buf[offset] = 0x82;
|
41
|
-
buf[offset + 1] = len >> 8;
|
42
|
-
buf[offset + 2] = len;
|
43
|
-
return 3;
|
44
|
-
}
|
45
|
-
else if (len <= 0xffffff) {
|
46
|
-
buf[offset] = 0x83;
|
47
|
-
buf[offset + 1] = len >> 16;
|
48
|
-
buf[offset + 2] = len >> 8;
|
49
|
-
buf[offset + 3] = len;
|
50
|
-
return 4;
|
51
|
-
}
|
52
|
-
else {
|
53
|
-
throw new Error('Length too long (> 4 bytes)');
|
54
|
-
}
|
55
|
-
};
|
56
|
-
export const decodeLenBytes = (buf, offset) => {
|
57
|
-
if (buf[offset] < 0x80)
|
58
|
-
return 1;
|
59
|
-
if (buf[offset] === 0x80)
|
60
|
-
throw new Error('Invalid length 0');
|
61
|
-
if (buf[offset] === 0x81)
|
62
|
-
return 2;
|
63
|
-
if (buf[offset] === 0x82)
|
64
|
-
return 3;
|
65
|
-
if (buf[offset] === 0x83)
|
66
|
-
return 4;
|
67
|
-
throw new Error('Length too long (> 4 bytes)');
|
68
|
-
};
|
69
|
-
export const decodeLen = (buf, offset) => {
|
70
|
-
const lenBytes = decodeLenBytes(buf, offset);
|
71
|
-
if (lenBytes === 1)
|
72
|
-
return buf[offset];
|
73
|
-
else if (lenBytes === 2)
|
74
|
-
return buf[offset + 1];
|
75
|
-
else if (lenBytes === 3)
|
76
|
-
return (buf[offset + 1] << 8) + buf[offset + 2];
|
77
|
-
else if (lenBytes === 4)
|
78
|
-
return (buf[offset + 1] << 16) + (buf[offset + 2] << 8) + buf[offset + 3];
|
79
|
-
throw new Error('Length too long (> 4 bytes)');
|
80
|
-
};
|
81
|
-
/**
|
82
|
-
* A DER encoded `SEQUENCE(OID)` for DER-encoded-COSE
|
83
|
-
*/
|
84
|
-
export const DER_COSE_OID = Uint8Array.from([
|
85
|
-
...[0x30, 0x0c],
|
86
|
-
...[0x06, 0x0a],
|
87
|
-
...[0x2b, 0x06, 0x01, 0x04, 0x01, 0x83, 0xb8, 0x43, 0x01, 0x01], // DER encoded COSE
|
88
|
-
]);
|
89
|
-
/**
|
90
|
-
* A DER encoded `SEQUENCE(OID)` for the Ed25519 algorithm
|
91
|
-
*/
|
92
|
-
export const ED25519_OID = Uint8Array.from([
|
93
|
-
...[0x30, 0x05],
|
94
|
-
...[0x06, 0x03],
|
95
|
-
...[0x2b, 0x65, 0x70], // id-Ed25519 OID
|
96
|
-
]);
|
97
|
-
/**
|
98
|
-
* A DER encoded `SEQUENCE(OID)` for secp256k1 with the ECDSA algorithm
|
99
|
-
*/
|
100
|
-
export const SECP256K1_OID = Uint8Array.from([
|
101
|
-
...[0x30, 0x10],
|
102
|
-
...[0x06, 0x07],
|
103
|
-
...[0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01],
|
104
|
-
...[0x06, 0x05],
|
105
|
-
...[0x2b, 0x81, 0x04, 0x00, 0x0a], // OID secp256k1
|
106
|
-
]);
|
107
|
-
/**
|
108
|
-
* Wraps the given `payload` in a DER encoding tagged with the given encoded `oid` like so:
|
109
|
-
* `SEQUENCE(oid, BITSTRING(payload))`
|
110
|
-
*
|
111
|
-
* @param payload The payload to encode as the bit string
|
112
|
-
* @param oid The DER encoded (and SEQUENCE wrapped!) OID to tag the payload with
|
113
|
-
*/
|
114
|
-
export function wrapDER(payload, oid) {
|
115
|
-
// The Bit String header needs to include the unused bit count byte in its length
|
116
|
-
const bitStringHeaderLength = 2 + encodeLenBytes(payload.byteLength + 1);
|
117
|
-
const len = oid.byteLength + bitStringHeaderLength + payload.byteLength;
|
118
|
-
let offset = 0;
|
119
|
-
const buf = new Uint8Array(1 + encodeLenBytes(len) + len);
|
120
|
-
// Sequence
|
121
|
-
buf[offset++] = 0x30;
|
122
|
-
// Sequence Length
|
123
|
-
offset += encodeLen(buf, offset, len);
|
124
|
-
// OID
|
125
|
-
buf.set(oid, offset);
|
126
|
-
offset += oid.byteLength;
|
127
|
-
// Bit String Header
|
128
|
-
buf[offset++] = 0x03;
|
129
|
-
offset += encodeLen(buf, offset, payload.byteLength + 1);
|
130
|
-
// 0 padding
|
131
|
-
buf[offset++] = 0x00;
|
132
|
-
buf.set(new Uint8Array(payload), offset);
|
133
|
-
return buf;
|
134
|
-
}
|
135
|
-
/**
|
136
|
-
* Extracts a payload from the given `derEncoded` data, and checks that it was tagged with the given `oid`.
|
137
|
-
*
|
138
|
-
* `derEncoded = SEQUENCE(oid, BITSTRING(payload))`
|
139
|
-
*
|
140
|
-
* @param derEncoded The DER encoded and tagged data
|
141
|
-
* @param oid The DER encoded (and SEQUENCE wrapped!) expected OID
|
142
|
-
* @returns The unwrapped payload
|
143
|
-
*/
|
144
|
-
export const unwrapDER = (derEncoded, oid) => {
|
145
|
-
let offset = 0;
|
146
|
-
const expect = (n, msg) => {
|
147
|
-
if (buf[offset++] !== n) {
|
148
|
-
throw new Error('Expected: ' + msg);
|
149
|
-
}
|
150
|
-
};
|
151
|
-
const buf = new Uint8Array(derEncoded);
|
152
|
-
expect(0x30, 'sequence');
|
153
|
-
offset += decodeLenBytes(buf, offset);
|
154
|
-
if (!bufEquals(buf.slice(offset, offset + oid.byteLength), oid)) {
|
155
|
-
throw new Error('Not the expected OID.');
|
156
|
-
}
|
157
|
-
offset += oid.byteLength;
|
158
|
-
expect(0x03, 'bit string');
|
159
|
-
const payloadLen = decodeLen(buf, offset) - 1; // Subtracting 1 to account for the 0 padding
|
160
|
-
offset += decodeLenBytes(buf, offset);
|
161
|
-
expect(0x00, '0 padding');
|
162
|
-
const result = buf.slice(offset);
|
163
|
-
if (payloadLen !== result.length) {
|
164
|
-
throw new Error(`DER payload mismatch: Expected length ${payloadLen} actual length ${result.length}`);
|
165
|
-
}
|
166
|
-
return result;
|
167
|
-
};
|
168
|
-
//# sourceMappingURL=der.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"der.js","sourceRoot":"","sources":["../../../src/identity/der.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,EAAe,EAAE,EAAe,EAAW,EAAE;IACrE,IAAI,EAAE,CAAC,UAAU,KAAK,EAAE,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IAClD,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAA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
export declare class Ed25519PublicKey implements PublicKey {
|
3
|
-
static from(key: PublicKey): Ed25519PublicKey;
|
4
|
-
static fromRaw(rawKey: ArrayBuffer): Ed25519PublicKey;
|
5
|
-
static fromDer(derKey: DerEncodedPublicKey): Ed25519PublicKey;
|
6
|
-
private static RAW_KEY_LENGTH;
|
7
|
-
private static derEncode;
|
8
|
-
private static derDecode;
|
9
|
-
private readonly rawKey;
|
10
|
-
private readonly derKey;
|
11
|
-
private constructor();
|
12
|
-
toDer(): DerEncodedPublicKey;
|
13
|
-
toRaw(): ArrayBuffer;
|
14
|
-
}
|
15
|
-
export declare class Ed25519KeyIdentity extends SignIdentity {
|
16
|
-
protected _privateKey: ArrayBuffer;
|
17
|
-
static generate(seed?: Uint8Array): Ed25519KeyIdentity;
|
18
|
-
static fromParsedJson(obj: JsonnableEd25519KeyIdentity): Ed25519KeyIdentity;
|
19
|
-
static fromJSON(json: string): Ed25519KeyIdentity;
|
20
|
-
static fromKeyPair(publicKey: ArrayBuffer, privateKey: ArrayBuffer): Ed25519KeyIdentity;
|
21
|
-
static fromSecretKey(secretKey: ArrayBuffer): Ed25519KeyIdentity;
|
22
|
-
protected _publicKey: Ed25519PublicKey;
|
23
|
-
protected constructor(publicKey: PublicKey, _privateKey: ArrayBuffer);
|
24
|
-
/**
|
25
|
-
* Serialize this key to JSON.
|
26
|
-
*/
|
27
|
-
toJSON(): JsonnableEd25519KeyIdentity;
|
28
|
-
/**
|
29
|
-
* Return a copy of the key pair.
|
30
|
-
*/
|
31
|
-
getKeyPair(): KeyPair;
|
32
|
-
/**
|
33
|
-
* Return the public key.
|
34
|
-
*/
|
35
|
-
getPublicKey(): PublicKey;
|
36
|
-
/**
|
37
|
-
* Signs a blob of data, with this identity's private key.
|
38
|
-
* @param challenge - challenge to sign with this identity's secretKey, producing a signature
|
39
|
-
*/
|
40
|
-
sign(challenge: ArrayBuffer): Promise<Signature>;
|
41
|
-
}
|
42
|
-
declare type PublicKeyHex = string;
|
43
|
-
declare type SecretKeyHex = string;
|
44
|
-
export declare type JsonnableEd25519KeyIdentity = [PublicKeyHex, SecretKeyHex];
|
45
|
-
export {};
|
@@ -1,110 +0,0 @@
|
|
1
|
-
import { SignIdentity } from '@astrox/agent';
|
2
|
-
import * as tweetnacl from 'tweetnacl';
|
3
|
-
import { fromHexString, toHexString } from '../buffer';
|
4
|
-
import { ED25519_OID, unwrapDER, wrapDER } from './der';
|
5
|
-
export class Ed25519PublicKey {
|
6
|
-
// `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
|
7
|
-
constructor(key) {
|
8
|
-
this.rawKey = key;
|
9
|
-
this.derKey = Ed25519PublicKey.derEncode(key);
|
10
|
-
}
|
11
|
-
static from(key) {
|
12
|
-
return this.fromDer(key.toDer());
|
13
|
-
}
|
14
|
-
static fromRaw(rawKey) {
|
15
|
-
return new Ed25519PublicKey(rawKey);
|
16
|
-
}
|
17
|
-
static fromDer(derKey) {
|
18
|
-
return new Ed25519PublicKey(this.derDecode(derKey));
|
19
|
-
}
|
20
|
-
static derEncode(publicKey) {
|
21
|
-
return wrapDER(publicKey, ED25519_OID).buffer;
|
22
|
-
}
|
23
|
-
static derDecode(key) {
|
24
|
-
const unwrapped = unwrapDER(key, ED25519_OID);
|
25
|
-
if (unwrapped.length !== this.RAW_KEY_LENGTH) {
|
26
|
-
throw new Error('An Ed25519 public key must be exactly 32bytes long');
|
27
|
-
}
|
28
|
-
return unwrapped;
|
29
|
-
}
|
30
|
-
toDer() {
|
31
|
-
return this.derKey;
|
32
|
-
}
|
33
|
-
toRaw() {
|
34
|
-
return this.rawKey;
|
35
|
-
}
|
36
|
-
}
|
37
|
-
// The length of Ed25519 public keys is always 32 bytes.
|
38
|
-
Ed25519PublicKey.RAW_KEY_LENGTH = 32;
|
39
|
-
export class Ed25519KeyIdentity extends SignIdentity {
|
40
|
-
// `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
|
41
|
-
constructor(publicKey, _privateKey) {
|
42
|
-
super();
|
43
|
-
this._privateKey = _privateKey;
|
44
|
-
this._publicKey = Ed25519PublicKey.from(publicKey);
|
45
|
-
}
|
46
|
-
static generate(seed) {
|
47
|
-
if (seed && seed.length !== 32) {
|
48
|
-
throw new Error('Ed25519 Seed needs to be 32 bytes long.');
|
49
|
-
}
|
50
|
-
const { publicKey, secretKey } = seed === undefined ? tweetnacl.sign.keyPair() : tweetnacl.sign.keyPair.fromSeed(seed);
|
51
|
-
return new this(Ed25519PublicKey.fromRaw(publicKey), secretKey);
|
52
|
-
}
|
53
|
-
static fromParsedJson(obj) {
|
54
|
-
const [publicKeyDer, privateKeyRaw] = obj;
|
55
|
-
return new Ed25519KeyIdentity(Ed25519PublicKey.fromDer(fromHexString(publicKeyDer)), fromHexString(privateKeyRaw));
|
56
|
-
}
|
57
|
-
static fromJSON(json) {
|
58
|
-
const parsed = JSON.parse(json);
|
59
|
-
if (Array.isArray(parsed)) {
|
60
|
-
if (typeof parsed[0] === 'string' && typeof parsed[1] === 'string') {
|
61
|
-
return this.fromParsedJson([parsed[0], parsed[1]]);
|
62
|
-
}
|
63
|
-
else {
|
64
|
-
throw new Error('Deserialization error: JSON must have at least 2 items.');
|
65
|
-
}
|
66
|
-
}
|
67
|
-
else if (typeof parsed === 'object' && parsed !== null) {
|
68
|
-
throw new Error('Deprecated JSON format for Ed25519 keys.');
|
69
|
-
}
|
70
|
-
throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(json)}`);
|
71
|
-
}
|
72
|
-
static fromKeyPair(publicKey, privateKey) {
|
73
|
-
return new Ed25519KeyIdentity(Ed25519PublicKey.fromRaw(publicKey), privateKey);
|
74
|
-
}
|
75
|
-
static fromSecretKey(secretKey) {
|
76
|
-
const keyPair = tweetnacl.sign.keyPair.fromSecretKey(new Uint8Array(secretKey));
|
77
|
-
return Ed25519KeyIdentity.fromKeyPair(keyPair.publicKey, keyPair.secretKey);
|
78
|
-
}
|
79
|
-
/**
|
80
|
-
* Serialize this key to JSON.
|
81
|
-
*/
|
82
|
-
toJSON() {
|
83
|
-
return [toHexString(this._publicKey.toDer()), toHexString(this._privateKey)];
|
84
|
-
}
|
85
|
-
/**
|
86
|
-
* Return a copy of the key pair.
|
87
|
-
*/
|
88
|
-
getKeyPair() {
|
89
|
-
return {
|
90
|
-
secretKey: this._privateKey,
|
91
|
-
publicKey: this._publicKey,
|
92
|
-
};
|
93
|
-
}
|
94
|
-
/**
|
95
|
-
* Return the public key.
|
96
|
-
*/
|
97
|
-
getPublicKey() {
|
98
|
-
return this._publicKey;
|
99
|
-
}
|
100
|
-
/**
|
101
|
-
* Signs a blob of data, with this identity's private key.
|
102
|
-
* @param challenge - challenge to sign with this identity's secretKey, producing a signature
|
103
|
-
*/
|
104
|
-
async sign(challenge) {
|
105
|
-
const blob = new Uint8Array(challenge);
|
106
|
-
const signature = tweetnacl.sign.detached(blob, new Uint8Array(this._privateKey)).buffer;
|
107
|
-
return signature;
|
108
|
-
}
|
109
|
-
}
|
110
|
-
//# sourceMappingURL=ed25519.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../src/identity/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsD,YAAY,EAAE,MAAM,eAAe,CAAC;AACjG,OAAO,KAAK,SAAS,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAExD,MAAM,OAAO,gBAAgB;IA+B3B,kFAAkF;IAClF,YAAoB,GAAgB;QAClC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAClB,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAChD,CAAC;IAlCM,MAAM,CAAC,IAAI,CAAC,GAAc;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EA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