-
Notifications
You must be signed in to change notification settings - Fork 923
refactor(experimental): create a sham for PublicKey
#1861
refactor(experimental): create a sham for PublicKey
#1861
Conversation
Current dependencies on/for this PR:
This stack of pull requests is managed by Graphite. |
aa45e91
to
e3abdd2
Compare
8036a03
to
f9982dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it! ❤️
|
||
This package is a drop-in replacement for a _subset_ of the version `1.x` `@solana/web3.js` interfaces. Its goal is to satisfy the legacy interfaces with as little code as possible. | ||
|
||
If you depend on on web3.js _directly_ then you should not use this. Instead, migrate to version `>=2` of `@solana/web3.js`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you depend on on web3.js _directly_ then you should not use this. Instead, migrate to version `>=2` of `@solana/web3.js`. | |
If you depend on web3.js _directly_ then you should not use this. Instead, migrate to version `>=2` of `@solana/web3.js`. |
'findProgramAddress', | ||
'findProgramAddressSync', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect people are likely gonna complain about these not being shamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙅🏼 shame!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People won't use the sham. Libraries will use the sham (eg. @metaplex/mpl-token-metadata
). So long as we support everything the libraries need, we're done.
it('returns `true` given two public keys with the same address', () => { | ||
expect(publicKey.equals(publicKey)).toBe(true); | ||
}); | ||
it('returns `false` given two public keys with the same address', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('returns `false` given two public keys with the same address', () => { | |
it('returns `false` given two public keys with different addresses', () => { |
}); | ||
describe('the `toJSON()` method', () => { | ||
it('returns the `Address` related to the public key as a string', () => { | ||
expect(publicKey.toJSON()).toBe(VALID_ADDRESS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be wrong here but shouldn't toJson()
return a JSON string instead of just a string? I.e. "I'm valid JSON"
instead of I'm not
.
expect(publicKey.toJSON()).toBe(VALID_ADDRESS); | |
expect(publicKey.toJSON()).toBe(`"${VALID_ADDRESS}"`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This confused me too in something else where I was using JSON.stringify(..)
but it seems like JavaScript ignores the quotes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, toJSON
doesn't return you a JSON string? Just the JSON-compatible JavaScript object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about it the less sure I am, but I'm imagining it's a JSON string until you pass it into a JavaScript function, then they are clipped? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just matching the behaviour of the existing library without trying to use my brain. I might hurt myself otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a neat approach! Are there any particular libraries you're using to figure out what is/isn't required in the sham?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we really naming it legacy-sham
? 🤭
Also PSA to all README'ers: Don't use instanceof
or you're toast.
describe('the `toBuffer()` method', () => { | ||
if (__NODEJS__) { | ||
it('returns a buffer representing the public key bytes', () => { | ||
expect(publicKey.toBuffer()).toEqual(Buffer.from(VALID_ADDRESS_BYTES)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💙
f9982dd
to
516772f
Compare
Merge activity
|
Yeah; starting with |
Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up. |
Preamble
Imagine that you depend on a module that itself depends on the legacy
@solana/web3.js
. This makes it difficult for you to remove the legacy library from your project.The chances are, though, that module only needs a fraction of what web3.js provides.
The goal of the sham – of which this PR is the first – is to provide drop-in replacements for just the things that a set list of known dependencies require. You should be able to hot-swap the sham in via npm
overrides
or yarnresolutions
and thereby drop the legacy web3.js from your bundle.Summary
This PR introduces an interface compatible(-ish)
PublicKey
that you can use as a drop-in replacement for the one in@solana/web3.js@1
Test Plan
See later diffs for a functional test plan where we actually drop the sham in to an actual application and see the results.
Addresses #1825.