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: allow option set agent replica time #923

Merged
merged 9 commits into from
Sep 11, 2024
Merged

feat: allow option set agent replica time #923

merged 9 commits into from
Sep 11, 2024

Conversation

krpeacock
Copy link
Contributor

@krpeacock krpeacock commented Sep 9, 2024

Description

Adds a new feature to set and read a specified replicaTime on the HttpAgent. This is useful in situations where the device clock may not be accurate, such as in an airgapped device, since the agent needs to agree with the replica when creating an Expiry or validating certificates.

Also improves the error handling for replica time errors, throwing a new ReplicaTimeError, containing a reference back to the agent that threw it, as well as a parsed replicaTime Date object from the error message.

This replaces the offset strategy previously used.

examples

// set replica time
const agent = HttpAgent.createSync();
agent.replicaTime = new Date('2021-01-01T00:00:00Z');
// read replica time
const agent = HttpAgent.createSync();
console.log(agent.replicaTime);
// catch time error
try {
  // should throw an error
  await actor.whoami();
} catch (err) {
  // handle the replica time error. "name" will be 'ReplicaTimeError'
  if (err.name === 'ReplicaTimeError') {
    console.log(err.agent);
    console.log(err.replicaTime);
    handleErr(err);
  }
}

Important: the error response cannot be certified. Using it directly could subject users to replay attacks

Fixes SDK-1812

How Has This Been Tested?

New e2e tests

Checklist:

  • My changes follow the guidelines in CONTRIBUTING.md.
  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

Copy link
Contributor

github-actions bot commented Sep 9, 2024

size-limit report 📦

Path Size
@dfinity/agent 86.33 KB (+0.43% 🔺)
@dfinity/candid 13.58 KB (0%)
@dfinity/principal 4.97 KB (0%)
@dfinity/auth-client 60.42 KB (+0.1% 🔺)
@dfinity/assets 81.14 KB (+0.19% 🔺)
@dfinity/identity 57.65 KB (+0.16% 🔺)
@dfinity/identity-secp256k1 131.89 KB (+0.57% 🔺)

@krpeacock krpeacock changed the title feat: allow option to sync with replica time using errors feat: allow option set agent replica time Sep 11, 2024
@krpeacock krpeacock marked this pull request as ready for review September 11, 2024 00:19
@krpeacock krpeacock requested a review from a team as a code owner September 11, 2024 00:19
@krpeacock krpeacock merged commit a973a4d into main Sep 11, 2024
16 checks passed
@krpeacock krpeacock deleted the kai/sync-time branch September 11, 2024 21:25
dfx-json added a commit that referenced this pull request Sep 27, 2024
dfx-json added a commit that referenced this pull request Sep 30, 2024
* Revert "feat: allow option set agent replica time (#923)"

This reverts commit a973a4d.

* chore: override rollup version to resolve vulnerability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants