diff --git a/docs/generated/changelog.html b/docs/generated/changelog.html
index a100a90d0..12f33a7ed 100644
--- a/docs/generated/changelog.html
+++ b/docs/generated/changelog.html
@@ -12,6 +12,7 @@
Agent-JS Changelog
Version x.x.x
+ - feat: Principal class serializes to JSON
-
feat: certificate checks validate that certificate time is not more than 5 minutes ahead
of or behind system time.
diff --git a/packages/agent/src/canisterStatus/__snapshots__/index.test.ts.snap b/packages/agent/src/canisterStatus/__snapshots__/index.test.ts.snap
index 745b0263b..d502985f8 100644
--- a/packages/agent/src/canisterStatus/__snapshots__/index.test.ts.snap
+++ b/packages/agent/src/canisterStatus/__snapshots__/index.test.ts.snap
@@ -2,21 +2,7 @@
exports[`Canister Status utility should query canister controllers 1`] = `
Array [
- Principal {
- "_arr": Uint8Array [
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 1,
- 1,
- ],
- "_isPrincipal": true,
- },
+ "rwlgt-iiaaa-aaaaa-aaaaa-cai",
]
`;
@@ -36,21 +22,7 @@ exports[`Canister Status utility should support multiple requests 1`] = `2022-05
exports[`Canister Status utility should support multiple requests 2`] = `
Array [
- Principal {
- "_arr": Uint8Array [
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 1,
- 1,
- ],
- "_isPrincipal": true,
- },
+ "rwlgt-iiaaa-aaaaa-aaaaa-cai",
]
`;
diff --git a/packages/principal/src/index.test.ts b/packages/principal/src/index.test.ts
index e549a4417..92a9d510d 100644
--- a/packages/principal/src/index.test.ts
+++ b/packages/principal/src/index.test.ts
@@ -41,4 +41,15 @@ describe('Principal', () => {
expect(anonymous.compareTo(principal1)).toBe('gt');
expect(anonymous.compareTo(principal2)).toBe('gt');
});
+
+ it('serializes to String constructor', () => {
+ const principal = Principal.fromText('ryjl3-tyaaa-aaaaa-aaaba-cai');
+ expect(principal.toString()).toBe('ryjl3-tyaaa-aaaaa-aaaba-cai');
+ expect(String(principal)).toBe('ryjl3-tyaaa-aaaaa-aaaba-cai');
+ });
+
+ it('serializes to JSON', () => {
+ const principal = Principal.fromText('ryjl3-tyaaa-aaaaa-aaaba-cai');
+ expect(JSON.stringify(principal)).toBe('"ryjl3-tyaaa-aaaaa-aaaba-cai"');
+ });
});
diff --git a/packages/principal/src/index.ts b/packages/principal/src/index.ts
index 857282a60..92c19045f 100644
--- a/packages/principal/src/index.ts
+++ b/packages/principal/src/index.ts
@@ -107,6 +107,14 @@ export class Principal {
return this.toText();
}
+ /**
+ * Serializes to JSON
+ * @returns {string} string
+ */
+ public toJSON(): string {
+ return this.toText();
+ }
+
/**
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification
* @param {Principal} other - a {@link Principal} to compare