@dfinity/principal
Advanced tools
Comparing version 0.19.2 to 0.19.3
@@ -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; | ||
@@ -46,8 +47,16 @@ const ANONYMOUS_SUFFIX = 4; | ||
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).`); | ||
} | ||
@@ -87,2 +96,9 @@ return principal; | ||
/** | ||
* 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 | ||
@@ -89,0 +105,0 @@ * @param {Principal} other - a {@link Principal} to compare |
@@ -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; | ||
@@ -43,8 +44,16 @@ const ANONYMOUS_SUFFIX = 4; | ||
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).`); | ||
} | ||
@@ -84,2 +93,9 @@ return principal; | ||
/** | ||
* 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 | ||
@@ -86,0 +102,0 @@ * @param {Principal} other - a {@link Principal} to compare |
{ | ||
"name": "@dfinity/principal", | ||
"version": "0.19.2", | ||
"version": "0.19.3", | ||
"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
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
146069
713