@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.
Files changed (42) hide show
  1. package/package.json +2 -2
  2. package/lib/cjs/buffer.d.ts +0 -10
  3. package/lib/cjs/buffer.js +0 -21
  4. package/lib/cjs/buffer.js.map +0 -1
  5. package/lib/cjs/identity/delegation.d.ts +0 -131
  6. package/lib/cjs/identity/delegation.js +0 -244
  7. package/lib/cjs/identity/delegation.js.map +0 -1
  8. package/lib/cjs/identity/der.d.ts +0 -26
  9. package/lib/cjs/identity/der.js +0 -145
  10. package/lib/cjs/identity/der.js.map +0 -1
  11. package/lib/cjs/identity/ed25519.d.ts +0 -45
  12. package/lib/cjs/identity/ed25519.js +0 -134
  13. package/lib/cjs/identity/ed25519.js.map +0 -1
  14. package/lib/cjs/identity/webauthn.d.ts +0 -40
  15. package/lib/cjs/identity/webauthn.js +0 -205
  16. package/lib/cjs/identity/webauthn.js.map +0 -1
  17. package/lib/cjs/index.d.ts +0 -4
  18. package/lib/cjs/index.js +0 -17
  19. package/lib/cjs/index.js.map +0 -1
  20. package/lib/esm/buffer.d.ts +0 -10
  21. package/lib/esm/buffer.js +0 -16
  22. package/lib/esm/buffer.js.map +0 -1
  23. package/lib/esm/identity/delegation.d.ts +0 -131
  24. package/lib/esm/identity/delegation.js +0 -219
  25. package/lib/esm/identity/delegation.js.map +0 -1
  26. package/lib/esm/identity/der.d.ts +0 -35
  27. package/lib/esm/identity/der.js +0 -168
  28. package/lib/esm/identity/der.js.map +0 -1
  29. package/lib/esm/identity/ed25519.d.ts +0 -45
  30. package/lib/esm/identity/ed25519.js +0 -110
  31. package/lib/esm/identity/ed25519.js.map +0 -1
  32. package/lib/esm/identity/secp256k1.d.ts +0 -73
  33. package/lib/esm/identity/secp256k1.js +0 -148
  34. package/lib/esm/identity/secp256k1.js.map +0 -1
  35. package/lib/esm/identity/webauthn.d.ts +0 -40
  36. package/lib/esm/identity/webauthn.js +0 -178
  37. package/lib/esm/identity/webauthn.js.map +0 -1
  38. package/lib/esm/index.d.ts +0 -4
  39. package/lib/esm/index.js +0 -5
  40. package/lib/esm/index.js.map +0 -1
  41. package/lib/tsconfig-cjs.tsbuildinfo +0 -1744
  42. package/lib/tsconfig.tsbuildinfo +0 -2508
