Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dfinity/principal

Package Overview
Dependencies
Maintainers
10
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dfinity/principal - npm Package Compare versions

Comparing version 1.0.0-beta.0 to 1.0.0

37

lib/cjs/index.d.ts

@@ -1,11 +0,6 @@

/**
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's
*/
export declare abstract class PrincipalLike {
abstract _isPrincipal: boolean;
abstract toString(): string;
abstract toText(): string;
abstract toUint8Array(): Uint8Array;
}
export declare class Principal implements PrincipalLike {
export declare const JSON_KEY_PRINCIPAL = "__principal__";
export declare type JsonnablePrincipal = {
[JSON_KEY_PRINCIPAL]: string;
};
export declare class Principal {
private _arr;

@@ -23,14 +18,2 @@ static anonymous(): Principal;

static fromUint8Array(arr: Uint8Array): Principal;
/**
* Utility to check if a provided value is a Principal
* @param maybePrincipal unknown type
* @returns boolean
*/
static isPrincipal(maybePrincipal: unknown): maybePrincipal is Principal;
/**
* Utility to check if a provided value is a Principal-encoded string
* @param maybeValid unknown type
* @returns boolean
*/
static isPrincipalString(maybeValid: unknown): boolean;
readonly _isPrincipal = true;

@@ -44,2 +27,7 @@ protected constructor(_arr: Uint8Array);

/**
* 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

@@ -49,4 +37,3 @@ * @param {Principal} other - a {@link Principal} to compare

*/
compareTo(other: PrincipalLike): 'lt' | 'eq' | 'gt';
static compare(p1: PrincipalLike, p2: PrincipalLike): 'lt' | 'eq' | 'gt';
compareTo(other: Principal): 'lt' | 'eq' | 'gt';
/**

@@ -63,3 +50,3 @@ * Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method

*/
gtEq(other: PrincipalLike): boolean;
gtEq(other: Principal): boolean;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Principal = exports.PrincipalLike = 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;

@@ -12,8 +13,2 @@ const ANONYMOUS_SUFFIX = 4;

const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
/**
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's
*/
class PrincipalLike {
}
exports.PrincipalLike = PrincipalLike;
class Principal {

@@ -42,5 +37,10 @@ constructor(_arr) {

}
else if (Principal.isPrincipal(other)) {
return new Principal(other.toUint8Array());
else if (Object.getPrototypeOf(other) === Uint8Array.prototype) {
return new Principal(other);
}
else if (typeof other === 'object' &&
other !== null &&
other._isPrincipal === true) {
return new Principal(other._arr);
}
throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`);

@@ -52,8 +52,16 @@ }

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).`);
}

@@ -65,38 +73,2 @@ return principal;

}
/**
* Utility to check if a provided value is a Principal
* @param maybePrincipal unknown type
* @returns boolean
*/
static isPrincipal(maybePrincipal) {
if (maybePrincipal instanceof Principal)
return true;
if (typeof maybePrincipal === 'object' && maybePrincipal !== null) {
if ('toText' in maybePrincipal &&
'toString' in maybePrincipal &&
'toUint8Array' in maybePrincipal &&
'_isPrincipal' in maybePrincipal) {
return maybePrincipal['_isPrincipal'];
}
}
return false;
}
/**
* Utility to check if a provided value is a Principal-encoded string
* @param maybeValid unknown type
* @returns boolean
*/
static isPrincipalString(maybeValid) {
if (typeof maybeValid === 'string') {
try {
Principal.fromText(maybeValid);
return true;
}
catch (_) {
// errors are expected and deliberately suppressed
return false;
}
}
return false;
}
isAnonymous() {

@@ -130,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

@@ -136,17 +115,12 @@ * @param {Principal} other - a {@link Principal} to compare

compareTo(other) {
return Principal.compare(this, other);
}
static compare(p1, p2) {
const p1Arr = p1.toUint8Array();
const p2Arr = p2.toUint8Array();
for (let i = 0; i < Math.min(p1Arr.length, p2Arr.length); i++) {
if (p1Arr[i] < p2Arr[i])
for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) {
if (this._arr[i] < other._arr[i])
return 'lt';
else if (p1Arr[i] > p2Arr[i])
else if (this._arr[i] > other._arr[i])
return 'gt';
}
// Here, at least one principal is a prefix of the p2 principal (they could be the same)
if (p1Arr.length < p2Arr.length)
// Here, at least one principal is a prefix of the other principal (they could be the same)
if (this._arr.length < other._arr.length)
return 'lt';
if (p1Arr.length > p2Arr.length)
if (this._arr.length > other._arr.length)
return 'gt';

@@ -153,0 +127,0 @@ return 'eq';

"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

@@ -1,11 +0,6 @@

/**
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's
*/
export declare abstract class PrincipalLike {
abstract _isPrincipal: boolean;
abstract toString(): string;
abstract toText(): string;
abstract toUint8Array(): Uint8Array;
}
export declare class Principal implements PrincipalLike {
export declare const JSON_KEY_PRINCIPAL = "__principal__";
export declare type JsonnablePrincipal = {
[JSON_KEY_PRINCIPAL]: string;
};
export declare class Principal {
private _arr;

@@ -23,14 +18,2 @@ static anonymous(): Principal;

static fromUint8Array(arr: Uint8Array): Principal;
/**
* Utility to check if a provided value is a Principal
* @param maybePrincipal unknown type
* @returns boolean
*/
static isPrincipal(maybePrincipal: unknown): maybePrincipal is Principal;
/**
* Utility to check if a provided value is a Principal-encoded string
* @param maybeValid unknown type
* @returns boolean
*/
static isPrincipalString(maybeValid: unknown): boolean;
readonly _isPrincipal = true;

@@ -44,2 +27,7 @@ protected constructor(_arr: Uint8Array);

/**
* 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

@@ -49,4 +37,3 @@ * @param {Principal} other - a {@link Principal} to compare

*/
compareTo(other: PrincipalLike): 'lt' | 'eq' | 'gt';
static compare(p1: PrincipalLike, p2: PrincipalLike): 'lt' | 'eq' | 'gt';
compareTo(other: Principal): 'lt' | 'eq' | 'gt';
/**

@@ -63,3 +50,3 @@ * Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method

*/
gtEq(other: PrincipalLike): boolean;
gtEq(other: Principal): boolean;
}
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;

@@ -9,7 +10,2 @@ const ANONYMOUS_SUFFIX = 4;

const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
/**
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's
*/
export class PrincipalLike {
}
export class Principal {

@@ -38,5 +34,10 @@ constructor(_arr) {

}
else if (Principal.isPrincipal(other)) {
return new Principal(other.toUint8Array());
else if (Object.getPrototypeOf(other) === Uint8Array.prototype) {
return new Principal(other);
}
else if (typeof other === 'object' &&
other !== null &&
other._isPrincipal === true) {
return new Principal(other._arr);
}
throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`);

@@ -48,8 +49,16 @@ }

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).`);
}

@@ -61,38 +70,2 @@ return principal;

}
/**
* Utility to check if a provided value is a Principal
* @param maybePrincipal unknown type
* @returns boolean
*/
static isPrincipal(maybePrincipal) {
if (maybePrincipal instanceof Principal)
return true;
if (typeof maybePrincipal === 'object' && maybePrincipal !== null) {
if ('toText' in maybePrincipal &&
'toString' in maybePrincipal &&
'toUint8Array' in maybePrincipal &&
'_isPrincipal' in maybePrincipal) {
return maybePrincipal['_isPrincipal'];
}
}
return false;
}
/**
* Utility to check if a provided value is a Principal-encoded string
* @param maybeValid unknown type
* @returns boolean
*/
static isPrincipalString(maybeValid) {
if (typeof maybeValid === 'string') {
try {
Principal.fromText(maybeValid);
return true;
}
catch (_) {
// errors are expected and deliberately suppressed
return false;
}
}
return false;
}
isAnonymous() {

@@ -126,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

@@ -132,17 +112,12 @@ * @param {Principal} other - a {@link Principal} to compare

compareTo(other) {
return Principal.compare(this, other);
}
static compare(p1, p2) {
const p1Arr = p1.toUint8Array();
const p2Arr = p2.toUint8Array();
for (let i = 0; i < Math.min(p1Arr.length, p2Arr.length); i++) {
if (p1Arr[i] < p2Arr[i])
for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) {
if (this._arr[i] < other._arr[i])
return 'lt';
else if (p1Arr[i] > p2Arr[i])
else if (this._arr[i] > other._arr[i])
return 'gt';
}
// Here, at least one principal is a prefix of the p2 principal (they could be the same)
if (p1Arr.length < p2Arr.length)
// Here, at least one principal is a prefix of the other principal (they could be the same)
if (this._arr.length < other._arr.length)
return 'lt';
if (p1Arr.length > p2Arr.length)
if (this._arr.length > other._arr.length)
return 'gt';

@@ -149,0 +124,0 @@ return 'eq';

@@ -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": "1.0.0-beta.0",
"version": "1.0.0",
"author": "DFINITY Stiftung <sdk@dfinity.org>",
"license": "Apache-2.0",
"description": "JavaScript and TypeScript library to work with Internet Computer principals",
"homepage": "https://smartcontracts.org",
"homepage": "https://internetcomputer.org",
"repository": {

@@ -36,30 +36,37 @@ "type": "git",

"module": "./lib/esm/index.js",
"unpkg": "./lib/esm/index",
"scripts": {
"build": "tsc -b && tsc -p tsconfig-cjs.json",
"bundle": "npm run build",
"bundle": "esbuild --bundle src/index.ts --outfile=dist/index.js",
"size-limit": "size-limit",
"lint": "eslint 'src' --ext '.js,.jsx,.ts,.tsx'",
"lint:fix": "npm run lint -- --fix",
"make:docs/reference": "typedoc src/index.ts --out ../../docs/generated/principal --excludeInternal",
"release": "npm publish",
"test": "jest",
"test:coverage": "jest --verbose --collectCoverage"
"test:coverage": "jest --collectCoverage"
},
"devDependencies": {
"@dfinity/principal-legacy": "npm:@dfinity/principal@^0.9.3",
"@types/jest": "^28.1.4",
"@dfinity/utils": "^2.0.0",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"esbuild": "^0.15.16",
"eslint": "^8.19.0",
"eslint-plugin-jsdoc": "^39.3.3",
"jest": "^28.1.2",
"jest": "^29.7.0",
"size-limit": "^8.2.6",
"text-encoding": "^0.7.0",
"ts-jest": "^28.0.5",
"tslint": "^5.20.0",
"ts-node": "^10.8.2",
"typedoc": "^0.22.11",
"typescript": "^4.2.3",
"whatwg-fetch": "^3.0.0"
},
"size-limit": [
{
"path": "./dist/index.js",
"limit": "100 kB",
"webpack": false
}
],
"dependencies": {
"ts-node": "^10.8.2"
"@noble/hashes": "^1.3.1"
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc