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 Signer interface #1308

Merged
merged 2 commits into from
Apr 14, 2022
Merged

[ts-sdk] Define the Signer interface #1308

merged 2 commits into from
Apr 14, 2022

Conversation

666lcz
Copy link
Contributor

@666lcz 666lcz commented Apr 9, 2022

As mentioned below, this PR defines the "Signer" interface for the Typescript SDK. The subsequent PRs will add missing implementations for:

  • "Provider" interface methods against the new JSON-RPC server written by @patrickkuo
  • Public key -> Sui Address translation
  • Integrate with existing apps

Use cases

Some example scenarios where this SDK might be used:

  • Web Applications(e.g., pure Javascript app, or Unity + WebGL + Non-custodial Wallet)
  • NodeJS Server + raw keypair(e.g., the key is stored as an environment variable)
  • NodeJS Server + pure signing service(that does nothing except signing)
  • Implementing a browser extension based wallet

API Design

The API design takes inspiration from Ethers.js and separates the API into two different parts:

  • A “provider” that serves as an anonymous connection to the Sui network, checking state and sending transactions
  • A “signer” that holds keys and signs transaction

In addition, we also expose a TxDataSerializer API that allows the caller to construct a BCS encoded transaction data. Application developers should not need to use this directly.

DevNet API

// Provider API
// mirror https://github.com/MystenLabs/sui/blob/7c30c0a99102e46c47d7009d700e0617b60e7662/sui/src/rest_server.rs#L106
get_objects
object_schema
object_info
sync_account_state
execute_transaction

// Signer API
sign_message
// Convenience method for signing and executing the transaction
// it uses the provider under the hood
sign_and_execute_transaction
// Below are convenience methods that are part of the Signer API.
// They use the Raw Transasction Builder API under the hood
transfer
split_coin
merge_coin
publish_module
execute_move_call

// Transaction Data Serializer API to get the BCS encoded data
// Clients most likely do not need to use these directly
new_transfer
split_coin
merge_coin
publish
move_call

Future changes

  • execute_move_call will be deprecated in favor of ABI generated contract API
  • Transaction Data Serializer API currently needs to talk to the JSON-RPC server to get the BCS encoded transaction data. We should try to implement this by constructing the BCS encoded transaction data locally to avoid this extra round trip.

Usage of the SDK

// NFT marketplace with non-custodial wallet
const provider = new sui.providers.WalletProvider(window.sui);
const signer = provider.getSigner();
signer.execute_move_call();

// Node JS server + raw key pair
const provider = new sui.providers.JsonRpcProvider(gateway_url);
const signer = new sui.signers.RawSigner(keypair, provider);
signer.execute_move_call();

// Node JS server that uses a pure signing service
const provider = new sui.providers.JsonRpcProvider(gateway_url);
const signer = new sui.signers.RpcSigner(signing_service_url, provider, address);
signer.execute_move_call();

// Node JS server that uses a signing service that also supports gateway
const provider = new sui.providers.CustodialRpcProvider(signing_service_url);
const signer = provider.getSigner(address);
signer.execute_move_call();

// Implementing a browser-extension based wallet
const provider = new sui.providers.JsonRpcProvider(gateway_url);
// the wallet has direct access to the keypair
const signer = new sui.signers.RawSigner(keypair, provider);
signer.execute_move_call();

Testing

  • Added unit tests
  • Manual testing on the Signer API
  1. Write a React App with this component, which uses the sui.js sdk
  2. Use npm link to link sui.js to the React App
    CleanShot 2022-04-09 at 16 05 34

@codecov
Copy link

codecov bot commented Apr 9, 2022

Codecov Report

Merging #1308 (8e0f6d4) into main (4d09cb9) will increase coverage by 0.02%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1308      +/-   ##
==========================================
+ Coverage   81.72%   81.75%   +0.02%     
==========================================
  Files         100      100              
  Lines       20966    20972       +6     
==========================================
+ Hits        17134    17145      +11     
+ Misses       3832     3827       -5     
Impacted Files Coverage Δ
...i_programmability/verifier/src/id_leak_verifier.rs 90.58% <0.00%> (-0.45%) ⬇️
sui_core/src/authority/temporary_store.rs 83.52% <0.00%> (-0.40%) ⬇️
sui/src/rest_server.rs 23.43% <0.00%> (-0.34%) ⬇️
sui_types/src/object.rs 81.49% <0.00%> (-0.26%) ⬇️
...ammability/adapter/src/unit_tests/adapter_tests.rs 98.45% <0.00%> (-0.11%) ⬇️
sui/src/wallet_commands.rs 79.37% <0.00%> (ø)
sui_core/src/authority_aggregator.rs 87.31% <0.00%> (+0.60%) ⬆️
sui_core/src/safe_client.rs 60.00% <0.00%> (+4.76%) ⬆️

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 4d09cb9...8e0f6d4. Read the comment docs.

@oxade oxade self-requested a review April 11, 2022 01:28
@666lcz 666lcz changed the title [ts-sdk] Define SDK interface [ts-sdk] Define the Signer 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.

looks good, few minor questions but could be merged as-is

@666lcz 666lcz merged commit 7a4e7a1 into main Apr 14, 2022
@666lcz 666lcz deleted the chris/provider branch April 14, 2022 00:12
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