-
Notifications
You must be signed in to change notification settings - Fork 345
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: make account parser implementation extendable #1530
Open
0xpatrickdev
wants to merge
4
commits into
cosmos:main
Choose a base branch
from
0xpatrickdev:feat/extendable-account-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−39
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffb1fa1
feat: make accountParser implementation extendable
0xpatrickdev 4cbb028
feat: add tests for createAccountParserRegistry and AccountParserManager
0xpatrickdev 9ba311a
feat: export createAccountParserRegistry function, AccountParserManag…
0xpatrickdev 53afd70
docs: add jsdoc descriptions for exported account functions
0xpatrickdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -40,49 +40,103 @@ function accountFromBaseAccount(input: BaseAccount): Account { | |||||||||||||
*/ | ||||||||||||||
export type AccountParser = (any: Any) => Account; | ||||||||||||||
|
||||||||||||||
export type AccountParserRegistry = Map<Any["typeUrl"], AccountParser>; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Basic implementation of AccountParser. This is supposed to support the most relevant | ||||||||||||||
* common Cosmos SDK account types. If you need support for exotic account types, | ||||||||||||||
* you'll need to write your own account decoder. | ||||||||||||||
* AccountParserManager is a class responsible for managing a registry of account parsers. | ||||||||||||||
* It allows registering new parsers and parsing account data using registered parsers. | ||||||||||||||
* Once initialized, `AccountParserManager.parseAccount` can be provided for the `accountParser` | ||||||||||||||
* option when initializing the StargateSigningCleint. | ||||||||||||||
*/ | ||||||||||||||
export function accountFromAny(input: Any): Account { | ||||||||||||||
const { typeUrl, value } = input; | ||||||||||||||
export class AccountParserManager { | ||||||||||||||
private readonly registry = new Map<Any["typeUrl"], AccountParser>(); | ||||||||||||||
|
||||||||||||||
public constructor(initialRegistry: AccountParserRegistry = new Map()) { | ||||||||||||||
this.registry = initialRegistry; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
switch (typeUrl) { | ||||||||||||||
// auth | ||||||||||||||
/** Registers a new account parser for a specific typeUrl. */ | ||||||||||||||
public register(typeUrl: Any["typeUrl"], parser: AccountParser): void { | ||||||||||||||
this.registry.set(typeUrl, parser); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
case "/cosmos.auth.v1beta1.BaseAccount": | ||||||||||||||
return accountFromBaseAccount(BaseAccount.decode(value)); | ||||||||||||||
case "/cosmos.auth.v1beta1.ModuleAccount": { | ||||||||||||||
const baseAccount = ModuleAccount.decode(value).baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
/** | ||||||||||||||
* Parses an account from an `Any` encoded format using a registered parser based on the typeUrl. | ||||||||||||||
* @throws Will throw an error if no parser is registered for the account's typeUrl. | ||||||||||||||
*/ | ||||||||||||||
public parseAccount(input: Any): Account { | ||||||||||||||
const parser = this.registry.get(input.typeUrl); | ||||||||||||||
if (!parser) { | ||||||||||||||
throw new Error(`Unsupported type: '${input.typeUrl}'`); | ||||||||||||||
} | ||||||||||||||
return parser(input); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// vesting | ||||||||||||||
/** | ||||||||||||||
* Creates and returns a default registry of account parsers. | ||||||||||||||
* Each parser is a function responsible for converting an `Any` encoded account | ||||||||||||||
* from the chain into a common `Account` format. The registry maps `typeUrl` | ||||||||||||||
* strings to corresponding `AccountParser` functions. | ||||||||||||||
*/ | ||||||||||||||
export function createAccountParserRegistry(): AccountParserRegistry { | ||||||||||||||
const parseBaseAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
return accountFromBaseAccount(BaseAccount.decode(value)); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
case "/cosmos.vesting.v1beta1.BaseVestingAccount": { | ||||||||||||||
const baseAccount = BaseVestingAccount.decode(value)?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
} | ||||||||||||||
case "/cosmos.vesting.v1beta1.ContinuousVestingAccount": { | ||||||||||||||
const baseAccount = ContinuousVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
} | ||||||||||||||
case "/cosmos.vesting.v1beta1.DelayedVestingAccount": { | ||||||||||||||
const baseAccount = DelayedVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
} | ||||||||||||||
case "/cosmos.vesting.v1beta1.PeriodicVestingAccount": { | ||||||||||||||
const baseAccount = PeriodicVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
} | ||||||||||||||
const parseModuleAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
const baseAccount = ModuleAccount.decode(value).baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
default: | ||||||||||||||
throw new Error(`Unsupported type: '${typeUrl}'`); | ||||||||||||||
} | ||||||||||||||
const parseBaseVestingAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
const baseAccount = BaseVestingAccount.decode(value)?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
const parseContinuousVestingAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
const baseAccount = ContinuousVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
const parseDelayedVestingAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
const baseAccount = DelayedVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
const parsePeriodicVestingAccount: AccountParser = ({ value }: Any): Account => { | ||||||||||||||
const baseAccount = PeriodicVestingAccount.decode(value)?.baseVestingAccount?.baseAccount; | ||||||||||||||
assert(baseAccount); | ||||||||||||||
return accountFromBaseAccount(baseAccount); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
return new Map<Any["typeUrl"], AccountParser>([ | ||||||||||||||
["/cosmos.auth.v1beta1.BaseAccount", parseBaseAccount], | ||||||||||||||
["/cosmos.auth.v1beta1.ModuleAccount", parseModuleAccount], | ||||||||||||||
["/cosmos.vesting.v1beta1.BaseVestingAccount", parseBaseVestingAccount], | ||||||||||||||
["/cosmos.vesting.v1beta1.ContinuousVestingAccount", parseContinuousVestingAccount], | ||||||||||||||
["/cosmos.vesting.v1beta1.DelayedVestingAccount", parseDelayedVestingAccount], | ||||||||||||||
["/cosmos.vesting.v1beta1.PeriodicVestingAccount", parsePeriodicVestingAccount], | ||||||||||||||
]); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Basic implementation of AccountParser. This is supposed to support the most relevant | ||||||||||||||
* common Cosmos SDK account types. If you need support for additional account types, | ||||||||||||||
* you'll need to use `AccountParserManager` and `createAccountParserRegistry` directly. | ||||||||||||||
* | ||||||||||||||
* @example | ||||||||||||||
* ``` | ||||||||||||||
* const myAccountParserManager = new AccountParserManager(createAccountParserRegistry()); | ||||||||||||||
* myAccountParserManager.register('/custom.type.v1.CustomAccount', customAccountParser); | ||||||||||||||
* const account = customParserManager.parseAccount(someInput); | ||||||||||||||
Comment on lines
+134
to
+136
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.
Suggested change
This probably aligns closer to expected usage |
||||||||||||||
* ``` | ||||||||||||||
*/ | ||||||||||||||
export function accountFromAny(input: Any): Account { | ||||||||||||||
const accountParser = new AccountParserManager(createAccountParserRegistry()); | ||||||||||||||
return accountParser.parseAccount(input); | ||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Realizing we may also want to export
accountFromBaseAccount
(L26-L35), as this will likely be needed by those supplying a custom account parser. Alternatively, this could be baked into everyparseAccount
call as every AccountParser function likely needs this as the last step.Will wait for feedback on this and the overall design before making any updates.