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

feat: deprecate Network, use explicit passphrase (2) #207

Merged
merged 8 commits into from Aug 1, 2019

Conversation

Akuukis
Copy link
Contributor

@Akuukis Akuukis commented Jul 19, 2019

Deprecate global singleton for Network. The following classes and methods take an optional network passphrase, and issue a warning if it is not passed:

Keypair.master

Keypair.master(Networks.TESTNET)

constructor for Transaction

const xenv = new xdr.TransactionEnvelope({ tx: xtx });
new Transaction(xenv, Networks.TESTNET);

constructor for TransactionBuilder and method TransactionBuilder.setNetworkPassphrase

const transaction = new StellarSdk.TransactionBuilder(account, {
  fee: StellarSdk.BASE_FEE,
  networkPassphrase: Networks.TESTNET
})

The Network class will be removed on the 2.0 release.

Fixes #112.

Copy link
Contributor

@abuiles abuiles left a comment

Choose a reason for hiding this comment

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

@Akuukis Looking good!

Besides the inline comments, could you do the following:

We also need to update the docs to make sure they reflect the new syntax, but it doesn't block this PR.

Thanks!

src/transaction_builder.js Outdated Show resolved Hide resolved
@Akuukis
Copy link
Contributor Author

Akuukis commented Jul 26, 2019

@abuiles Done! Anything else?

Copy link
Contributor

@abuiles abuiles left a comment

Choose a reason for hiding this comment

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

@Akuukis looking good! One last change and I think we'll be good :). Let's avoid the breaking change for now and use a getter approach.

docs/reference/building-transactions.md Outdated Show resolved Hide resolved
src/transaction.js Outdated Show resolved Hide resolved
docs/reference/building-transactions.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@Akuukis
Copy link
Contributor Author

Akuukis commented Jul 31, 2019

@abuiles Review implemented, and all tests files reverted too (so no breaking changes noticed). Anything else?

@abuiles
Copy link
Contributor

abuiles commented Jul 31, 2019

@Akuukis Thanks! I'll release this later today :)

src/network.js Show resolved Hide resolved
@abuiles abuiles merged commit 46c4349 into stellar:master Aug 1, 2019
@MisterTicot
Copy link
Contributor

Hi guys, I just landed here after running a

StellarSdk.Network.useTestNetwork()

Global class Network is deprecated. Please pass explicit argument instead, e.g. new Transaction(envelope, Networks.PUBLIC) (see https://git.io/fj9fG for more info).

The only reason I called that function was to make it happy, though:

Error: No network selected. Use Network.use, Network.usePublicNetwork or Network.useTestNetwork helper methods to select network.

@Akuukis Akuukis deleted the explicit-network-2 branch August 10, 2019 17:59
@Akuukis
Copy link
Contributor Author

Akuukis commented Aug 10, 2019

@MisterTicot Hi! Hmm, I'm ready to take a look at it. Can you please share the link to your code or elaborate more on the context of error? Or, even better, extract a small snippet where it fails?

@mkaliberda
Copy link

mkaliberda commented Aug 13, 2019

I use stellar-sdk.js when You apdated It, stellar-sdk does not work with old version!

@abuiles
Copy link
Contributor

abuiles commented Aug 13, 2019

@mkaliberda can you elaborate please?

@MisterTicot
Copy link
Contributor

@Akuukis This

@marcinx
Copy link

marcinx commented Sep 23, 2019

Recently updated to newer versions and now see warnings in our logs: Sep 23 18:48:18 localhost stardust: Global class 'Network' is deprecated. Please pass explicit argument instead, e.g. 'new Transaction(envelope, Networks.PUBLIC)' (see https://git.io/fj9fG for more info).

This appears to be triggered by StellarSdk.Network.usePublicNetwork(), and StellarSdk.Network.useTestNetwork().

I don't understand the motivation behind this change by the way. The error message suggests that we now need to pass the network for every single transaction issued in our application? I am a little bit confused about the warning and the suggested solution (passing the network to tx constructor every time).

Looking forward to some input on this @abuiles Thanks!

@morleyzhi
Copy link
Contributor

@marcinx The original issue, #112, lays out the reasoning behind the change. In my personal opinion, having ported some code to the new way of setting networks, the change isn't that bad and accommodates more use cases like the one in the linked issue, at the expense of a few keystrokes.

@msfeldstein
Copy link

Ideally you'd use an environment variable so you can easily change networks, including between testnet and mainnet deployments

@marcinx
Copy link

marcinx commented Sep 23, 2019

Okay. Thanks for the follow ups.

So basically we can scrap StellarSdk.Network.usePublicNetwork() and forget about the singleton.

What network is selected by default if we don't pass a passphrase to TransactonBuilder? It looks like the passphrase is being set to null if no opts.networkPassphrase is given: this.networkPassphrase = opts.networkPassphrase || null; What happens when the passphrase is null?

We currently don't work with Testnet. Do we need to specifically add Networks.PUBLIC (or env var, or some other way, etc.) to each transaction builder call or will it automatically select PUBLIC if no such opt is provided?

Edit: Looks like it will simply throw an error down the road if the network is not set in the singleton. When support for the singleton is removed in the future the transactions will simply fail if no network is explicitly set in the opts. Is that intended?

Wouldn't it be better to default the passPhrase to Networks.PUBLIC (or testnet) instead of null if it is not provided in the opts for transaction builder? I.e. this.networkPassphrase = opts.networkPassphrase || Networks.PUBLIC. ?

@morleyzhi
Copy link
Contributor

Yes, I would specifically pass Networks.PUBLIC in places that expect a network passphrase.

When you don't pass in a passphrase, it will fall back to the current Network.use*() setting:

console.warn(

Ostensibly when we remove Network.use*(), we'll throw an error wherever a network passphrase is expected but not supplied.

@msfeldstein
Copy link

we definitely don't want to default things to Networks.PUBLIC, if anything we'd default to TESTNET, but its better to have stuff like this just fail so it can be fixed properly instead of continuing to work in an undefined way

@marcinx
Copy link

marcinx commented Sep 23, 2019

Okay, thanks for the super fast replies! Figured it all out now. I'm definitely glad I subscribed to js-stellar-base so I can keep track of these changes more continuously.

ernestognw added a commit to ernestognw/docs that referenced this pull request Jan 11, 2020
StellarSdk.Network.useTestNetwork() was deprecated by Networks.TESTNET constant, so this should be updated in order to be consistent with this update:  stellar/js-stellar-base#207
grempe added a commit to grempe/js-stellar-sdk that referenced this pull request Apr 28, 2020
Per deprecation warning message, which links to here:

stellar/js-stellar-base#207
abuiles pushed a commit to stellar/js-stellar-sdk that referenced this pull request Apr 28, 2020
@bantustic
Copy link

For those using the sdk to connect with stellarSdk.Network.useTestNetwork() did not work for me and says deprecated on top of the page. I have only managed to get it to work with StellarSdk.Networks.TESTNET. best of luck.

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

Successfully merging this pull request may close these issues.

Network is a global singleton
8 participants