-
Notifications
You must be signed in to change notification settings - Fork 89
Redacted + improved privacy of private keys #404
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) Mysten Labs, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| type RedactedRegistry = WeakMap<Redacted<any>, any>; | ||
|
|
||
| const moduleRedactedRegistry: RedactedRegistry = new WeakMap(); | ||
|
|
||
| const REDACTED_REGISTRY_KEY = Symbol.for('@mysten/redacted/registry'); | ||
| function getRedactedRegistry() { | ||
| try { | ||
| const target = globalThis as { | ||
| [REDACTED_REGISTRY_KEY]?: RedactedRegistry; | ||
| }; | ||
|
|
||
| if (!target[REDACTED_REGISTRY_KEY]) { | ||
| target[REDACTED_REGISTRY_KEY] = moduleRedactedRegistry; | ||
| } | ||
|
|
||
| return target[REDACTED_REGISTRY_KEY]; | ||
| } catch (e) { | ||
| return moduleRedactedRegistry; | ||
| } | ||
| } | ||
|
|
||
| const RedactedType: unique symbol = Symbol.for('@mysten/redacted'); | ||
| export interface Redacted<T> { | ||
| [RedactedType]: T; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might not work well across sdk versions. I think when we introduced symbols for Transaction and/or SuiClient properties it caused some issues. I'm not sure how
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think you're right. I just was trying to hide any public API from this. I can just tack on a string property or something though. |
||
| } | ||
|
|
||
| export function getRedactedValue<T>(redacted: Redacted<T>): T { | ||
| const redactedRegistry = getRedactedRegistry(); | ||
|
|
||
| if (redactedRegistry.has(redacted)) { | ||
| return redactedRegistry.get(redacted) as T; | ||
| } else { | ||
| throw new Error('Unable to get redacted value'); | ||
| } | ||
| } | ||
|
|
||
| export function redacted<T>(value: T): Redacted<T> { | ||
| const redactedRegistry = getRedactedRegistry(); | ||
|
|
||
| const redacted = { | ||
| [RedactedType]: true, | ||
| [Symbol.toStringTag]: 'Redacted', | ||
| [Symbol.for('nodejs.util.inspect.custom')]: () => '<redacted>', | ||
| toString() { | ||
| return '<redacted>'; | ||
| }, | ||
| toJSON() { | ||
| return '<redacted>'; | ||
| }, | ||
| }; | ||
|
|
||
| redactedRegistry.set(redacted, value); | ||
|
|
||
| return redacted as never; | ||
| } | ||
|
|
||
| export function isRedacted(value: any): value is Redacted<any> { | ||
| return typeof value === 'object' && Object.hasOwn(value, RedactedType); | ||
| } | ||
|
|
||
| /** @internal */ | ||
| export function getRedactedOrPlainValue<T>(value: T | Redacted<T>): T { | ||
| if (isRedacted(value)) { | ||
| return getRedactedValue(value); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can just be a concrete implementation right?