@codama/dynamic-address-resolution 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +155 -0
- package/dist/index.browser.cjs +1288 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +1278 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.node.cjs +1288 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +1278 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/index.react-native.mjs +1278 -0
- package/dist/index.react-native.mjs.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/resolvers/index.d.ts +9 -0
- package/dist/types/resolvers/index.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-account-address.d.ts +13 -0
- package/dist/types/resolvers/resolve-account-address.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-account-value-node-address.d.ts +15 -0
- package/dist/types/resolvers/resolve-account-value-node-address.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-conditional.d.ts +13 -0
- package/dist/types/resolvers/resolve-conditional.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-instruction-account-address.d.ts +13 -0
- package/dist/types/resolvers/resolve-instruction-account-address.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-pda-address.d.ts +13 -0
- package/dist/types/resolvers/resolve-pda-address.d.ts.map +1 -0
- package/dist/types/resolvers/resolve-standalone-pda.d.ts +8 -0
- package/dist/types/resolvers/resolve-standalone-pda.d.ts.map +1 -0
- package/dist/types/resolvers/types.d.ts +16 -0
- package/dist/types/resolvers/types.d.ts.map +1 -0
- package/dist/types/shared/address.d.ts +13 -0
- package/dist/types/shared/address.d.ts.map +1 -0
- package/dist/types/shared/bytes-encoding.d.ts +15 -0
- package/dist/types/shared/bytes-encoding.d.ts.map +1 -0
- package/dist/types/shared/codecs.d.ts +9 -0
- package/dist/types/shared/codecs.d.ts.map +1 -0
- package/dist/types/shared/nodes.d.ts +2 -0
- package/dist/types/shared/nodes.d.ts.map +1 -0
- package/dist/types/shared/types.d.ts +13 -0
- package/dist/types/shared/types.d.ts.map +1 -0
- package/dist/types/shared/util.d.ts +15 -0
- package/dist/types/shared/util.d.ts.map +1 -0
- package/dist/types/visitors/account-default-value.d.ts +16 -0
- package/dist/types/visitors/account-default-value.d.ts.map +1 -0
- package/dist/types/visitors/codec-input-transformer.d.ts +37 -0
- package/dist/types/visitors/codec-input-transformer.d.ts.map +1 -0
- package/dist/types/visitors/condition-node-value.d.ts +12 -0
- package/dist/types/visitors/condition-node-value.d.ts.map +1 -0
- package/dist/types/visitors/default-value-encoder.d.ts +16 -0
- package/dist/types/visitors/default-value-encoder.d.ts.map +1 -0
- package/dist/types/visitors/index.d.ts +7 -0
- package/dist/types/visitors/index.d.ts.map +1 -0
- package/dist/types/visitors/pda-seed-value.d.ts +20 -0
- package/dist/types/visitors/pda-seed-value.d.ts.map +1 -0
- package/dist/types/visitors/value-node-value.d.ts +14 -0
- package/dist/types/visitors/value-node-value.d.ts.map +1 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Codama
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Codama ➤ Dynamic Address Resolution
|
|
2
|
+
|
|
3
|
+
[![npm][npm-image]][npm-url]
|
|
4
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
5
|
+
|
|
6
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/dynamic-address-resolution.svg?style=flat
|
|
7
|
+
[npm-image]: https://img.shields.io/npm/v/@codama/dynamic-address-resolution.svg?style=flat&label=%40codama%2Fdynamic-address-resolution
|
|
8
|
+
[npm-url]: https://www.npmjs.com/package/@codama/dynamic-address-resolution
|
|
9
|
+
|
|
10
|
+
This package provides the address resolution functionality for instruction accounts of a Codama IDL. It powers [`@codama/dynamic-client`](../dynamic-client/README.md).
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm install @codama/dynamic-address-resolution
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
> [!NOTE]
|
|
19
|
+
> This package is **not** included in the main [`codama`](../library) package.
|
|
20
|
+
|
|
21
|
+
## Types
|
|
22
|
+
|
|
23
|
+
- `AccountsInput`, `ArgumentsInput` — user-input shapes for accounts and arguments.
|
|
24
|
+
- `ResolverFn`, `ResolversInput`, `ResolverFnInput` — user-supplied custom resolver functions.
|
|
25
|
+
- `AddressInput`, `PublicKeyLike` — accepted address-like inputs (modern `Address` strings, base58 strings, or legacy `PublicKey`-like objects with `.toBase58()`).
|
|
26
|
+
|
|
27
|
+
## Types generation
|
|
28
|
+
|
|
29
|
+
> For now, types for arguments, accounts, custom resolvers, and PDA seeds can be produced via [`@codama/dynamic-client`](../dynamic-client/README.md)'s `generate-client-types` command, and passed as generics to the functions. Types generation for this package will be added in a follow-up release.
|
|
30
|
+
|
|
31
|
+
## Functions
|
|
32
|
+
|
|
33
|
+
### `resolveInstructionAccountAddress(input)`
|
|
34
|
+
|
|
35
|
+
Resolves the on-chain `Address` for a single `InstructionAccountNode` of an instruction, applying default values, PDA derivation, conditional resolution, and any user-supplied custom resolvers. Returns `null` for optional accounts that resolve to "omitted".
|
|
36
|
+
|
|
37
|
+
**Untyped:**
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const address = await resolveInstructionAccountAddress({
|
|
41
|
+
accountsInput: { authority: ownerAddress },
|
|
42
|
+
argumentsInput: { amount: 1_000_000_000n },
|
|
43
|
+
ixAccountNode,
|
|
44
|
+
ixNode,
|
|
45
|
+
root,
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Typed:**
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import type { TransferSolAccounts, TransferSolArgs } from './generated/system-program-idl-types';
|
|
53
|
+
|
|
54
|
+
const address = await resolveInstructionAccountAddress<TransferSolAccounts, TransferSolArgs>({
|
|
55
|
+
accountsInput: { source, destination },
|
|
56
|
+
argumentsInput: { amount: 1_000_000_000n },
|
|
57
|
+
ixAccountNode,
|
|
58
|
+
ixNode,
|
|
59
|
+
root,
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Automatic resolution rules
|
|
64
|
+
|
|
65
|
+
Accounts (PDAs, program ids, constants) with a `defaultValue` are resolved automatically and may be omitted from `accountsInput`.
|
|
66
|
+
|
|
67
|
+
| Account scenario | Type in `accountsInput` | Auto resolution |
|
|
68
|
+
| --------------------------------------------------------------- | ------------------------------ | -------------------------------------------------------------------------------------------- |
|
|
69
|
+
| Required account without `defaultValue` | `{ system: Address }` | No |
|
|
70
|
+
| Required account with `defaultValue`<br>(PDA, programId, etc.) | `{ system?: Address }` | Auto-resolved to `defaultValue` if omitted |
|
|
71
|
+
| Optional account (`isOptional: true`)<br>without `defaultValue` | `{ system: Address \| null }` | Resolved via `optionalAccountStrategy`,<br>if provided as `null` |
|
|
72
|
+
| Optional account (`isOptional: true`)<br>with `defaultValue` | `{ system?: Address \| null }` | - `null` resolves via `optionalAccountStrategy`<br>- `undefined` resolves via `defaultValue` |
|
|
73
|
+
|
|
74
|
+
Auto-resolved kinds include:
|
|
75
|
+
|
|
76
|
+
- **PDA accounts** — derived from seeds defined in the IDL (`pdaValueNode`).
|
|
77
|
+
- **Program IDs** — known program addresses (e.g. System Program, Token Program).
|
|
78
|
+
- **Constants** — `constantValueNode` defaults.
|
|
79
|
+
- **Conditional values** — `conditionalValueNode` defaults.
|
|
80
|
+
- **Custom resolvers** — `resolverValueNode` defaults supplied via `resolversInput`.
|
|
81
|
+
|
|
82
|
+
### `resolveStandalonePda(root, pdaNode, seedInputs?)`
|
|
83
|
+
|
|
84
|
+
Derives a `ProgramDerivedAddress` for a `PdaNode` outside any instruction context — seeds are supplied directly as a `Record<string, unknown>`.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const pdaNode = root.program.pdas.find(p => p.name === 'metadata')!;
|
|
88
|
+
const [pda, bump] = await resolveStandalonePda(root, pdaNode, {
|
|
89
|
+
authority: ownerAddress,
|
|
90
|
+
seed: 'idl',
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### `createCodecInputTransformer(typeNode, root, options?)`
|
|
95
|
+
|
|
96
|
+
Returns a function that transforms user-supplied input into the codec-compatible shape expected by [`@codama/dynamic-codecs`](../dynamic-codecs/README.md) — e.g. `Uint8Array` → `[encoding, hex]`.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { bytesTypeNode } from 'codama';
|
|
100
|
+
|
|
101
|
+
const transform = createCodecInputTransformer(bytesTypeNode(), root, {
|
|
102
|
+
bytesEncoding: 'base16',
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
transform(new Uint8Array([72, 101, 108, 108, 111]));
|
|
106
|
+
// => ['base16', '48656c6c6f']
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `createDefaultValueEncoderVisitor(codec)`
|
|
110
|
+
|
|
111
|
+
Returns a `Visitor<ReadonlyUint8Array>` that encodes default `ValueNode`s.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { getNodeCodec } from '@codama/dynamic-codecs';
|
|
115
|
+
import { bytesValueNode, visit } from 'codama';
|
|
116
|
+
|
|
117
|
+
const codec = getNodeCodec([root, root.program, argNode]);
|
|
118
|
+
const encoder = createDefaultValueEncoderVisitor(codec);
|
|
119
|
+
const bytes = visit(bytesValueNode('base16', 'a1b2c3'), encoder);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Helpers
|
|
123
|
+
|
|
124
|
+
#### `toAddress(input)`
|
|
125
|
+
|
|
126
|
+
Normalizes any `AddressInput` (modern `Address` string, base58 string, or legacy `PublicKey`-like object with `.toBase58()`) into an `Address`.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const a1 = toAddress('11111111111111111111111111111111');
|
|
130
|
+
const a2 = toAddress(new PublicKey());
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### `isPublicKeyLike(value)`
|
|
134
|
+
|
|
135
|
+
Duck-typed guard for legacy `PublicKey` objects.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
if (isPublicKeyLike(value)) {
|
|
139
|
+
const addr = toAddress(value);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### `isAddressConvertible(value)`
|
|
144
|
+
|
|
145
|
+
Returns `true` if `value` is a string `Address` or a `PublicKeyLike` object — i.e. safe to pass to `toAddress`.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
if (isAddressConvertible(input)) {
|
|
149
|
+
return toAddress(input);
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Constants
|
|
154
|
+
|
|
155
|
+
- `OPTIONAL_NODE_KINDS` — type-node kinds treated as optional.
|