Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Principal class serializes to JSON #766

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
<section>
<h2>Version x.x.x</h2>
<ul>
<li>feat: Principal class serializes to JSON</li>
<li>
feat: certificate checks validate that certificate time is not more than 5 minutes ahead
of or behind system time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`;

Expand All @@ -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",
]
`;

Expand Down
11 changes: 11 additions & 0 deletions packages/principal/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"');
});
});
8 changes: 8 additions & 0 deletions packages/principal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export class Principal {
return this.toText();
}

/**
* Serializes to JSON
* @returns {string} string
*/
public toJSON(): string {
return this.toText();
Copy link
Member

@peterpeterparker peterpeterparker Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a bit weak? It means that any string is either a principal or a string which can be quite error prone if you stringify and parse an object that contains string and principals.

In @dfinity/utils we prefix principal to make their encoding and decoding more safe by using JSON reviver and replacer.

https://github.com/dfinity/ic-js/blob/main/packages/utils/src/utils/json.utils.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could serialize into { "__principal__": principal.toText() } instead. The main case I wanted to cover was just ensuring that JSON.stringify() on objects containing principals handles the case gracefully

}

/**
* Utility method taking a Principal to compare against. Used for determining canister ranges in certificate verification
* @param {Principal} other - a {@link Principal} to compare
Expand Down