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

Introduce transact plugin hooks #1019

Merged
merged 5 commits into from
Jan 29, 2025
Merged

Conversation

James-Mart
Copy link
Member

@James-Mart James-Mart commented Jan 28, 2025

Transact plugin hooks

Adds hooks to the transact plugin, allowing third-party apps to "hook" into various functionality to dynamically modify the process of transaction construction and authorization.

For example:

  • The invite plugin uses hooks to ensure that its users are able to sign and submit transactions as the invited-sys user with custom auth claims/proofs in order to create their first account.
  • The auth-sig plugin uses hooks to add claims/proofs
  • The staged-tx plugin will use hooks to wrap action into staged-tx proposals

Transaction construction lifecycle with hooks

The transaction construction cycle, including hooks, is as follows:

  • start-tx

  • add-action-to-transaction

    • on-action-sender - the hooked plugin can set the sender of the action, otherwise the logged-in user is used by default
    • on-action-auth-claims - the hooked plugin can add claims for this action
    • save action details
  • finish-tx

    • on-user-claim - the user auth plugin adds the user claim
    • on-tx-transform - the hooked plugin can transform the set of actions in the transaction
    • construct transaction
    • hash-transaction
    • on-user-auth-proof - the user auth plugin adds the user proof
    • on-action-auth-proofs - the hooked plugin can add proofs for their action claims
    • publish transaction

Hook worlds

The transact plugin's wit file contains worlds for using various hooks. Users of these hooks simply include the world corresponding to their hook, and their toolchain will enforce that their plugin exports the necessary functionality to be compatible with the transact plugin's expectations.

world hook-action-auth {
    import hooks;
    export transact-hook-action-auth: interface {
        use host:common/types.{error};
        use types.{claim, proof, action};
        on-action-auth-claims: func(action: action) -> result<list<claim>, error>;
        on-action-auth-proofs: func(claims: list<claim>, transaction-hash: list<u8>) -> result<list<proof>, error>;
    }
}

Support inline interfaces

Allows the component parser (used by supervisor to understand plugins) to support plugins with interfaces defined inline in a world, rather than requiring them to be defined outside of a world and pulled in.

Better adherence to the wit world spec.

@James-Mart
Copy link
Member Author

@brandonfancher adding you as a reviewer because of the updates to the supervisor / plugin loading code

@James-Mart James-Mart added the Dev Experience Related to the experience of developers label Jan 28, 2025
Copy link
Contributor

Choose a reason for hiding this comment

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

Deleted code is tested code 🔥

Comment on lines 87 to 104
let user = this.getLoggedInUser();
if (!!user) {
const account = this.getAccount(user);
if (account === undefined) {
console.warn(
`Invalid user account '${user}' detected. Automatically logging out.`,
);
this.logout();
// Consider deleting the user from the accounts plugin db
} else {
// Current limitation: an auth service plugin must be called "plugin" ("<service>:plugin")
this.loader.trackPlugins([
pluginId(account.authService, "plugin"),
]);
}
await this.loader.processPlugins();
await this.loader.awaitReady();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are lines 102 and 103 supposed to run even when the user account is invalid?

Assuming they are not and assuming you're okay with early returns, this all could be:

// Phase 2: Loads plugins needed by the current user
const user = this.getLoggedInUser();
if (!user) return;

const account = this.getAccount(user);
if (account === undefined) {
    console.warn(`Invalid user account '${user}' detected. Automatically logging out.`);
    this.logout();
    // Consider deleting the user from the accounts plugin db
    return;
}

// Current limitation: an auth service plugin must be called "plugin" ("<service>:plugin")
this.loader.trackPlugins([
    pluginId(account.authService, "plugin"),
]);
await this.loader.processPlugins();
await this.loader.awaitReady();

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, refactored

}

let action_groups: Vec<ActionGroup> = partition_by_label(actions);
let transformer_plugin = TxTransformLabel::get_transformer_plugin().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

let Some() or if let Some() is better than a separate test and unwrap.

@James-Mart James-Mart merged commit 06b2daf into main Jan 29, 2025
3 checks passed
@James-Mart James-Mart deleted the staged-tx/use-transact-plugin-hooks branch January 29, 2025 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dev Experience Related to the experience of developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants