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 Provider and Signer #51

Merged
merged 5 commits into from
Nov 9, 2021
Merged

Introduce Provider and Signer #51

merged 5 commits into from
Nov 9, 2021

Conversation

janek26
Copy link
Member

@janek26 janek26 commented Nov 8, 2021

The API of the starknet package got a bit out of hand with more and more utility and types.
closes #49

Inspired by ethers I introduced the concept of providers and signers, which can also get passed into more high level abstractions, such as the Contract class.

Provider

To keep it simple (and because there's currently just one network goerli-alpha3) it also exports a defaultProvider from the very top level. So you can still getting started by doing

import { defaultProvider } from 'starknet';

defaultProvider.getContractAddresses().then((data) => {
  console.log(data);
});

using a provider (normally providers are read only, but as you can write to starknet without signing, providers can also write to starknet using senderAddress = 0) you can specify the network you wanna use, in preparation for main net launch.

import { Provider } from 'starknet';

// not supported right now, as main net isn't online yet
const mainnetProvider = new Provider({ network: "mainnet" });

mainnetProvider.getContractAddresses().then((data) => {
  console.log(data);
});

Signer

Signer can be used to intercept specific methods from a provider, like addTransaction to add a signature to every call. This is useful to interact with standard wallet implementations on starknet, like open zeppelin and argent.
Signers can be provided by ie an extension, and their async methods can require user interactions with the extension before they resolve. A very simple Signer is shipped with starknet.js:

import { Signer, defaultProvider, ec } from 'starknet';

// random key pair 
const starkKeyPair = ec.genKeyPair();

// sign every request with a local key pair and invoke it through an `execute` endpoint on the given address (default wallet implementation)
const localSigner = new Signer(defaultProvider, WALLET_ADDRESS , starkKeyPair);

localSigner.getContractAddresses().then((data) => {
  console.log(data);
  // the signer could intercept this function call and append some signer specific logic
  localSigner.invokeFunction(contractAddress, entrypoint, calldata).then(response => {
    console.log("invokeResult:", response);
  });
});

As a result this PR changes the way many functions are exported, therefore this is considered a BREAKING CHANGE and MAJOR version bump.

@janek26 janek26 added Type: feature New feature or request MAJOR labels Nov 8, 2021
@janek26 janek26 requested a review from 0xs34n November 8, 2021 21:07
@janek26 janek26 self-assigned this Nov 8, 2021
@janek26 janek26 linked an issue Nov 8, 2021 that may be closed by this pull request
@0xs34n
Copy link
Collaborator

0xs34n commented Nov 9, 2021

Hey @janek26 , at first glance I really love this implementation. I think it strikes a great balance between standard defaults and also flexibility for developers. I will play around with it (hopefully this weekend) and let you know my feedback and merge it. Thanks!

__tests__/account/account.test.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/provider/default.ts Show resolved Hide resolved
src/provider/default.ts Outdated Show resolved Hide resolved
src/signer/default.ts Outdated Show resolved Hide resolved
@0xs34n
Copy link
Collaborator

0xs34n commented Nov 9, 2021

Thanks, @delaaxe for reviewing! @janek26 feel free to merge to dev when it's ready.

@janek26 janek26 merged commit 61ac0ae into develop Nov 9, 2021
@janek26 janek26 deleted the feature/provider-signer branch November 9, 2021 12:39
@github-actions
Copy link

github-actions bot commented Nov 9, 2021

🎉 This PR is included in version 1.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

invokeFunction() on starknet.ts
3 participants