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

[ts-sdk] Define the Provider Interface #1331

Merged
merged 3 commits into from
Apr 15, 2022
Merged

[ts-sdk] Define the Provider Interface #1331

merged 3 commits into from
Apr 15, 2022

Conversation

666lcz
Copy link
Contributor

@666lcz 666lcz commented Apr 12, 2022

For more context, please see https://github.com/MystenLabs/sui/pull/1308/files.

This PR defines the barebone interface for Provider. Subsequent PRs will add more methods and implement the method using a JSON-RPC client

@666lcz 666lcz changed the title [WIP][ts-sdk] Define the Provider Interface [ts-sdk] Define the Provider Interface Apr 12, 2022
Copy link
Contributor

@stella3d stella3d left a comment

Choose a reason for hiding this comment

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

I think we should change the way this is setup to eliminate the need for null/undefined checking in the signer (by making the constructor set the values).

async transfer(
transaction: TransferTransaction
): Promise<TransactionResponse> {
this._checkProviderAndSerializer('transfer');
Copy link
Contributor

@stella3d stella3d Apr 13, 2022

Choose a reason for hiding this comment

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

it seems like we're always checking for prerequisite objects before doing anything, but i'm not sure we gain any significant flexibility by allowing these objects to be null/undefined at all.

Can we set the provider & serializer in the constructor (they're already readonly)?
that way they're always defined.
If users want to change the serializer or provider, they can construct a new signer class instance.

Copy link
Contributor Author

@666lcz 666lcz Apr 14, 2022

Choose a reason for hiding this comment

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

Some use cases might just need a signer without a provider, e.g., to produce pre-signed message, to prove that the user owns this address, etc. This latter use case is very popular in some portfolio tracking applications such as https://debank.com/.

My demo app also uses similar pattern: https://gist.github.com/666lcz/6cc6c983e841eda9f7a457e47bc473b7 without a provider.

Ether.js is also using a similar pattern: https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/abstract-signer/src.ts/index.ts#L98

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, now that i think about this some more - I think it seems like we should completely remove the provider from the signer. Signing & sending the tx seem like two separate concerns.

I think the ideal signer might be something that implements only

interface Signer { 
    signData(data: Base64DataBuffer): Promise<SignaturePubkeyPair>
}

Base automatically changed from chris/provider to main April 14, 2022 00:12
@codecov
Copy link

codecov bot commented Apr 14, 2022

Codecov Report

Merging #1331 (5c5fef6) into main (7a4e7a1) will increase coverage by 0.62%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1331      +/-   ##
==========================================
+ Coverage   81.79%   82.41%   +0.62%     
==========================================
  Files         100      103       +3     
  Lines       20972    20801     -171     
==========================================
- Hits        17153    17143      -10     
+ Misses       3819     3658     -161     
Impacted Files Coverage Δ
sui_programmability/verifier/src/lib.rs 60.34% <0.00%> (-39.66%) ⬇️
sui/src/sui_json.rs 81.74% <0.00%> (-8.52%) ⬇️
sui_programmability/adapter/src/adapter.rs 89.35% <0.00%> (-2.94%) ⬇️
sui_core/src/consensus_client.rs 61.26% <0.00%> (-1.33%) ⬇️
sui/src/lib.rs 83.33% <0.00%> (ø)
sui_types/src/object.rs 81.49% <0.00%> (ø)
sui_core/src/authority.rs 94.95% <0.00%> (ø)
test_utils/src/sequencer.rs 78.35% <0.00%> (ø)
sui/src/unit_tests/sui_json.rs 100.00% <0.00%> (ø)
sui_core/src/execution_engine.rs 100.00% <0.00%> (ø)
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a4e7a1...5c5fef6. Read the comment docs.

@666lcz
Copy link
Contributor Author

666lcz commented Apr 15, 2022

Addressed @stella3d 's comment by:

  • defining a Signer interface that is provider free
  • Rename the original Signer abstract class to SignerWithProvider to provide an ergonomic interface for the API caller(see example below)
  • Added a VoidProvider so that we do not need to add checks to see if the provider is null

With these changes, we keep the Signer interface clean while also providing an ergonomic interface for the API caller.

The code snippets below will demonstrate why having a SignerWithProvider is more ergonomic than always having the Signer and the Provider separate

With the SignerWithProvider

// NFT marketplace with non-custodial wallet
const provider = new sui.providers.WalletProvider(window.sui);
// for operations that does not need signing
const address = provider.getAddress();

// for operations that need signing
const signer = provider.getSigner();
signer.execute_move_call();

If we always have the Provider and Signer separate:

const provider = new sui.providers.WalletProvider(window.sui);
// for operations that does not need signing
const address = provider.getAddress();

// for operations that need signing
const data = provider.move_call_data();
const signer = provider.getSigner();
const signedData = signer.signData(data)
provider.execute(signedData)

@666lcz 666lcz requested a review from stella3d April 15, 2022 05:03
Copy link
Contributor

@stella3d stella3d left a comment

Choose a reason for hiding this comment

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

This latest update looks good, it addresses my previous concerns with initialization of dependency objects. great work

@666lcz 666lcz merged commit d806371 into main Apr 15, 2022
@666lcz 666lcz deleted the chris/ts-rpc branch April 15, 2022 22:18
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.

3 participants