@solana/addresses 2.0.0-experimental.fd11bd1 → 2.0.0-experimental.fd64233
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/README.md +21 -21
- package/dist/index.browser.cjs +25 -41
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +25 -41
- package/dist/index.browser.js.map +1 -1
- package/dist/index.native.js +25 -41
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +25 -41
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +25 -41
- package/dist/index.node.js.map +1 -1
- package/dist/types/address.d.ts +8 -14
- package/dist/types/address.d.ts.map +1 -0
- package/dist/types/curve.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/program-derived-address.d.ts +6 -6
- package/dist/types/program-derived-address.d.ts.map +1 -0
- package/dist/types/public-key.d.ts +2 -2
- package/dist/types/public-key.d.ts.map +1 -0
- package/dist/types/vendor/noble/ed25519.d.ts.map +1 -0
- package/package.json +10 -10
- package/dist/index.development.js +0 -663
- package/dist/index.development.js.map +0 -1
- package/dist/index.production.min.js +0 -22
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ This package contains utilities for generating account addresses. It can be used
|
|
|
18
18
|
|
|
19
19
|
## Types
|
|
20
20
|
|
|
21
|
-
### `
|
|
21
|
+
### `Address`
|
|
22
22
|
|
|
23
23
|
This type represents a string that validates as a Solana address. Functions that require well-formed addresses should specify their inputs in terms of this type.
|
|
24
24
|
|
|
@@ -38,7 +38,7 @@ This type represents an integer between 0-255 used as a seed when deriving a pro
|
|
|
38
38
|
|
|
39
39
|
### `address()`
|
|
40
40
|
|
|
41
|
-
This helper combines _asserting_ that a string is an address with _coercing_ it to the `
|
|
41
|
+
This helper combines _asserting_ that a string is an address with _coercing_ it to the `Address` type. It's best used with untrusted input.
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
44
|
import { address } from '@solana/addresses';
|
|
@@ -49,15 +49,15 @@ await transfer(address(fromAddress), address(toAddress), lamports(100000n));
|
|
|
49
49
|
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.
|
|
50
50
|
|
|
51
51
|
```ts
|
|
52
|
-
import {
|
|
52
|
+
import { Address } from '@solana/addresses';
|
|
53
53
|
|
|
54
54
|
const MEMO_PROGRAM_ADDRESS =
|
|
55
|
-
'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' as
|
|
55
|
+
'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' as Address<'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'>;
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
### `assertIsAddress()`
|
|
59
59
|
|
|
60
|
-
Client applications primarily deal with addresses and public keys in the form of base58-encoded strings. Addresses returned from the RPC API conform to the type `
|
|
60
|
+
Client applications primarily deal with addresses and public keys in the form of base58-encoded strings. Addresses returned from the RPC API conform to the type `Address`. You can use a value of that type wherever a base58-encoded address is expected.
|
|
61
61
|
|
|
62
62
|
From time to time you might acquire a string, that you expect to validate as an address, from an untrusted network API or user input. To assert that such an arbitrary string is a base58-encoded address, use the `assertIsAddress` function.
|
|
63
63
|
|
|
@@ -70,9 +70,9 @@ function handleSubmit() {
|
|
|
70
70
|
const address: string = accountAddressInput.value;
|
|
71
71
|
try {
|
|
72
72
|
// If this type assertion function doesn't throw, then
|
|
73
|
-
// Typescript will upcast `address` to `
|
|
73
|
+
// Typescript will upcast `address` to `Address`.
|
|
74
74
|
assertIsAddress(address);
|
|
75
|
-
// At this point, `address` is
|
|
75
|
+
// At this point, `address` is an `Address` that can be used with the RPC.
|
|
76
76
|
const balanceInLamports = await rpc.getBalance(address).send();
|
|
77
77
|
} catch (e) {
|
|
78
78
|
// `address` turned out not to be a base58-encoded address
|
|
@@ -82,7 +82,7 @@ function handleSubmit() {
|
|
|
82
82
|
|
|
83
83
|
### `assertIsProgramDerivedAddress()`
|
|
84
84
|
|
|
85
|
-
In the event that you receive an address/bump-seed tuple from some untrusted source, you can assert that such
|
|
85
|
+
In the event that you receive an address/bump-seed tuple from some untrusted source, you can assert that such a tuple conforms to the `ProgramDerivedAddress` type using this function.
|
|
86
86
|
|
|
87
87
|
See [`assertIsAddress()`](#assertisaddress) for an example of how to use an assertion function.
|
|
88
88
|
|
|
@@ -95,16 +95,16 @@ import { createAddressWithSeed } from '@solana/addresses';
|
|
|
95
95
|
|
|
96
96
|
const derivedAddress = await createAddressWithSeed({
|
|
97
97
|
// The private key associated with this address will be able to sign for `derivedAddress`.
|
|
98
|
-
baseAddress: 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as
|
|
98
|
+
baseAddress: 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as Address,
|
|
99
99
|
// Only this program will be able to write data to this account.
|
|
100
|
-
programAddress: '445erYq578p2aERrGW9mn9KiYe3fuG6uHdcJ2LPPShGw' as
|
|
100
|
+
programAddress: '445erYq578p2aERrGW9mn9KiYe3fuG6uHdcJ2LPPShGw' as Address,
|
|
101
101
|
seed: 'data-account',
|
|
102
102
|
});
|
|
103
103
|
```
|
|
104
104
|
|
|
105
105
|
### `getAddressDecoder()`
|
|
106
106
|
|
|
107
|
-
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 `
|
|
107
|
+
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 `Address` and the offset within the byte array at which the decoder stopped reading.
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
110
|
import { getAddressDecoder } from '@solana/addresses';
|
|
@@ -114,7 +114,7 @@ const addressBytes = new Uint8Array([
|
|
|
114
114
|
179, 178, 232, 220, 89, 53, 127,
|
|
115
115
|
]);
|
|
116
116
|
const addressDecoder = getAddressDecoder();
|
|
117
|
-
const
|
|
117
|
+
const address = addressDecoder.decode(address); // B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka
|
|
118
118
|
```
|
|
119
119
|
|
|
120
120
|
### `getAddressEncoder()`
|
|
@@ -124,7 +124,7 @@ Returns an encoder that you can use to encode a base58-encoded address to a byte
|
|
|
124
124
|
```ts
|
|
125
125
|
import { getAddressEncoder } from '@solana/addresses';
|
|
126
126
|
|
|
127
|
-
const address = 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as
|
|
127
|
+
const address = 'B9Lf9z5BfNPT4d5KMeaBFx8x1G4CULZYR1jA2kmxRDka' as Address;
|
|
128
128
|
const addressEncoder = getAddressEncoder();
|
|
129
129
|
const addressBytes = addressEncoder.encode(address);
|
|
130
130
|
// Uint8Array(32) [
|
|
@@ -137,7 +137,7 @@ const addressBytes = addressEncoder.encode(address);
|
|
|
137
137
|
|
|
138
138
|
### `getAddressFromPublicKey()`
|
|
139
139
|
|
|
140
|
-
Given a public `CryptoKey`, this method will return its associated `
|
|
140
|
+
Given a public `CryptoKey`, this method will return its associated `Address`.
|
|
141
141
|
|
|
142
142
|
```ts
|
|
143
143
|
import { getAddressFromPublicKey } from '@solana/addresses';
|
|
@@ -147,35 +147,35 @@ const address = await getAddressFromPublicKey(publicKey);
|
|
|
147
147
|
|
|
148
148
|
### `getProgramDerivedAddress()`
|
|
149
149
|
|
|
150
|
-
Given a program's `
|
|
150
|
+
Given a program's `Address` and up to 16 `Seeds`, this method will return the program derived address (PDA) associated with each.
|
|
151
151
|
|
|
152
152
|
```ts
|
|
153
153
|
import { getAddressEncoder, getProgramDerivedAddress } from '@solana/addresses';
|
|
154
154
|
|
|
155
155
|
const addressEncoder = getAddressEncoder();
|
|
156
156
|
const { bumpSeed, pda } = await getProgramDerivedAddress({
|
|
157
|
-
programAddress: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL' as
|
|
157
|
+
programAddress: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL' as Address,
|
|
158
158
|
seeds: [
|
|
159
159
|
// Owner
|
|
160
|
-
addressEncoder.encode('9fYLFVoVqwH37C3dyPi6cpeobfbQ2jtLpN5HgAYDDdkm' as
|
|
160
|
+
addressEncoder.encode('9fYLFVoVqwH37C3dyPi6cpeobfbQ2jtLpN5HgAYDDdkm' as Address),
|
|
161
161
|
// Token program
|
|
162
|
-
addressEncoder.encode('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as
|
|
162
|
+
addressEncoder.encode('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address),
|
|
163
163
|
// Mint
|
|
164
|
-
addressEncoder.encode('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' as
|
|
164
|
+
addressEncoder.encode('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' as Address),
|
|
165
165
|
],
|
|
166
166
|
});
|
|
167
167
|
```
|
|
168
168
|
|
|
169
169
|
### `isAddress()`
|
|
170
170
|
|
|
171
|
-
This is a type guard that accepts a string as input. It will both return `true` if the string conforms to the `
|
|
171
|
+
This is a type guard that accepts a string as input. It will both return `true` if the string conforms to the `Address` type and will refine the type for use in your program.
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
174
|
import { isAddress } from '@solana/addresses';
|
|
175
175
|
|
|
176
176
|
if (isAddress(ownerAddress)) {
|
|
177
177
|
// At this point, `ownerAddress` has been refined to a
|
|
178
|
-
// `
|
|
178
|
+
// `Address` that can be used with the RPC.
|
|
179
179
|
const { value: lamports } = await rpc.getBalance(ownerAddress).send();
|
|
180
180
|
setBalanceLamports(lamports);
|
|
181
181
|
} else {
|
package/dist/index.browser.cjs
CHANGED
|
@@ -17,66 +17,58 @@ function getMemoizedBase58Decoder() {
|
|
|
17
17
|
memoizedBase58Decoder = codecsStrings.getBase58Decoder();
|
|
18
18
|
return memoizedBase58Decoder;
|
|
19
19
|
}
|
|
20
|
-
function isAddress(
|
|
20
|
+
function isAddress(putativeAddress) {
|
|
21
21
|
if (
|
|
22
22
|
// Lowest address (32 bytes of zeroes)
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
24
|
+
putativeAddress.length > 44
|
|
25
25
|
) {
|
|
26
26
|
return false;
|
|
27
27
|
}
|
|
28
28
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
29
|
-
const bytes = base58Encoder.encode(
|
|
29
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
30
30
|
const numBytes = bytes.byteLength;
|
|
31
31
|
if (numBytes !== 32) {
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
34
|
return true;
|
|
35
35
|
}
|
|
36
|
-
function assertIsAddress(
|
|
36
|
+
function assertIsAddress(putativeAddress) {
|
|
37
37
|
try {
|
|
38
38
|
if (
|
|
39
39
|
// Lowest address (32 bytes of zeroes)
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
41
|
+
putativeAddress.length > 44
|
|
42
42
|
) {
|
|
43
43
|
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
44
44
|
}
|
|
45
45
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
46
|
-
const bytes = base58Encoder.encode(
|
|
46
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
47
47
|
const numBytes = bytes.byteLength;
|
|
48
48
|
if (numBytes !== 32) {
|
|
49
49
|
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
50
50
|
}
|
|
51
51
|
} catch (e) {
|
|
52
|
-
throw new Error(`\`${
|
|
52
|
+
throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
|
|
53
53
|
cause: e
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
function address(
|
|
58
|
-
assertIsAddress(
|
|
59
|
-
return
|
|
57
|
+
function address(putativeAddress) {
|
|
58
|
+
assertIsAddress(putativeAddress);
|
|
59
|
+
return putativeAddress;
|
|
60
60
|
}
|
|
61
|
-
function getAddressEncoder(
|
|
61
|
+
function getAddressEncoder() {
|
|
62
62
|
return codecsCore.mapEncoder(
|
|
63
|
-
codecsStrings.getStringEncoder({
|
|
64
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
65
|
-
encoding: getMemoizedBase58Encoder(),
|
|
66
|
-
size: 32
|
|
67
|
-
}),
|
|
63
|
+
codecsStrings.getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
|
|
68
64
|
(putativeAddress) => address(putativeAddress)
|
|
69
65
|
);
|
|
70
66
|
}
|
|
71
|
-
function getAddressDecoder(
|
|
72
|
-
return codecsStrings.getStringDecoder({
|
|
73
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
74
|
-
encoding: getMemoizedBase58Decoder(),
|
|
75
|
-
size: 32
|
|
76
|
-
});
|
|
67
|
+
function getAddressDecoder() {
|
|
68
|
+
return codecsStrings.getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
|
|
77
69
|
}
|
|
78
|
-
function getAddressCodec(
|
|
79
|
-
return codecsCore.combineCodec(getAddressEncoder(
|
|
70
|
+
function getAddressCodec() {
|
|
71
|
+
return codecsCore.combineCodec(getAddressEncoder(), getAddressDecoder());
|
|
80
72
|
}
|
|
81
73
|
function getAddressComparator() {
|
|
82
74
|
return new Intl.Collator("en", {
|
|
@@ -187,7 +179,7 @@ function assertIsProgramDerivedAddress(value) {
|
|
|
187
179
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
188
180
|
if (!validFormat) {
|
|
189
181
|
throw new Error(
|
|
190
|
-
`Expected given program derived address to have the following format: [
|
|
182
|
+
`Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`
|
|
191
183
|
);
|
|
192
184
|
}
|
|
193
185
|
if (value[1] < 0 || value[1] > 255) {
|
|
@@ -223,17 +215,14 @@ var PDA_MARKER_BYTES = [
|
|
|
223
215
|
];
|
|
224
216
|
var PointOnCurveError = class extends Error {
|
|
225
217
|
};
|
|
226
|
-
async function createProgramDerivedAddress({
|
|
227
|
-
programAddress,
|
|
228
|
-
seeds
|
|
229
|
-
}) {
|
|
218
|
+
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
230
219
|
await assertions.assertDigestCapabilityIsAvailable();
|
|
231
220
|
if (seeds.length > MAX_SEEDS) {
|
|
232
221
|
throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);
|
|
233
222
|
}
|
|
234
223
|
let textEncoder;
|
|
235
224
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
236
|
-
const bytes = typeof seed === "string" ? (textEncoder
|
|
225
|
+
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
237
226
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
238
227
|
throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);
|
|
239
228
|
}
|
|
@@ -250,7 +239,7 @@ async function createProgramDerivedAddress({
|
|
|
250
239
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
251
240
|
throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
|
|
252
241
|
}
|
|
253
|
-
return base58EncodedAddressCodec.decode(addressBytes)
|
|
242
|
+
return base58EncodedAddressCodec.decode(addressBytes);
|
|
254
243
|
}
|
|
255
244
|
async function getProgramDerivedAddress({
|
|
256
245
|
programAddress,
|
|
@@ -274,11 +263,7 @@ async function getProgramDerivedAddress({
|
|
|
274
263
|
}
|
|
275
264
|
throw new Error("Unable to find a viable program address bump seed");
|
|
276
265
|
}
|
|
277
|
-
async function createAddressWithSeed({
|
|
278
|
-
baseAddress,
|
|
279
|
-
programAddress,
|
|
280
|
-
seed
|
|
281
|
-
}) {
|
|
266
|
+
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
282
267
|
const { encode, decode } = getAddressCodec();
|
|
283
268
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
284
269
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
@@ -293,7 +278,7 @@ async function createAddressWithSeed({
|
|
|
293
278
|
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
294
279
|
);
|
|
295
280
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
296
|
-
return decode(addressBytes)
|
|
281
|
+
return decode(addressBytes);
|
|
297
282
|
}
|
|
298
283
|
async function getAddressFromPublicKey(publicKey) {
|
|
299
284
|
await assertions.assertKeyExporterIsAvailable();
|
|
@@ -301,8 +286,7 @@ async function getAddressFromPublicKey(publicKey) {
|
|
|
301
286
|
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
302
287
|
}
|
|
303
288
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
304
|
-
|
|
305
|
-
return base58EncodedAddress;
|
|
289
|
+
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
306
290
|
}
|
|
307
291
|
|
|
308
292
|
exports.address = address;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/address.ts","../src/program-derived-address.ts","../src/vendor/noble/ed25519.ts","../src/curve.ts","../src/public-key.ts"],"names":["address"],"mappings":";AAAA,SAAgB,cAAgC,kBAAkB;AAClE,SAAS,kBAAkB,kBAAkB,kBAAkB,wBAAwB;AAMvF,IAAI;AACJ,IAAI;AAEJ,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEA,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEO,SAAS,UACZ,8BACyF;AAEzF;AAAA;AAAA,IAEI,6BAA6B,S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,QAAM,YAAY,OAAO,SAAS,WAAW,IAAI,YAAY,EAAE,OAAO,IAAI,IAAI;AAC9E,MAAI,UAAU,aAAa,iBAAiB;AAExC,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AAEA,QAAM,sBAAsB,OAAO,cAAc;AACjD,MACI,oBAAoB,UAAU,iBAAiB,UAC/C,oBAAoB,MAAM,CAAC,iBAAiB,MAAM,EAAE,MAAM,CAAC,MAAM,UAAU,SAAS,iBAAiB,KAAK,CAAC,GAC7G;AAEE,UAAM,IAAI,MAAM,+CAA+C;AAAA,EACnE;AAEA,QAAM,qBAAqB,MAAM,OAAO,OAAO;AAAA,IAC3C;AAAA,IACA,IAAI,WAAW,CAAC,GAAG,OAAO,WAAW,GAAG,GAAG,WAAW,GAAG,mBAAmB,CAAC;AAAA,EACjF;AACA,QAAM,eAAe,IAAI,WAAW,kBAAkB;AAEtD,SAAO,OAAO,YAAY,EAAE,CAAC;AACjC;;;AGxKA,SAAS,oCAAoC;AAI7C,eAAsB,wBAAwB,WAAqD;AAC/F,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AAEvE,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,QAAM,CAAC,oBAAoB,IAAI,kBAAkB,EAAE,OAAO,IAAI,WAAW,cAAc,CAAC;AACxF,SAAO;AACX","sourcesContent":["import { Codec, combineCodec, Decoder, Encoder, mapEncoder } from '@solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\n\nexport type Base58EncodedAddress<TAddress extends string = string> = TAddress & {\n readonly __brand: unique symbol;\n};\n\nlet memoizedBase58Encoder: Encoder<string> | undefined;\nlet memoizedBase58Decoder: Decoder<string> | undefined;\n\nfunction getMemoizedBase58Encoder(): Encoder<string> {\n if (!memoizedBase58Encoder) memoizedBase58Encoder = getBase58Encoder();\n return memoizedBase58Encoder;\n}\n\nfunction getMemoizedBase58Decoder(): Decoder<string> {\n if (!memoizedBase58Decoder) memoizedBase58Decoder = getBase58Decoder();\n return memoizedBase58Decoder;\n}\n\nexport function isAddress(\n putativeBase58EncodedAddress: string\n): putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n return false;\n }\n return true;\n}\n\nexport function assertIsAddress(\n putativeBase58EncodedAddress: string\n): asserts putativeBase58EncodedAddress is Base58EncodedAddress<typeof putativeBase58EncodedAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeBase58EncodedAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeBase58EncodedAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeBase58EncodedAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeBase58EncodedAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function address<TAddress extends string = string>(\n putativeBase58EncodedAddress: TAddress\n): Base58EncodedAddress<TAddress> {\n assertIsAddress(putativeBase58EncodedAddress);\n return putativeBase58EncodedAddress as Base58EncodedAddress<TAddress>;\n}\n\nexport function getAddressEncoder(config?: Readonly<{ description: string }>): Encoder<Base58EncodedAddress> {\n return mapEncoder(\n getStringEncoder({\n description: config?.description ?? 'Base58EncodedAddress',\n encoding: getMemoizedBase58Encoder(),\n size: 32,\n }),\n putativeAddress => address(putativeAddress)\n );\n}\n\nexport function getAddressDecoder(config?: Readonly<{ description: string }>): Decoder<Base58EncodedAddress> {\n return getStringDecoder({\n description: config?.description ?? 'Base58EncodedAddress',\n encoding: getMemoizedBase58Decoder(),\n size: 32,\n }) as Decoder<Base58EncodedAddress>;\n}\n\nexport function getAddressCodec(config?: Readonly<{ description: string }>): Codec<Base58EncodedAddress> {\n return combineCodec(getAddressEncoder(config), getAddressDecoder(config));\n}\n\nexport function getAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n","import { assertDigestCapabilityIsAvailable } from '@solana/assertions';\n\nimport { assertIsAddress, Base58EncodedAddress, getAddressCodec, isAddress } from './address';\nimport { compressedPointBytesAreOnCurve } from './curve';\n\n/**\n * An address derived from a program address and a set of seeds.\n * It includes the bump seed used to derive the address and\n * ensure the address is not on the Ed25519 curve.\n */\nexport type ProgramDerivedAddress<TAddress extends string = string> = Readonly<\n [Base58EncodedAddress<TAddress>, ProgramDerivedAddressBump]\n>;\n\n/**\n * A number between 0 and 255, inclusive.\n */\nexport type ProgramDerivedAddressBump = number & {\n readonly __brand: unique symbol;\n};\n\n/**\n * Returns true if the input value is a program derived address.\n */\nexport function isProgramDerivedAddress<TAddress extends string = string>(\n value: unknown\n): value is ProgramDerivedAddress<TAddress> {\n return (\n Array.isArray(value) &&\n value.length === 2 &&\n typeof value[0] === 'string' &&\n typeof value[1] === 'number' &&\n value[1] >= 0 &&\n value[1] <= 255 &&\n isAddress(value[0])\n );\n}\n\n/**\n * Fails if the input value is not a program derived address.\n */\nexport function assertIsProgramDerivedAddress<TAddress extends string = string>(\n value: unknown\n): asserts value is ProgramDerivedAddress<TAddress> {\n const validFormat =\n Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && typeof value[1] === 'number';\n if (!validFormat) {\n // TODO: Coded error.\n throw new Error(\n `Expected given program derived address to have the following format: [Base58EncodedAddress, ProgramDerivedAddressBump].`\n );\n }\n if (value[1] < 0 || value[1] > 255) {\n // TODO: Coded error.\n throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);\n }\n assertIsAddress(value[0]);\n}\n\ntype ProgramDerivedAddressInput = Readonly<{\n programAddress: Base58EncodedAddress;\n seeds: Seed[];\n}>;\n\ntype SeedInput = Readonly<{\n baseAddress: Base58EncodedAddress;\n programAddress: Base58EncodedAddress;\n seed: Seed;\n}>;\n\ntype Seed = string | Uint8Array;\n\nconst MAX_SEED_LENGTH = 32;\nconst MAX_SEEDS = 16;\nconst PDA_MARKER_BYTES = [\n // The string 'ProgramDerivedAddress'\n 80, 114, 111, 103, 114, 97, 109, 68, 101, 114, 105, 118, 101, 100, 65, 100, 100, 114, 101, 115, 115,\n] as const;\n\n// TODO: Coded error.\nclass PointOnCurveError extends Error {}\n\nasync function createProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<Base58EncodedAddress> {\n await assertDigestCapabilityIsAvailable();\n if (seeds.length > MAX_SEEDS) {\n // TODO: Coded error.\n throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);\n }\n let textEncoder: TextEncoder;\n const seedBytes = seeds.reduce((acc, seed, ii) => {\n const bytes = typeof seed === 'string' ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;\n if (bytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);\n }\n acc.push(...bytes);\n return acc;\n }, [] as number[]);\n const base58EncodedAddressCodec = getAddressCodec();\n const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES])\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n if (await compressedPointBytesAreOnCurve(addressBytes)) {\n // TODO: Coded error.\n throw new PointOnCurveError('Invalid seeds; point must fall off the Ed25519 curve');\n }\n return base58EncodedAddressCodec.decode(addressBytes)[0];\n}\n\nexport async function getProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<ProgramDerivedAddress> {\n let bumpSeed = 255;\n while (bumpSeed > 0) {\n try {\n const address = await createProgramDerivedAddress({\n programAddress,\n seeds: [...seeds, new Uint8Array([bumpSeed])],\n });\n return [address, bumpSeed as ProgramDerivedAddressBump];\n } catch (e) {\n if (e instanceof PointOnCurveError) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n // TODO: Coded error.\n throw new Error('Unable to find a viable program address bump seed');\n}\n\nexport async function createAddressWithSeed({\n baseAddress,\n programAddress,\n seed,\n}: SeedInput): Promise<Base58EncodedAddress> {\n const { encode, decode } = getAddressCodec();\n\n const seedBytes = typeof seed === 'string' ? new TextEncoder().encode(seed) : seed;\n if (seedBytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed exceeds the maximum length of 32 bytes`);\n }\n\n const programAddressBytes = encode(programAddress);\n if (\n programAddressBytes.length >= PDA_MARKER_BYTES.length &&\n programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])\n ) {\n // TODO: Coded error.\n throw new Error(`programAddress cannot end with the PDA marker`);\n }\n\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n\n return decode(addressBytes)[0];\n}\n","/**!\n * noble-ed25519\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2019 Paul Miller (https://paulmillr.com)\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the “Software”), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\nconst D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;\nconst P = 57896044618658097711785492504343953926634992332820282019728792003956564819949n; // 2n ** 255n - 19n; ed25519 is twisted edwards curve\nconst RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n; // √-1\n\n// mod division\nfunction mod(a: bigint): bigint {\n const r = a % P;\n return r >= 0n ? r : P + r;\n}\nfunction pow2(x: bigint, power: bigint): bigint {\n // pow2(x, 4) == x^(2^4)\n let r = x;\n while (power-- > 0n) {\n r *= r;\n r %= P;\n }\n return r;\n}\nfunction pow_2_252_3(x: bigint): bigint {\n // x^(2^252-3) unrolled util for square root\n const x2 = (x * x) % P; // x^2, bits 1\n const b2 = (x2 * x) % P; // x^3, bits 11\n const b4 = (pow2(b2, 2n) * b2) % P; // x^(2^4-1), bits 1111\n const b5 = (pow2(b4, 1n) * x) % P; // x^(2^5-1), bits 11111\n const b10 = (pow2(b5, 5n) * b5) % P; // x^(2^10)\n const b20 = (pow2(b10, 10n) * b10) % P; // x^(2^20)\n const b40 = (pow2(b20, 20n) * b20) % P; // x^(2^40)\n const b80 = (pow2(b40, 40n) * b40) % P; // x^(2^80)\n const b160 = (pow2(b80, 80n) * b80) % P; // x^(2^160)\n const b240 = (pow2(b160, 80n) * b80) % P; // x^(2^240)\n const b250 = (pow2(b240, 10n) * b10) % P; // x^(2^250)\n const pow_p_5_8 = (pow2(b250, 2n) * x) % P; // < To pow to (p+3)/8, multiply it by x.\n return pow_p_5_8;\n}\nfunction uvRatio(u: bigint, v: bigint): bigint | null {\n // for sqrt comp\n const v3 = mod(v * v * v); // v³\n const v7 = mod(v3 * v3 * v); // v⁷\n const pow = pow_2_252_3(u * v7); // (uv⁷)^(p-5)/8\n let x = mod(u * v3 * pow); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * RM1); // Second root candidate; RM1 is √-1\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * RM1); // There is no valid root, vx² = -u√-1\n if (useRoot1) x = root1;\n if (useRoot2 || noRoot) x = root2; // We return root2 anyway, for const-time\n if ((mod(x) & 1n) === 1n) x = mod(-x); // edIsNegative\n if (!useRoot1 && !useRoot2) {\n return null;\n }\n return x;\n}\n// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.3\nexport function pointIsOnCurve(y: bigint, lastByte: number): boolean {\n const y2 = mod(y * y); // y²\n const u = mod(y2 - 1n); // u=y²-1\n const v = mod(D * y2 + 1n);\n const x = uvRatio(u, v); // (uv³)(uv⁷)^(p-5)/8; square root\n if (x === null) {\n return false;\n }\n const isLastByteOdd = (lastByte & 0x80) !== 0; // x_0, last bit\n if (x === 0n && isLastByteOdd) {\n return false;\n }\n return true;\n}\n","import { pointIsOnCurve } from './vendor/noble/ed25519';\n\nfunction byteToHex(byte: number): string {\n const hexString = byte.toString(16);\n if (hexString.length === 1) {\n return `0${hexString}`;\n } else {\n return hexString;\n }\n}\n\nfunction decompressPointBytes(bytes: Uint8Array): bigint {\n const hexString = bytes.reduce((acc, byte, ii) => `${byteToHex(ii === 31 ? byte & ~0x80 : byte)}${acc}`, '');\n const integerLiteralString = `0x${hexString}`;\n return BigInt(integerLiteralString);\n}\n\nexport async function compressedPointBytesAreOnCurve(bytes: Uint8Array): Promise<boolean> {\n if (bytes.byteLength !== 32) {\n return false;\n }\n const y = decompressPointBytes(bytes);\n return pointIsOnCurve(y, bytes[31]);\n}\n","import { assertKeyExporterIsAvailable } from '@solana/assertions';\n\nimport { Base58EncodedAddress, getAddressDecoder } from './address';\n\nexport async function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Base58EncodedAddress> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n // TODO: Coded error.\n throw new Error('The `CryptoKey` must be an `Ed25519` public key');\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n const [base58EncodedAddress] = getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\n return base58EncodedAddress;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/address.ts","../src/program-derived-address.ts","../src/vendor/noble/ed25519.ts","../src/curve.ts","../src/public-key.ts"],"names":["address"],"mappings":";AAAA;AAAA,EACI;AAAA,EAMA;AAAA,OACG;AACP,SAAS,kBAAkB,kBAAkB,kBAAkB,wBAAwB;AAMvF,IAAI;AACJ,IAAI;AAEJ,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEA,SAAS,2BAA4C;AACjD,MAAI,CAAC;AAAuB,4BAAwB,iBAAiB;AACrE,SAAO;AACX;AAEO,SAAS,UAAU,iBAA6E;AAEnG;AAAA;AAAA,IAEI,gBAAgB,SAAS;AAAA,IAE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jD,MACI,oBAAoB,UAAU,iBAAiB,UAC/C,oBAAoB,MAAM,CAAC,iBAAiB,MAAM,EAAE,MAAM,CAAC,MAAM,UAAU,SAAS,iBAAiB,KAAK,CAAC,GAC7G;AAEE,UAAM,IAAI,MAAM,+CAA+C;AAAA,EACnE;AAEA,QAAM,qBAAqB,MAAM,OAAO,OAAO;AAAA,IAC3C;AAAA,IACA,IAAI,WAAW,CAAC,GAAG,OAAO,WAAW,GAAG,GAAG,WAAW,GAAG,mBAAmB,CAAC;AAAA,EACjF;AACA,QAAM,eAAe,IAAI,WAAW,kBAAkB;AAEtD,SAAO,OAAO,YAAY;AAC9B;;;AGjKA,SAAS,oCAAoC;AAI7C,eAAsB,wBAAwB,WAAwC;AAClF,QAAM,6BAA6B;AACnC,MAAI,UAAU,SAAS,YAAY,UAAU,UAAU,SAAS,WAAW;AAEvE,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACrE;AACA,QAAM,iBAAiB,MAAM,OAAO,OAAO,UAAU,OAAO,SAAS;AACrE,SAAO,kBAAkB,EAAE,OAAO,IAAI,WAAW,cAAc,CAAC;AACpE","sourcesContent":["import {\n combineCodec,\n Decoder,\n Encoder,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n mapEncoder,\n} from '@solana/codecs-core';\nimport { getBase58Decoder, getBase58Encoder, getStringDecoder, getStringEncoder } from '@solana/codecs-strings';\n\nexport type Address<TAddress extends string = string> = TAddress & {\n readonly __brand: unique symbol;\n};\n\nlet memoizedBase58Encoder: Encoder<string> | undefined;\nlet memoizedBase58Decoder: Decoder<string> | undefined;\n\nfunction getMemoizedBase58Encoder(): Encoder<string> {\n if (!memoizedBase58Encoder) memoizedBase58Encoder = getBase58Encoder();\n return memoizedBase58Encoder;\n}\n\nfunction getMemoizedBase58Decoder(): Decoder<string> {\n if (!memoizedBase58Decoder) memoizedBase58Decoder = getBase58Decoder();\n return memoizedBase58Decoder;\n}\n\nexport function isAddress(putativeAddress: string): putativeAddress is Address<typeof putativeAddress> {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeAddress.length > 44\n ) {\n return false;\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n return false;\n }\n return true;\n}\n\nexport function assertIsAddress(putativeAddress: string): asserts putativeAddress is Address<typeof putativeAddress> {\n try {\n // Fast-path; see if the input string is of an acceptable length.\n if (\n // Lowest address (32 bytes of zeroes)\n putativeAddress.length < 32 ||\n // Highest address (32 bytes of 255)\n putativeAddress.length > 44\n ) {\n throw new Error('Expected input string to decode to a byte array of length 32.');\n }\n // Slow-path; actually attempt to decode the input string.\n const base58Encoder = getMemoizedBase58Encoder();\n const bytes = base58Encoder.encode(putativeAddress);\n const numBytes = bytes.byteLength;\n if (numBytes !== 32) {\n throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);\n }\n } catch (e) {\n throw new Error(`\\`${putativeAddress}\\` is not a base-58 encoded address`, {\n cause: e,\n });\n }\n}\n\nexport function address<TAddress extends string = string>(putativeAddress: TAddress): Address<TAddress> {\n assertIsAddress(putativeAddress);\n return putativeAddress as Address<TAddress>;\n}\n\nexport function getAddressEncoder(): FixedSizeEncoder<Address, 32> {\n return mapEncoder(getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }), putativeAddress =>\n address(putativeAddress),\n );\n}\n\nexport function getAddressDecoder(): FixedSizeDecoder<Address, 32> {\n return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 }) as FixedSizeDecoder<Address, 32>;\n}\n\nexport function getAddressCodec(): FixedSizeCodec<Address, Address, 32> {\n return combineCodec(getAddressEncoder(), getAddressDecoder());\n}\n\nexport function getAddressComparator(): (x: string, y: string) => number {\n return new Intl.Collator('en', {\n caseFirst: 'lower',\n ignorePunctuation: false,\n localeMatcher: 'best fit',\n numeric: false,\n sensitivity: 'variant',\n usage: 'sort',\n }).compare;\n}\n","import { assertDigestCapabilityIsAvailable } from '@solana/assertions';\n\nimport { Address, assertIsAddress, getAddressCodec, isAddress } from './address';\nimport { compressedPointBytesAreOnCurve } from './curve';\n\n/**\n * An address derived from a program address and a set of seeds.\n * It includes the bump seed used to derive the address and\n * ensure the address is not on the Ed25519 curve.\n */\nexport type ProgramDerivedAddress<TAddress extends string = string> = Readonly<\n [Address<TAddress>, ProgramDerivedAddressBump]\n>;\n\n/**\n * A number between 0 and 255, inclusive.\n */\nexport type ProgramDerivedAddressBump = number & {\n readonly __brand: unique symbol;\n};\n\n/**\n * Returns true if the input value is a program derived address.\n */\nexport function isProgramDerivedAddress<TAddress extends string = string>(\n value: unknown,\n): value is ProgramDerivedAddress<TAddress> {\n return (\n Array.isArray(value) &&\n value.length === 2 &&\n typeof value[0] === 'string' &&\n typeof value[1] === 'number' &&\n value[1] >= 0 &&\n value[1] <= 255 &&\n isAddress(value[0])\n );\n}\n\n/**\n * Fails if the input value is not a program derived address.\n */\nexport function assertIsProgramDerivedAddress<TAddress extends string = string>(\n value: unknown,\n): asserts value is ProgramDerivedAddress<TAddress> {\n const validFormat =\n Array.isArray(value) && value.length === 2 && typeof value[0] === 'string' && typeof value[1] === 'number';\n if (!validFormat) {\n // TODO: Coded error.\n throw new Error(\n `Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`,\n );\n }\n if (value[1] < 0 || value[1] > 255) {\n // TODO: Coded error.\n throw new Error(`Expected program derived address bump to be in the range [0, 255], got: ${value[1]}.`);\n }\n assertIsAddress(value[0]);\n}\n\ntype ProgramDerivedAddressInput = Readonly<{\n programAddress: Address;\n seeds: Seed[];\n}>;\n\ntype SeedInput = Readonly<{\n baseAddress: Address;\n programAddress: Address;\n seed: Seed;\n}>;\n\ntype Seed = string | Uint8Array;\n\nconst MAX_SEED_LENGTH = 32;\nconst MAX_SEEDS = 16;\nconst PDA_MARKER_BYTES = [\n // The string 'ProgramDerivedAddress'\n 80, 114, 111, 103, 114, 97, 109, 68, 101, 114, 105, 118, 101, 100, 65, 100, 100, 114, 101, 115, 115,\n] as const;\n\n// TODO: Coded error.\nclass PointOnCurveError extends Error {}\n\nasync function createProgramDerivedAddress({ programAddress, seeds }: ProgramDerivedAddressInput): Promise<Address> {\n await assertDigestCapabilityIsAvailable();\n if (seeds.length > MAX_SEEDS) {\n // TODO: Coded error.\n throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);\n }\n let textEncoder: TextEncoder;\n const seedBytes = seeds.reduce((acc, seed, ii) => {\n const bytes = typeof seed === 'string' ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;\n if (bytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);\n }\n acc.push(...bytes);\n return acc;\n }, [] as number[]);\n const base58EncodedAddressCodec = getAddressCodec();\n const programAddressBytes = base58EncodedAddressCodec.encode(programAddress);\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...seedBytes, ...programAddressBytes, ...PDA_MARKER_BYTES]),\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n if (await compressedPointBytesAreOnCurve(addressBytes)) {\n // TODO: Coded error.\n throw new PointOnCurveError('Invalid seeds; point must fall off the Ed25519 curve');\n }\n return base58EncodedAddressCodec.decode(addressBytes);\n}\n\nexport async function getProgramDerivedAddress({\n programAddress,\n seeds,\n}: ProgramDerivedAddressInput): Promise<ProgramDerivedAddress> {\n let bumpSeed = 255;\n while (bumpSeed > 0) {\n try {\n const address = await createProgramDerivedAddress({\n programAddress,\n seeds: [...seeds, new Uint8Array([bumpSeed])],\n });\n return [address, bumpSeed as ProgramDerivedAddressBump];\n } catch (e) {\n if (e instanceof PointOnCurveError) {\n bumpSeed--;\n } else {\n throw e;\n }\n }\n }\n // TODO: Coded error.\n throw new Error('Unable to find a viable program address bump seed');\n}\n\nexport async function createAddressWithSeed({ baseAddress, programAddress, seed }: SeedInput): Promise<Address> {\n const { encode, decode } = getAddressCodec();\n\n const seedBytes = typeof seed === 'string' ? new TextEncoder().encode(seed) : seed;\n if (seedBytes.byteLength > MAX_SEED_LENGTH) {\n // TODO: Coded error.\n throw new Error(`The seed exceeds the maximum length of 32 bytes`);\n }\n\n const programAddressBytes = encode(programAddress);\n if (\n programAddressBytes.length >= PDA_MARKER_BYTES.length &&\n programAddressBytes.slice(-PDA_MARKER_BYTES.length).every((byte, index) => byte === PDA_MARKER_BYTES[index])\n ) {\n // TODO: Coded error.\n throw new Error(`programAddress cannot end with the PDA marker`);\n }\n\n const addressBytesBuffer = await crypto.subtle.digest(\n 'SHA-256',\n new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes]),\n );\n const addressBytes = new Uint8Array(addressBytesBuffer);\n\n return decode(addressBytes);\n}\n","/**!\n * noble-ed25519\n *\n * The MIT License (MIT)\n *\n * Copyright (c) 2019 Paul Miller (https://paulmillr.com)\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the “Software”), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\nconst D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;\nconst P = 57896044618658097711785492504343953926634992332820282019728792003956564819949n; // 2n ** 255n - 19n; ed25519 is twisted edwards curve\nconst RM1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n; // √-1\n\n// mod division\nfunction mod(a: bigint): bigint {\n const r = a % P;\n return r >= 0n ? r : P + r;\n}\nfunction pow2(x: bigint, power: bigint): bigint {\n // pow2(x, 4) == x^(2^4)\n let r = x;\n while (power-- > 0n) {\n r *= r;\n r %= P;\n }\n return r;\n}\nfunction pow_2_252_3(x: bigint): bigint {\n // x^(2^252-3) unrolled util for square root\n const x2 = (x * x) % P; // x^2, bits 1\n const b2 = (x2 * x) % P; // x^3, bits 11\n const b4 = (pow2(b2, 2n) * b2) % P; // x^(2^4-1), bits 1111\n const b5 = (pow2(b4, 1n) * x) % P; // x^(2^5-1), bits 11111\n const b10 = (pow2(b5, 5n) * b5) % P; // x^(2^10)\n const b20 = (pow2(b10, 10n) * b10) % P; // x^(2^20)\n const b40 = (pow2(b20, 20n) * b20) % P; // x^(2^40)\n const b80 = (pow2(b40, 40n) * b40) % P; // x^(2^80)\n const b160 = (pow2(b80, 80n) * b80) % P; // x^(2^160)\n const b240 = (pow2(b160, 80n) * b80) % P; // x^(2^240)\n const b250 = (pow2(b240, 10n) * b10) % P; // x^(2^250)\n const pow_p_5_8 = (pow2(b250, 2n) * x) % P; // < To pow to (p+3)/8, multiply it by x.\n return pow_p_5_8;\n}\nfunction uvRatio(u: bigint, v: bigint): bigint | null {\n // for sqrt comp\n const v3 = mod(v * v * v); // v³\n const v7 = mod(v3 * v3 * v); // v⁷\n const pow = pow_2_252_3(u * v7); // (uv⁷)^(p-5)/8\n let x = mod(u * v3 * pow); // (uv³)(uv⁷)^(p-5)/8\n const vx2 = mod(v * x * x); // vx²\n const root1 = x; // First root candidate\n const root2 = mod(x * RM1); // Second root candidate; RM1 is √-1\n const useRoot1 = vx2 === u; // If vx² = u (mod p), x is a square root\n const useRoot2 = vx2 === mod(-u); // If vx² = -u, set x <-- x * 2^((p-1)/4)\n const noRoot = vx2 === mod(-u * RM1); // There is no valid root, vx² = -u√-1\n if (useRoot1) x = root1;\n if (useRoot2 || noRoot) x = root2; // We return root2 anyway, for const-time\n if ((mod(x) & 1n) === 1n) x = mod(-x); // edIsNegative\n if (!useRoot1 && !useRoot2) {\n return null;\n }\n return x;\n}\n// https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.3\nexport function pointIsOnCurve(y: bigint, lastByte: number): boolean {\n const y2 = mod(y * y); // y²\n const u = mod(y2 - 1n); // u=y²-1\n const v = mod(D * y2 + 1n);\n const x = uvRatio(u, v); // (uv³)(uv⁷)^(p-5)/8; square root\n if (x === null) {\n return false;\n }\n const isLastByteOdd = (lastByte & 0x80) !== 0; // x_0, last bit\n if (x === 0n && isLastByteOdd) {\n return false;\n }\n return true;\n}\n","import { pointIsOnCurve } from './vendor/noble/ed25519';\n\nfunction byteToHex(byte: number): string {\n const hexString = byte.toString(16);\n if (hexString.length === 1) {\n return `0${hexString}`;\n } else {\n return hexString;\n }\n}\n\nfunction decompressPointBytes(bytes: Uint8Array): bigint {\n const hexString = bytes.reduce((acc, byte, ii) => `${byteToHex(ii === 31 ? byte & ~0x80 : byte)}${acc}`, '');\n const integerLiteralString = `0x${hexString}`;\n return BigInt(integerLiteralString);\n}\n\nexport async function compressedPointBytesAreOnCurve(bytes: Uint8Array): Promise<boolean> {\n if (bytes.byteLength !== 32) {\n return false;\n }\n const y = decompressPointBytes(bytes);\n return pointIsOnCurve(y, bytes[31]);\n}\n","import { assertKeyExporterIsAvailable } from '@solana/assertions';\n\nimport { Address, getAddressDecoder } from './address';\n\nexport async function getAddressFromPublicKey(publicKey: CryptoKey): Promise<Address> {\n await assertKeyExporterIsAvailable();\n if (publicKey.type !== 'public' || publicKey.algorithm.name !== 'Ed25519') {\n // TODO: Coded error.\n throw new Error('The `CryptoKey` must be an `Ed25519` public key');\n }\n const publicKeyBytes = await crypto.subtle.exportKey('raw', publicKey);\n return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));\n}\n"]}
|
package/dist/index.browser.js
CHANGED
|
@@ -15,66 +15,58 @@ function getMemoizedBase58Decoder() {
|
|
|
15
15
|
memoizedBase58Decoder = getBase58Decoder();
|
|
16
16
|
return memoizedBase58Decoder;
|
|
17
17
|
}
|
|
18
|
-
function isAddress(
|
|
18
|
+
function isAddress(putativeAddress) {
|
|
19
19
|
if (
|
|
20
20
|
// Lowest address (32 bytes of zeroes)
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
22
|
+
putativeAddress.length > 44
|
|
23
23
|
) {
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
26
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
27
|
-
const bytes = base58Encoder.encode(
|
|
27
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
28
28
|
const numBytes = bytes.byteLength;
|
|
29
29
|
if (numBytes !== 32) {
|
|
30
30
|
return false;
|
|
31
31
|
}
|
|
32
32
|
return true;
|
|
33
33
|
}
|
|
34
|
-
function assertIsAddress(
|
|
34
|
+
function assertIsAddress(putativeAddress) {
|
|
35
35
|
try {
|
|
36
36
|
if (
|
|
37
37
|
// Lowest address (32 bytes of zeroes)
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
putativeAddress.length < 32 || // Highest address (32 bytes of 255)
|
|
39
|
+
putativeAddress.length > 44
|
|
40
40
|
) {
|
|
41
41
|
throw new Error("Expected input string to decode to a byte array of length 32.");
|
|
42
42
|
}
|
|
43
43
|
const base58Encoder = getMemoizedBase58Encoder();
|
|
44
|
-
const bytes = base58Encoder.encode(
|
|
44
|
+
const bytes = base58Encoder.encode(putativeAddress);
|
|
45
45
|
const numBytes = bytes.byteLength;
|
|
46
46
|
if (numBytes !== 32) {
|
|
47
47
|
throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
|
|
48
48
|
}
|
|
49
49
|
} catch (e) {
|
|
50
|
-
throw new Error(`\`${
|
|
50
|
+
throw new Error(`\`${putativeAddress}\` is not a base-58 encoded address`, {
|
|
51
51
|
cause: e
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
function address(
|
|
56
|
-
assertIsAddress(
|
|
57
|
-
return
|
|
55
|
+
function address(putativeAddress) {
|
|
56
|
+
assertIsAddress(putativeAddress);
|
|
57
|
+
return putativeAddress;
|
|
58
58
|
}
|
|
59
|
-
function getAddressEncoder(
|
|
59
|
+
function getAddressEncoder() {
|
|
60
60
|
return mapEncoder(
|
|
61
|
-
getStringEncoder({
|
|
62
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
63
|
-
encoding: getMemoizedBase58Encoder(),
|
|
64
|
-
size: 32
|
|
65
|
-
}),
|
|
61
|
+
getStringEncoder({ encoding: getMemoizedBase58Encoder(), size: 32 }),
|
|
66
62
|
(putativeAddress) => address(putativeAddress)
|
|
67
63
|
);
|
|
68
64
|
}
|
|
69
|
-
function getAddressDecoder(
|
|
70
|
-
return getStringDecoder({
|
|
71
|
-
description: config?.description ?? "Base58EncodedAddress",
|
|
72
|
-
encoding: getMemoizedBase58Decoder(),
|
|
73
|
-
size: 32
|
|
74
|
-
});
|
|
65
|
+
function getAddressDecoder() {
|
|
66
|
+
return getStringDecoder({ encoding: getMemoizedBase58Decoder(), size: 32 });
|
|
75
67
|
}
|
|
76
|
-
function getAddressCodec(
|
|
77
|
-
return combineCodec(getAddressEncoder(
|
|
68
|
+
function getAddressCodec() {
|
|
69
|
+
return combineCodec(getAddressEncoder(), getAddressDecoder());
|
|
78
70
|
}
|
|
79
71
|
function getAddressComparator() {
|
|
80
72
|
return new Intl.Collator("en", {
|
|
@@ -185,7 +177,7 @@ function assertIsProgramDerivedAddress(value) {
|
|
|
185
177
|
const validFormat = Array.isArray(value) && value.length === 2 && typeof value[0] === "string" && typeof value[1] === "number";
|
|
186
178
|
if (!validFormat) {
|
|
187
179
|
throw new Error(
|
|
188
|
-
`Expected given program derived address to have the following format: [
|
|
180
|
+
`Expected given program derived address to have the following format: [Address, ProgramDerivedAddressBump].`
|
|
189
181
|
);
|
|
190
182
|
}
|
|
191
183
|
if (value[1] < 0 || value[1] > 255) {
|
|
@@ -221,17 +213,14 @@ var PDA_MARKER_BYTES = [
|
|
|
221
213
|
];
|
|
222
214
|
var PointOnCurveError = class extends Error {
|
|
223
215
|
};
|
|
224
|
-
async function createProgramDerivedAddress({
|
|
225
|
-
programAddress,
|
|
226
|
-
seeds
|
|
227
|
-
}) {
|
|
216
|
+
async function createProgramDerivedAddress({ programAddress, seeds }) {
|
|
228
217
|
await assertDigestCapabilityIsAvailable();
|
|
229
218
|
if (seeds.length > MAX_SEEDS) {
|
|
230
219
|
throw new Error(`A maximum of ${MAX_SEEDS} seeds may be supplied when creating an address`);
|
|
231
220
|
}
|
|
232
221
|
let textEncoder;
|
|
233
222
|
const seedBytes = seeds.reduce((acc, seed, ii) => {
|
|
234
|
-
const bytes = typeof seed === "string" ? (textEncoder
|
|
223
|
+
const bytes = typeof seed === "string" ? (textEncoder ||= new TextEncoder()).encode(seed) : seed;
|
|
235
224
|
if (bytes.byteLength > MAX_SEED_LENGTH) {
|
|
236
225
|
throw new Error(`The seed at index ${ii} exceeds the maximum length of 32 bytes`);
|
|
237
226
|
}
|
|
@@ -248,7 +237,7 @@ async function createProgramDerivedAddress({
|
|
|
248
237
|
if (await compressedPointBytesAreOnCurve(addressBytes)) {
|
|
249
238
|
throw new PointOnCurveError("Invalid seeds; point must fall off the Ed25519 curve");
|
|
250
239
|
}
|
|
251
|
-
return base58EncodedAddressCodec.decode(addressBytes)
|
|
240
|
+
return base58EncodedAddressCodec.decode(addressBytes);
|
|
252
241
|
}
|
|
253
242
|
async function getProgramDerivedAddress({
|
|
254
243
|
programAddress,
|
|
@@ -272,11 +261,7 @@ async function getProgramDerivedAddress({
|
|
|
272
261
|
}
|
|
273
262
|
throw new Error("Unable to find a viable program address bump seed");
|
|
274
263
|
}
|
|
275
|
-
async function createAddressWithSeed({
|
|
276
|
-
baseAddress,
|
|
277
|
-
programAddress,
|
|
278
|
-
seed
|
|
279
|
-
}) {
|
|
264
|
+
async function createAddressWithSeed({ baseAddress, programAddress, seed }) {
|
|
280
265
|
const { encode, decode } = getAddressCodec();
|
|
281
266
|
const seedBytes = typeof seed === "string" ? new TextEncoder().encode(seed) : seed;
|
|
282
267
|
if (seedBytes.byteLength > MAX_SEED_LENGTH) {
|
|
@@ -291,7 +276,7 @@ async function createAddressWithSeed({
|
|
|
291
276
|
new Uint8Array([...encode(baseAddress), ...seedBytes, ...programAddressBytes])
|
|
292
277
|
);
|
|
293
278
|
const addressBytes = new Uint8Array(addressBytesBuffer);
|
|
294
|
-
return decode(addressBytes)
|
|
279
|
+
return decode(addressBytes);
|
|
295
280
|
}
|
|
296
281
|
async function getAddressFromPublicKey(publicKey) {
|
|
297
282
|
await assertKeyExporterIsAvailable();
|
|
@@ -299,8 +284,7 @@ async function getAddressFromPublicKey(publicKey) {
|
|
|
299
284
|
throw new Error("The `CryptoKey` must be an `Ed25519` public key");
|
|
300
285
|
}
|
|
301
286
|
const publicKeyBytes = await crypto.subtle.exportKey("raw", publicKey);
|
|
302
|
-
|
|
303
|
-
return base58EncodedAddress;
|
|
287
|
+
return getAddressDecoder().decode(new Uint8Array(publicKeyBytes));
|
|
304
288
|
}
|
|
305
289
|
|
|
306
290
|
export { address, assertIsAddress, assertIsProgramDerivedAddress, createAddressWithSeed, getAddressCodec, getAddressComparator, getAddressDecoder, getAddressEncoder, getAddressFromPublicKey, getProgramDerivedAddress, isAddress, isProgramDerivedAddress };
|