@solana/addresses
Advanced tools
Comparing version 2.0.0-experimental.0ec7c58 to 2.0.0-experimental.10bfcd8
@@ -307,1 +307,3 @@ import { mapEncoder, combineCodec } from '@solana/codecs-core'; | ||
export { address, assertIsAddress, assertIsProgramDerivedAddress, createAddressWithSeed, getAddressCodec, getAddressComparator, getAddressDecoder, getAddressEncoder, getAddressFromPublicKey, getProgramDerivedAddress, isAddress, isProgramDerivedAddress }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.native.js.map |
{ | ||
"name": "@solana/addresses", | ||
"version": "2.0.0-experimental.0ec7c58", | ||
"version": "2.0.0-experimental.10bfcd8", | ||
"description": "Helpers for generating account addresses", | ||
@@ -52,10 +52,10 @@ "exports": { | ||
"dependencies": { | ||
"@solana/assertions": "2.0.0-experimental.0ec7c58", | ||
"@solana/codecs-core": "2.0.0-experimental.0ec7c58", | ||
"@solana/codecs-strings": "2.0.0-experimental.0ec7c58" | ||
"@solana/assertions": "2.0.0-experimental.10bfcd8", | ||
"@solana/codecs-core": "2.0.0-experimental.10bfcd8", | ||
"@solana/codecs-strings": "2.0.0-experimental.10bfcd8" | ||
}, | ||
"devDependencies": { | ||
"@solana/eslint-config-solana": "^1.0.2", | ||
"@swc/jest": "^0.2.28", | ||
"@types/jest": "^29.5.5", | ||
"@swc/jest": "^0.2.29", | ||
"@types/jest": "^29.5.6", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
@@ -65,7 +65,7 @@ "@typescript-eslint/parser": "^6.3.0", | ||
"eslint": "^8.45.0", | ||
"eslint-plugin-jest": "^27.2.3", | ||
"eslint-plugin-jest": "^27.4.2", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.6.4", | ||
"jest-runner-eslint": "^2.1.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"jest-runner-eslint": "^2.1.2", | ||
"jest-runner-prettier": "^1.0.0", | ||
@@ -72,0 +72,0 @@ "prettier": "^2.8", |
119
README.md
@@ -25,6 +25,35 @@ [![npm][npm-image]][npm-url] | ||
Whenever you need to validate an arbitrary string as a base58-encoded address, use the `assertIsAddress()` function in this package. | ||
Whenever you need to validate an arbitrary string as a base58-encoded address, use the `address()`, `assertIsAddress()`, or `isAddress()` functions in this package. | ||
### `ProgramDerivedAddress` | ||
This type represents the tuple of a program derived address and the bump seed used to ensure that the address, as derived, is not found on the Ed25519 curve. | ||
Whenever you need to validate an arbitrary tuple as one that represents a program derived address, use the `assertIsProgramDerivedAddress()` or `isProgramDerivedAddress()` functions in this package. | ||
### `ProgramDerivedAddressBump` | ||
This type represents an integer between 0-255 used as a seed when deriving a program derived address. The purpose of this value is to modify the derivation enough to ensure that the derived address does not find itself on the Ed25519 curve. | ||
## Functions | ||
### `address()` | ||
This helper combines _asserting_ that a string is an address with _coercing_ it to the `Base58EncodedAddress` type. It's best used with untrusted input. | ||
```ts | ||
import { address } from '@solana/addresses'; | ||
await transfer(address(fromAddress), address(toAddress), lamports(100000n)); | ||
``` | ||
When starting from a known-good address as a string, it's more efficient to typecast it rather than to use the `address()` helper, because the helper unconditionally performs validation on its input. | ||
```ts | ||
import { Base58EncodedAddress } from '@solana/addresses'; | ||
const MEMO_PROGRAM_ADDRESS = | ||
'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' as Base58EncodedAddress<'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'>; | ||
``` | ||
### `assertIsAddress()` | ||
@@ -55,2 +84,57 @@ | ||
### `assertIsProgramDerivedAddress()` | ||
In the event that you receive an address/bump-seed tuple from some untrusted source, you can assert that such an tuple conforms to the `ProgramDerivedAddress` type using this function. | ||
See [`assertIsAddress()`](#assertisaddress) for an example of how to use an assertion function. | ||
### `createAddressWithSeed()` | ||
Returns a base58-encoded address derived from some base address, some program address, and a seed string or byte array. | ||
```ts | ||
import { createAddressWithSeed } from '@solana/addresses'; | ||
const derivedAddress = await createAddressWithSeed({ | ||
// The private key associated with this address will be able to sign for `derivedAddress`. | ||
baseAddress: 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as Base58EncodedAddress, | ||
// Only this program will be able to write data to this account. | ||
programAddress: '445erYq578p2aERrGW9mn9KiYe3fuG6uHdcJ2LPPShGw' as Base58EncodedAddress, | ||
seed: 'data-account', | ||
}); | ||
``` | ||
### `getAddressDecoder()` | ||
Returns a decoder that you can use to convert an array of 32 bytes representing an address to the base58-encoded representation of that address. Returns a tuple of the `Base58EncodedAddress` and the offset within the byte array at which the decoder stopped reading. | ||
```ts | ||
import { getAddressDecoder } from '@solana/addresses'; | ||
const addressBytes = new Uint8Array([ | ||
150, 183, 190, 48, 171, 8, 39, 156, 122, 213, 172, 108, 193, 95, 26, 158, 149, 243, 115, 254, 20, 200, 36, 30, 248, | ||
179, 178, 232, 220, 89, 53, 127, | ||
]); | ||
const addressDecoder = getAddressDecoder(); | ||
const [address, offset] = addressDecoder.decode(address); // [B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka, 32] | ||
``` | ||
### `getAddressEncoder()` | ||
Returns an encoder that you can use to encode a base58-encoded address to a byte array. | ||
```ts | ||
import { getAddressEncoder } from '@solana/addresses'; | ||
const address = 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as Base58EncodedAddress; | ||
const addressEncoder = getAddressEncoder(); | ||
const addressBytes = addressEncoder.encode(address); | ||
// Uint8Array(32) [ | ||
// 150, 183, 190, 48, 171, 8, 39, 156, | ||
// 122, 213, 172, 108, 193, 95, 26, 158, | ||
// 149, 243, 115, 254, 20, 200, 36, 30, | ||
// 248, 179, 178, 232, 220, 89, 53, 127 | ||
// ] | ||
``` | ||
### `getAddressFromPublicKey()` | ||
@@ -71,5 +155,5 @@ | ||
```ts | ||
import { getAddressCodec, getProgramDerivedAddress } from '@solana/addresses'; | ||
import { getAddressEncoder, getProgramDerivedAddress } from '@solana/addresses'; | ||
const { serialize } = getAddressCodec(); | ||
const addressEncoder = getAddressEncoder(); | ||
const { bumpSeed, pda } = await getProgramDerivedAddress({ | ||
@@ -79,9 +163,32 @@ programAddress: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL' as Base58EncodedAddress, | ||
// Owner | ||
serialize('9fYLFVoVqwH37C3dyPi6cpeobfbQ2jtLpN5HgAYDDdkm' as Base58EncodedAddress), | ||
addressEncoder.encode('9fYLFVoVqwH37C3dyPi6cpeobfbQ2jtLpN5HgAYDDdkm' as Base58EncodedAddress), | ||
// Token program | ||
serialize('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Base58EncodedAddress), | ||
addressEncoder.encode('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Base58EncodedAddress), | ||
// Mint | ||
serialize('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' as Base58EncodedAddress), | ||
addressEncoder.encode('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' as Base58EncodedAddress), | ||
], | ||
}); | ||
``` | ||
### `isAddress()` | ||
This is a type guard that accepts a string as input. It will both return `true` if the string conforms to the `Base58EncodedAddress` type and will refine the type for use in your program. | ||
```ts | ||
import { isAddress } from '@solana/addresses'; | ||
if (isAddress(ownerAddress)) { | ||
// At this point, `ownerAddress` has been refined to a | ||
// `Base58EncodedAddress` that can be used with the RPC. | ||
const { value: lamports } = await rpc.getBalance(ownerAddress).send(); | ||
setBalanceLamports(lamports); | ||
} else { | ||
setError(`${ownerAddress} is not an address`); | ||
} | ||
``` | ||
### `isProgramDerivedAddress()` | ||
This is a type guard that accepts a tuple as input. It will both return `true` if the tuple conforms to the `ProgramDerivedAddress` type and will refine the type for use in your program. | ||
See [`isAddress()`](#isaddress) for an example of how to use a type guard. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
283438
2613
191
23
+ Added@solana/assertions@2.0.0-experimental.10bfcd8(transitive)
+ Added@solana/codecs-core@2.0.0-experimental.10bfcd8(transitive)
+ Added@solana/codecs-numbers@2.0.0-experimental.10bfcd8(transitive)
+ Added@solana/codecs-strings@2.0.0-experimental.10bfcd8(transitive)
- Removed@solana/assertions@2.0.0-experimental.0ec7c58(transitive)
- Removed@solana/codecs-core@2.0.0-experimental.0ec7c58(transitive)
- Removed@solana/codecs-numbers@2.0.0-experimental.0ec7c58(transitive)
- Removed@solana/codecs-strings@2.0.0-experimental.0ec7c58(transitive)