@@ -1,134 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.Ed25519KeyIdentity = exports.Ed25519PublicKey = void 0;
23
- const agent_1 = require("@astrox/agent");
24
- const tweetnacl = __importStar(require("tweetnacl"));
25
- const buffer_1 = require("../buffer");
26
- const der_1 = require("./der");
27
- class Ed25519PublicKey {
28
- // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
29
- constructor(key) {
30
- this.rawKey = key;
31
- this.derKey = Ed25519PublicKey.derEncode(key);
32
- }
33
- static from(key) {
34
- return this.fromDer(key.toDer());
35
- }
36
- static fromRaw(rawKey) {
37
- return new Ed25519PublicKey(rawKey);
38
- }
39
- static fromDer(derKey) {
40
- return new Ed25519PublicKey(this.derDecode(derKey));
41
- }
42
- static derEncode(publicKey) {
43
- return der_1.wrapDER(publicKey, der_1.ED25519_OID).buffer;
44
- }
45
- static derDecode(key) {
46
- const unwrapped = der_1.unwrapDER(key, der_1.ED25519_OID);
47
- if (unwrapped.length !== this.RAW_KEY_LENGTH) {
48
- throw new Error('An Ed25519 public key must be exactly 32bytes long');
49
- }
50
- return unwrapped;
51
- }
52
- toDer() {
53
- return this.derKey;
54
- }
55
- toRaw() {
56
- return this.rawKey;
57
- }
58
- }
59
- exports.Ed25519PublicKey = Ed25519PublicKey;
60
- // The length of Ed25519 public keys is always 32 bytes.
61
- Ed25519PublicKey.RAW_KEY_LENGTH = 32;
62
- class Ed25519KeyIdentity extends agent_1.SignIdentity {
63
- // `fromRaw` and `fromDer` should be used for instantiation, not this constructor.
64
- constructor(publicKey, _privateKey) {
65
- super();
66
- this._privateKey = _privateKey;
67
- this._publicKey = Ed25519PublicKey.from(publicKey);
68
- }
69
- static generate(seed) {
70
- if (seed && seed.length !== 32) {
71
- throw new Error('Ed25519 Seed needs to be 32 bytes long.');
72
- }
73
- const { publicKey, secretKey } = seed === undefined ? tweetnacl.sign.keyPair() : tweetnacl.sign.keyPair.fromSeed(seed);
74
- return new this(Ed25519PublicKey.fromRaw(publicKey), secretKey);
75
- }
76
- static fromParsedJson(obj) {
77
- const [publicKeyDer, privateKeyRaw] = obj;
78
- return new Ed25519KeyIdentity(Ed25519PublicKey.fromDer(buffer_1.fromHexString(publicKeyDer)), buffer_1.fromHexString(privateKeyRaw));
79
- }
80
- static fromJSON(json) {
81
- const parsed = JSON.parse(json);
82
- if (Array.isArray(parsed)) {
83
- if (typeof parsed[0] === 'string' && typeof parsed[1] === 'string') {
84
- return this.fromParsedJson([parsed[0], parsed[1]]);
85
- }
86
- else {
87
- throw new Error('Deserialization error: JSON must have at least 2 items.');
88
- }
89
- }
90
- else if (typeof parsed === 'object' && parsed !== null) {
91
- throw new Error('Deprecated JSON format for Ed25519 keys.');
92
- }
93
- throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(json)}`);
94
- }
95
- static fromKeyPair(publicKey, privateKey) {
96
- return new Ed25519KeyIdentity(Ed25519PublicKey.fromRaw(publicKey), privateKey);
97
- }
98
- static fromSecretKey(secretKey) {
99
- const keyPair = tweetnacl.sign.keyPair.fromSecretKey(new Uint8Array(secretKey));
100
- return Ed25519KeyIdentity.fromKeyPair(keyPair.publicKey, keyPair.secretKey);
101
- }
102
- /**
103
- * Serialize this key to JSON.
104
- */
105
- toJSON() {
106
- return [buffer_1.toHexString(this._publicKey.toDer()), buffer_1.toHexString(this._privateKey)];
107
- }
108
- /**
109
- * Return a copy of the key pair.
110
- */
111
- getKeyPair() {
112
- return {
113
- secretKey: this._privateKey,
114
- publicKey: this._publicKey,
115
- };
116
- }
117
- /**
118
- * Return the public key.
119
- */
120
- getPublicKey() {
121
- return this._publicKey;
122
- }
123
- /**
124
- * Signs a blob of data, with this identity's private key.
125
- * @param challenge - challenge to sign with this identity's secretKey, producing a signature
126
- */
127
- async sign(challenge) {
128
- const blob = new Uint8Array(challenge);
129
- const signature = tweetnacl.sign.detached(blob, new Uint8Array(this._privateKey)).buffer;
130
- return signature;
131
- }
132
- }
133
- exports.Ed25519KeyIdentity = Ed25519KeyIdentity;
134
- //# sourceMappingURL=ed25519.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../src/identity/ed25519.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAiG;AACjG,qDAAuC;AACvC,sCAAuD;AACvD,+BAAwD;AAExD,MAAa,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,EAAE,CAAC,CAAC;IACnC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CAAC,SAAsB,EAAE,UAAuB;QACvE,OAAO,IAAI,kBAAkB,CAAC,gBAAgB,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,UAAU,CAAC,CAAC;IACjF,CAAC;IAEM,MAAM,CAAC,aAAa,CAAC,SAAsB;QAChD,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC;QAChF,OAAO,kBAAkB,CAAC,WAAW,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;IAC9E,CAAC;IAUD;;OAEG;IACI,MAAM;QACX,OAAO,CAAC,oBAAW,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,EAAE,oBAAW,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;IAC/E,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,WAAW;YAC3B,SAAS,EAAE,IAAI,CAAC,UAAU;SAC3B,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,YAAY;QACjB,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,IAAI,CAAC,SAAsB;QACtC,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;QACvC,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC;QACzF,OAAO,SAAsB,CAAC;IAChC,CAAC;CACF;AAnFD,gDAmFC"}
@@ -1,40 +0,0 @@
1
- import { DerEncodedPublicKey, PublicKey, Signature, SignIdentity } from '@astrox/agent';
2
- export declare class CosePublicKey implements PublicKey {
3
- protected _cose: ArrayBuffer;
4
- protected _encodedKey: DerEncodedPublicKey;
5
- constructor(_cose: ArrayBuffer);
6
- toDer(): DerEncodedPublicKey;
7
- getCose(): ArrayBuffer;
8
- }
9
- /**
10
- * A SignIdentity that uses `navigator.credentials`. See https://webauthn.guide/ for
11
- * more information about WebAuthentication.
12
- */
13
- export declare class WebAuthnIdentity extends SignIdentity {
14
- readonly rawId: ArrayBuffer;
15
- /**
16
- * Create an identity from a JSON serialization.
17
- * @param json - json to parse
18
- */
19
- static fromJSON(json: string): WebAuthnIdentity;
20
- /**
21
- * Create an identity.
22
- * @param credentialCreationOptions an optional CredentialCreationOptions Challenge
23
- */
24
- static create(credentialCreationOptions?: CredentialCreationOptions): Promise<WebAuthnIdentity>;
25
- protected _publicKey: CosePublicKey;
26
- protected constructor(rawId: ArrayBuffer, cose: ArrayBuffer);
27
- getPublicKey(): PublicKey;
28
- sign(blob: ArrayBuffer): Promise<Signature>;
29
- /**
30
- * Allow for JSON serialization of all information needed to reuse this identity.
31
- */
32
- toJSON(): JsonnableWebAuthnIdentitiy;
33
- }
34
- /**
35
- * ReturnType<WebAuthnIdentity.toJSON>
36
- */
37
- export interface JsonnableWebAuthnIdentitiy {
38
- publicKey: string;
39
- rawId: string;
40
- }
@@ -1,205 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __importDefault = (this && this.__importDefault) || function (mod) {
22
- return (mod && mod.__esModule) ? mod : { "default": mod };
23
- };
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.WebAuthnIdentity = exports.CosePublicKey = void 0;
26
- const agent_1 = require("@astrox/agent");
27
- const borc_1 = __importDefault(require("borc"));
28
- const tweetnacl = __importStar(require("tweetnacl"));
29
- const buffer_1 = require("../buffer");
30
- const der_1 = require("./der");
31
- function _coseToDerEncodedBlob(cose) {
32
- return der_1.wrapDER(cose, der_1.DER_COSE_OID).buffer;
33
- }
34
- /**
35
- * From the documentation;
36
- * The authData is a byte array described in the spec. Parsing it will involve slicing bytes from
37
- * the array and converting them into usable objects.
38
- *
39
- * See https://webauthn.guide/#registration (subsection "Example: Parsing the authenticator data").
40
- *
41
- * @param authData The authData field of the attestation response.
42
- * @returns The COSE key of the authData.
43
- */
44
- function _authDataToCose(authData) {
45
- const dataView = new DataView(new ArrayBuffer(2));
46
- const idLenBytes = authData.slice(53, 55);
47
- [...new Uint8Array(idLenBytes)].forEach((v, i) => dataView.setUint8(i, v));
48
- const credentialIdLength = dataView.getUint16(0);
49
- // Get the public key object.
50
- return authData.slice(55 + credentialIdLength);
51
- }
52
- class CosePublicKey {
53
- constructor(_cose) {
54
- this._cose = _cose;
55
- this._encodedKey = _coseToDerEncodedBlob(_cose);
56
- }
57
- toDer() {
58
- return this._encodedKey;
59
- }
60
- getCose() {
61
- return this._cose;
62
- }
63
- }
64
- exports.CosePublicKey = CosePublicKey;
65
- /**
66
- * Create a challenge from a string or array. The default challenge is always the same
67
- * because we don't need to verify the authenticity of the key on the server (we don't
68
- * register our keys with the IC). Any challenge would do, even one per key, randomly
69
- * generated.
70
- *
71
- * @param challenge The challenge to transform into a byte array. By default a hard
72
- * coded string.
73
- */
74
- function _createChallengeBuffer(challenge = '<ic0.app>') {
75
- if (typeof challenge === 'string') {
76
- return Uint8Array.from(challenge, c => c.charCodeAt(0));
77
- }
78
- else {
79
- return challenge;
80
- }
81
- }
82
- /**
83
- * Create a credentials to authenticate with a server. This is necessary in order in
84
- * WebAuthn to get credentials IDs (which give us the public key and allow us to
85
- * sign), but in the case of the Internet Computer, we don't actually need to register
86
- * it, so we don't.
87
- * @param credentialCreationOptions an optional CredentialCreationOptions object
88
- */
89
- async function _createCredential(credentialCreationOptions) {
90
- const creds = (await navigator.credentials.create(credentialCreationOptions !== null && credentialCreationOptions !== void 0 ? credentialCreationOptions : {
91
- publicKey: {
92
- authenticatorSelection: {
93
- userVerification: 'preferred',
94
- },
95
- attestation: 'direct',
96
- challenge: _createChallengeBuffer(),
97
- pubKeyCredParams: [{ type: 'public-key', alg: PubKeyCoseAlgo.ECDSA_WITH_SHA256 }],
98
- rp: {
99
- name: 'Internet Identity Service',
100
- },
101
- user: {
102
- id: tweetnacl.randomBytes(16),
103
- name: 'Internet Identity',
104
- displayName: 'Internet Identity',
105
- },
106
- },
107
- }));
108
- // Validate that it's the correct type at runtime, since WebAuthn does not HAVE to
109
- // reply with a PublicKeyCredential.
110
- if (creds.response === undefined || !(creds.rawId instanceof ArrayBuffer)) {
111
- return null;
112
- }
113
- else {
114
- return creds;
115
- }
116
- }
117
- // See https://www.iana.org/assignments/cose/cose.xhtml#algorithms for a complete
118
- // list of these algorithms. We only list the ones we support here.
119
- var PubKeyCoseAlgo;
120
- (function (PubKeyCoseAlgo) {
121
- PubKeyCoseAlgo[PubKeyCoseAlgo["ECDSA_WITH_SHA256"] = -7] = "ECDSA_WITH_SHA256";
122
- })(PubKeyCoseAlgo || (PubKeyCoseAlgo = {}));
123
- /**
124
- * A SignIdentity that uses `navigator.credentials`. See https://webauthn.guide/ for
125
- * more information about WebAuthentication.
126
- */
127
- class WebAuthnIdentity extends agent_1.SignIdentity {
128
- constructor(rawId, cose) {
129
- super();
130
- this.rawId = rawId;
131
- this._publicKey = new CosePublicKey(cose);
132
- }
133
- /**
134
- * Create an identity from a JSON serialization.
135
- * @param json - json to parse
136
- */
137
- static fromJSON(json) {
138
- const { publicKey, rawId } = JSON.parse(json);
139
- if (typeof publicKey !== 'string' || typeof rawId !== 'string') {
140
- throw new Error('Invalid JSON string.');
141
- }
142
- return new this(buffer_1.fromHexString(rawId), buffer_1.fromHexString(publicKey));
143
- }
144
- /**
145
- * Create an identity.
146
- * @param credentialCreationOptions an optional CredentialCreationOptions Challenge
147
- */
148
- static async create(credentialCreationOptions) {
149
- const creds = await _createCredential(credentialCreationOptions);
150
- if (!creds || creds.type !== 'public-key') {
151
- throw new Error('Could not create credentials.');
152
- }
153
- const response = creds.response;
154
- if (!(response.attestationObject instanceof ArrayBuffer)) {
155
- throw new Error('Was expecting an attestation response.');
156
- }
157
- // Parse the attestationObject as CBOR.
158
- const attObject = borc_1.default.decodeFirst(new Uint8Array(response.attestationObject));
159
- return new this(creds.rawId, _authDataToCose(attObject.authData));
160
- }
161
- getPublicKey() {
162
- return this._publicKey;
163
- }
164
- async sign(blob) {
165
- const result = (await navigator.credentials.get({
166
- publicKey: {
167
- allowCredentials: [
168
- {
169
- type: 'public-key',
170
- id: this.rawId,
171
- },
172
- ],
173
- challenge: blob,
174
- userVerification: 'preferred',
175
- },
176
- }));
177
- const response = result.response;
178
- if (response.signature instanceof ArrayBuffer &&
179
- response.authenticatorData instanceof ArrayBuffer) {
180
- const cbor = borc_1.default.encode(new borc_1.default.Tagged(55799, {
181
- authenticator_data: new Uint8Array(response.authenticatorData),
182
- client_data_json: new TextDecoder().decode(response.clientDataJSON),
183
- signature: new Uint8Array(response.signature),
184
- }));
185
- if (!cbor) {
186
- throw new Error('failed to encode cbor');
187
- }
188
- return cbor.buffer;
189
- }
190
- else {
191
- throw new Error('Invalid response from WebAuthn.');
192
- }
193
- }
194
- /**
195
- * Allow for JSON serialization of all information needed to reuse this identity.
196
- */
197
- toJSON() {
198
- return {
199
- publicKey: buffer_1.toHexString(this._publicKey.getCose()),
200
- rawId: buffer_1.toHexString(this.rawId),
201
- };
202
- }
203
- }
204
- exports.WebAuthnIdentity = WebAuthnIdentity;
205
- //# sourceMappingURL=webauthn.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"webauthn.js","sourceRoot":"","sources":["../../../src/identity/webauthn.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAwF;AACxF,gDAAwB;AACxB,qDAAuC;AACvC,sCAAuD;AACvD,+BAA8C;AAE9C,SAAS,qBAAqB,CAAC,IAAiB;IAC9C,OAAO,aAAO,CAAC,IAAI,EAAE,kBAAY,CAAC,CAAC,MAA6B,CAAC;AACnE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,eAAe,CAAC,QAAqB;IAC5C,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IAClD,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAC1C,CAAC,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3E,MAAM,kBAAkB,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IAEjD,6BAA6B;IAC7B,OAAO,QAAQ,CAAC,KAAK,CAAC,EAAE,GAAG,kBAAkB,CAAC,CAAC;AACjD,CAAC;AAED,MAAa,aAAa;IAExB,YAA6B,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;QAC7C,IAAI,CAAC,WAAW,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;IAClD,CAAC;IAEM,KAAK;QACV,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF;AAbD,sCAaC;AAED;;;;;;;;GAQG;AACH,SAAS,sB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9FD,4CA8FC"}
@@ -1,4 +0,0 @@
1
- export { Ed25519KeyIdentity, Ed25519PublicKey } from './identity/ed25519';
2
- export { Delegation, DelegationIdentity, DelegationChain, SignedDelegation, } from './identity/delegation';
3
- export { WebAuthnIdentity } from './identity/webauthn';
4
- export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from './identity/der';
package/lib/cjs/index.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var ed25519_1 = require("./identity/ed25519");
4
- Object.defineProperty(exports, "Ed25519KeyIdentity", { enumerable: true, get: function () { return ed25519_1.Ed25519KeyIdentity; } });
5
- Object.defineProperty(exports, "Ed25519PublicKey", { enumerable: true, get: function () { return ed25519_1.Ed25519PublicKey; } });
6
- var delegation_1 = require("./identity/delegation");
7
- Object.defineProperty(exports, "Delegation", { enumerable: true, get: function () { return delegation_1.Delegation; } });
8
- Object.defineProperty(exports, "DelegationIdentity", { enumerable: true, get: function () { return delegation_1.DelegationIdentity; } });
9
- Object.defineProperty(exports, "DelegationChain", { enumerable: true, get: function () { return delegation_1.DelegationChain; } });
10
- var webauthn_1 = require("./identity/webauthn");
11
- Object.defineProperty(exports, "WebAuthnIdentity", { enumerable: true, get: function () { return webauthn_1.WebAuthnIdentity; } });
12
- var der_1 = require("./identity/der");
13
- Object.defineProperty(exports, "wrapDER", { enumerable: true, get: function () { return der_1.wrapDER; } });
14
- Object.defineProperty(exports, "unwrapDER", { enumerable: true, get: function () { return der_1.unwrapDER; } });
15
- Object.defineProperty(exports, "DER_COSE_OID", { enumerable: true, get: function () { return der_1.DER_COSE_OID; } });
16
- Object.defineProperty(exports, "ED25519_OID", { enumerable: true, get: function () { return der_1.ED25519_OID; } });
17
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;AAAA,8CAA0E;AAAjE,6GAAA,kBAAkB,OAAA;AAAE,2GAAA,gBAAgB,OAAA;AAC7C,oDAK+B;AAJ7B,wGAAA,UAAU,OAAA;AACV,gHAAA,kBAAkB,OAAA;AAClB,6GAAA,eAAe,OAAA;AAGjB,gDAAuD;AAA9C,4GAAA,gBAAgB,OAAA;AACzB,sCAA+E;AAAtE,8FAAA,OAAO,OAAA;AAAE,gGAAA,SAAS,OAAA;AAAE,mGAAA,YAAY,OAAA;AAAE,kGAAA,WAAW,OAAA"}
@@ -1,10 +0,0 @@
1
- /**
2
- * Return an array buffer from its hexadecimal representation.
3
- * @param hexString The hexadecimal string.
4
- */
5
- export declare function fromHexString(hexString: string): ArrayBuffer;
6
- /**
7
- * Returns an hexadecimal representation of an array buffer.
8
- * @param bytes The array buffer.
9
- */
10
- export declare function toHexString(bytes: ArrayBuffer): string;
package/lib/esm/buffer.js DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * Return an array buffer from its hexadecimal representation.
3
- * @param hexString The hexadecimal string.
4
- */
5
- export function fromHexString(hexString) {
6
- var _a;
7
- return new Uint8Array(((_a = hexString.match(/.{1,2}/g)) !== null && _a !== void 0 ? _a : []).map(byte => parseInt(byte, 16))).buffer;
8
- }
9
- /**
10
- * Returns an hexadecimal representation of an array buffer.
11
- * @param bytes The array buffer.
12
- */
13
- export function toHexString(bytes) {
14
- return new Uint8Array(bytes).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
15
- }
16
- //# sourceMappingURL=buffer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"buffer.js","sourceRoot":"","sources":["../../src/buffer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,SAAiB;;IAC7C,OAAO,IAAI,UAAU,CAAC,CAAC,MAAA,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,mCAAI,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AACnG,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAAkB;IAC5C,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;AACnG,CAAC"}
@@ -1,131 +0,0 @@
1
- import { DerEncodedPublicKey, HttpAgentRequest, PublicKey, Signature, SignIdentity } from '@astrox/agent';
2
- import { Principal } from '@astrox/principal';
3
- import * as cbor from 'simple-cbor';
4
- /**
5
- * A single delegation object that is signed by a private key. This is constructed by
6
- * `DelegationChain.create()`.
7
- *
8
- * {@see DelegationChain}
9
- */
10
- export declare class Delegation {
11
- readonly pubkey: ArrayBuffer;
12
- readonly expiration: bigint;
13
- readonly targets?: Principal[] | undefined;
14
- constructor(pubkey: ArrayBuffer, expiration: bigint, targets?: Principal[] | undefined);
15
- toCBOR(): cbor.CborValue;
16
- toJSON(): JsonnableDelegation;
17
- }
18
- /**
19
- * Type of ReturnType<Delegation.toJSON>.
20
- * The goal here is to stringify all non-JSON-compatible types to some bytes representation we can
21
- * stringify as hex.
22
- * (Hex shouldn't be ambiguous ever, because you can encode as DER with semantic OIDs).
23
- */
24
- interface JsonnableDelegation {
25
- expiration: string;
26
- pubkey: string;
27
- targets?: string[];
28
- }
29
- /**
30
- * A signed delegation, which lends its identity to the public key in the delegation
31
- * object. This is constructed by `DelegationChain.create()`.
32
- *
33
- * {@see DelegationChain}
34
- */
35
- export interface SignedDelegation {
36
- delegation: Delegation;
37
- signature: Signature;
38
- }
39
- export interface JsonnableDelegationChain {
40
- publicKey: string;
41
- delegations: Array<{
42
- signature: string;
43
- delegation: {
44
- pubkey: string;
45
- expiration: string;
46
- targets?: string[];
47
- };
48
- }>;
49
- }
50
- /**
51
- * A chain of delegations. This is JSON Serializable.
52
- * This is the object to serialize and pass to a DelegationIdentity. It does not keep any
53
- * private keys.
54
- */
55
- export declare class DelegationChain {
56
- readonly delegations: SignedDelegation[];
57
- readonly publicKey: DerEncodedPublicKey;
58
- /**
59
- * Create a delegation chain between two (or more) keys. By default, the expiration time
60
- * will be very short (15 minutes).
61
- *
62
- * To build a chain of more than 2 identities, this function needs to be called multiple times,
63
- * passing the previous delegation chain into the options argument. For example:
64
- *
65
- * @example
66
- * const rootKey = createKey();
67
- * const middleKey = createKey();
68
- * const bottomeKey = createKey();
69
- *
70
- * const rootToMiddle = await DelegationChain.create(
71
- * root, middle.getPublicKey(), Date.parse('2100-01-01'),
72
- * );
73
- * const middleToBottom = await DelegationChain.create(
74
- * middle, bottom.getPublicKey(), Date.parse('2100-01-01'), { previous: rootToMiddle },
75
- * );
76
- *
77
- * // We can now use a delegation identity that uses the delegation above:
78
- * const identity = DelegationIdentity.fromDelegation(bottomKey, middleToBottom);
79
- *
80
- * @param from The identity that will delegate.
81
- * @param to The identity that gets delegated. It can now sign messages as if it was the
82
- * identity above.
83
- * @param expiration The length the delegation is valid. By default, 15 minutes from calling
84
- * this function.
85
- * @param options A set of options for this delegation. expiration and previous
86
- * @param options.previous - Another DelegationChain that this chain should start with.
87
- * @param options.targets - targets that scope the delegation (e.g. Canister Principals)
88
- */
89
- static create(from: SignIdentity, to: PublicKey, expiration?: Date, options?: {
90
- previous?: DelegationChain;
91
- targets?: Principal[];
92
- }): Promise<DelegationChain>;
93
- /**
94
- * Creates a DelegationChain object from a JSON string.
95
- *
96
- * @param json The JSON string to parse.
97
- */
98
- static fromJSON(json: string | JsonnableDelegationChain): DelegationChain;
99
- /**
100
- * Creates a DelegationChain object from a list of delegations and a DER-encoded public key.
101
- *
102
- * @param delegations The list of delegations.
103
- * @param publicKey The DER-encoded public key of the key-pair signing the first delegation.
104
- */
105
- static fromDelegations(delegations: SignedDelegation[], publicKey: DerEncodedPublicKey): DelegationChain;
106
- protected constructor(delegations: SignedDelegation[], publicKey: DerEncodedPublicKey);
107
- toJSON(): JsonnableDelegationChain;
108
- }
109
- /**
110
- * An Identity that adds delegation to a request. Everywhere in this class, the name
111
- * innerKey refers to the SignIdentity that is being used to sign the requests, while
112
- * originalKey is the identity that is being borrowed. More identities can be used
113
- * in the middle to delegate.
114
- */
115
- export declare class DelegationIdentity extends SignIdentity {
116
- private _inner;
117
- private _delegation;
118
- /**
119
- * Create a delegation without having access to delegateKey.
120
- *
121
- * @param key The key used to sign the reqyests.
122
- * @param delegation A delegation object created using `createDelegation`.
123
- */
124
- static fromDelegation(key: Pick<SignIdentity, 'sign'>, delegation: DelegationChain): DelegationIdentity;
125
- protected constructor(_inner: Pick<SignIdentity, 'sign'>, _delegation: DelegationChain);
126
- getDelegation(): DelegationChain;
127
- getPublicKey(): PublicKey;
128
- sign(blob: ArrayBuffer): Promise<Signature>;
129
- transformRequest(request: HttpAgentRequest): Promise<unknown>;
130
- }
131
- export {};