@dfinity/principal
Advanced tools
Comparing version 0.21.1 to 0.21.2
@@ -0,1 +1,5 @@ | ||
export declare const JSON_KEY_PRINCIPAL = "__principal__"; | ||
export declare type JsonnablePrincipal = { | ||
[JSON_KEY_PRINCIPAL]: string; | ||
}; | ||
export declare class Principal { | ||
@@ -22,2 +26,7 @@ private _arr; | ||
/** | ||
* Serializes to JSON | ||
* @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string | ||
*/ | ||
toJSON(): JsonnablePrincipal; | ||
/** | ||
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification | ||
@@ -24,0 +33,0 @@ * @param {Principal} other - a {@link Principal} to compare |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Principal = void 0; | ||
exports.Principal = exports.JSON_KEY_PRINCIPAL = void 0; | ||
const base32_1 = require("./utils/base32"); | ||
const getCrc_1 = require("./utils/getCrc"); | ||
const sha224_1 = require("./utils/sha224"); | ||
exports.JSON_KEY_PRINCIPAL = '__principal__'; | ||
const SELF_AUTHENTICATING_SUFFIX = 2; | ||
@@ -13,2 +14,6 @@ const ANONYMOUS_SUFFIX = 4; | ||
class Principal { | ||
constructor(_arr) { | ||
this._arr = _arr; | ||
this._isPrincipal = true; | ||
} | ||
static anonymous() { | ||
@@ -32,2 +37,5 @@ return new this(new Uint8Array([ANONYMOUS_SUFFIX])); | ||
} | ||
else if (Object.getPrototypeOf(other) === Uint8Array.prototype) { | ||
return new Principal(other); | ||
} | ||
else if (typeof other === 'object' && | ||
@@ -44,8 +52,16 @@ other !== null && | ||
static fromText(text) { | ||
const canisterIdNoDash = text.toLowerCase().replace(/-/g, ''); | ||
let maybePrincipal = text; | ||
// If formatted as JSON string, parse it first | ||
if (text.includes(exports.JSON_KEY_PRINCIPAL)) { | ||
const obj = JSON.parse(text); | ||
if (exports.JSON_KEY_PRINCIPAL in obj) { | ||
maybePrincipal = obj[exports.JSON_KEY_PRINCIPAL]; | ||
} | ||
} | ||
const canisterIdNoDash = maybePrincipal.toLowerCase().replace(/-/g, ''); | ||
let arr = (0, base32_1.decode)(canisterIdNoDash); | ||
arr = arr.slice(4, arr.length); | ||
const principal = new this(arr); | ||
if (principal.toText() !== text) { | ||
throw new Error(`Principal "${principal.toText()}" does not have a valid checksum (original value "${text}" may not be a valid Principal ID).`); | ||
if (principal.toText() !== maybePrincipal) { | ||
throw new Error(`Principal "${principal.toText()}" does not have a valid checksum (original value "${maybePrincipal}" may not be a valid Principal ID).`); | ||
} | ||
@@ -57,6 +73,2 @@ return principal; | ||
} | ||
constructor(_arr) { | ||
this._arr = _arr; | ||
this._isPrincipal = true; | ||
} | ||
isAnonymous() { | ||
@@ -90,2 +102,9 @@ return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX; | ||
/** | ||
* Serializes to JSON | ||
* @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string | ||
*/ | ||
toJSON() { | ||
return { [exports.JSON_KEY_PRINCIPAL]: this.toText() }; | ||
} | ||
/** | ||
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification | ||
@@ -92,0 +111,0 @@ * @param {Principal} other - a {@link Principal} to compare |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decode = exports.encode = void 0; | ||
// tslint:disable:no-bitwise | ||
const alphabet = 'abcdefghijklmnopqrstuvwxyz234567'; | ||
@@ -6,0 +5,0 @@ // Build a lookup table for decoding. |
"use strict"; | ||
// tslint:disable:no-bitwise | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -48,3 +47,2 @@ exports.getCrc32 = void 0; | ||
let crc = -1; | ||
// tslint:disable-next-line:prefer-for-of | ||
for (let i = 0; i < b.length; i++) { | ||
@@ -51,0 +49,0 @@ const byte = b[i]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sha224 = void 0; | ||
const js_sha256_1 = require("js-sha256"); | ||
const sha256_1 = require("@noble/hashes/sha256"); | ||
/** | ||
@@ -10,7 +10,5 @@ * Returns the SHA224 hash of the buffer. | ||
function sha224(data) { | ||
const shaObj = js_sha256_1.sha224.create(); | ||
shaObj.update(data); | ||
return new Uint8Array(shaObj.array()); | ||
return sha256_1.sha224.create().update(new Uint8Array(data)).digest(); | ||
} | ||
exports.sha224 = sha224; | ||
//# sourceMappingURL=sha224.js.map |
@@ -0,1 +1,5 @@ | ||
export declare const JSON_KEY_PRINCIPAL = "__principal__"; | ||
export declare type JsonnablePrincipal = { | ||
[JSON_KEY_PRINCIPAL]: string; | ||
}; | ||
export declare class Principal { | ||
@@ -22,2 +26,7 @@ private _arr; | ||
/** | ||
* Serializes to JSON | ||
* @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string | ||
*/ | ||
toJSON(): JsonnablePrincipal; | ||
/** | ||
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification | ||
@@ -24,0 +33,0 @@ * @param {Principal} other - a {@link Principal} to compare |
import { decode, encode } from './utils/base32'; | ||
import { getCrc32 } from './utils/getCrc'; | ||
import { sha224 } from './utils/sha224'; | ||
export const JSON_KEY_PRINCIPAL = '__principal__'; | ||
const SELF_AUTHENTICATING_SUFFIX = 2; | ||
@@ -10,2 +11,6 @@ const ANONYMOUS_SUFFIX = 4; | ||
export class Principal { | ||
constructor(_arr) { | ||
this._arr = _arr; | ||
this._isPrincipal = true; | ||
} | ||
static anonymous() { | ||
@@ -29,2 +34,5 @@ return new this(new Uint8Array([ANONYMOUS_SUFFIX])); | ||
} | ||
else if (Object.getPrototypeOf(other) === Uint8Array.prototype) { | ||
return new Principal(other); | ||
} | ||
else if (typeof other === 'object' && | ||
@@ -41,8 +49,16 @@ other !== null && | ||
static fromText(text) { | ||
const canisterIdNoDash = text.toLowerCase().replace(/-/g, ''); | ||
let maybePrincipal = text; | ||
// If formatted as JSON string, parse it first | ||
if (text.includes(JSON_KEY_PRINCIPAL)) { | ||
const obj = JSON.parse(text); | ||
if (JSON_KEY_PRINCIPAL in obj) { | ||
maybePrincipal = obj[JSON_KEY_PRINCIPAL]; | ||
} | ||
} | ||
const canisterIdNoDash = maybePrincipal.toLowerCase().replace(/-/g, ''); | ||
let arr = decode(canisterIdNoDash); | ||
arr = arr.slice(4, arr.length); | ||
const principal = new this(arr); | ||
if (principal.toText() !== text) { | ||
throw new Error(`Principal "${principal.toText()}" does not have a valid checksum (original value "${text}" may not be a valid Principal ID).`); | ||
if (principal.toText() !== maybePrincipal) { | ||
throw new Error(`Principal "${principal.toText()}" does not have a valid checksum (original value "${maybePrincipal}" may not be a valid Principal ID).`); | ||
} | ||
@@ -54,6 +70,2 @@ return principal; | ||
} | ||
constructor(_arr) { | ||
this._arr = _arr; | ||
this._isPrincipal = true; | ||
} | ||
isAnonymous() { | ||
@@ -87,2 +99,9 @@ return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX; | ||
/** | ||
* Serializes to JSON | ||
* @returns {JsonnablePrincipal} a JSON object with a single key, {@link JSON_KEY_PRINCIPAL}, whose value is the principal as a string | ||
*/ | ||
toJSON() { | ||
return { [JSON_KEY_PRINCIPAL]: this.toText() }; | ||
} | ||
/** | ||
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification | ||
@@ -89,0 +108,0 @@ * @param {Principal} other - a {@link Principal} to compare |
@@ -1,2 +0,1 @@ | ||
// tslint:disable:no-bitwise | ||
const alphabet = 'abcdefghijklmnopqrstuvwxyz234567'; | ||
@@ -3,0 +2,0 @@ // Build a lookup table for decoding. |
@@ -1,2 +0,1 @@ | ||
// tslint:disable:no-bitwise | ||
// This file is translated to JavaScript from | ||
@@ -45,3 +44,2 @@ // https://lxp32.github.io/docs/a-simple-example-crc32-calculation/ | ||
let crc = -1; | ||
// tslint:disable-next-line:prefer-for-of | ||
for (let i = 0; i < b.length; i++) { | ||
@@ -48,0 +46,0 @@ const byte = b[i]; |
@@ -1,2 +0,2 @@ | ||
import { sha224 as jsSha224 } from 'js-sha256'; | ||
import { sha224 as jsSha224 } from '@noble/hashes/sha256'; | ||
/** | ||
@@ -7,6 +7,4 @@ * Returns the SHA224 hash of the buffer. | ||
export function sha224(data) { | ||
const shaObj = jsSha224.create(); | ||
shaObj.update(data); | ||
return new Uint8Array(shaObj.array()); | ||
return jsSha224.create().update(new Uint8Array(data)).digest(); | ||
} | ||
//# sourceMappingURL=sha224.js.map |
{ | ||
"name": "@dfinity/principal", | ||
"version": "0.21.1", | ||
"version": "0.21.2", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165386
713