@solana/web3.js 1.53.0 → 1.54.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.
@@ -0,0 +1,108 @@
1
+ import nacl from 'tweetnacl';
2
+ import * as BufferLayout from '@solana/buffer-layout';
3
+
4
+ import {Signer} from '../keypair';
5
+ import assert from '../utils/assert';
6
+ import {VersionedMessage} from '../message/versioned';
7
+ import {SIGNATURE_LENGTH_IN_BYTES} from './constants';
8
+ import * as shortvec from '../utils/shortvec-encoding';
9
+ import * as Layout from '../layout';
10
+
11
+ export type TransactionVersion = 'legacy' | 0;
12
+
13
+ /**
14
+ * Versioned transaction class
15
+ */
16
+ export class VersionedTransaction {
17
+ signatures: Array<Uint8Array>;
18
+ message: VersionedMessage;
19
+
20
+ constructor(message: VersionedMessage, signatures?: Array<Uint8Array>) {
21
+ if (signatures !== undefined) {
22
+ assert(
23
+ signatures.length === message.header.numRequiredSignatures,
24
+ 'Expected signatures length to be equal to the number of required signatures',
25
+ );
26
+ this.signatures = signatures;
27
+ } else {
28
+ const defaultSignatures = [];
29
+ for (let i = 0; i < message.header.numRequiredSignatures; i++) {
30
+ defaultSignatures.push(new Uint8Array(SIGNATURE_LENGTH_IN_BYTES));
31
+ }
32
+ this.signatures = defaultSignatures;
33
+ }
34
+ this.message = message;
35
+ }
36
+
37
+ serialize(): Uint8Array {
38
+ const serializedMessage = this.message.serialize();
39
+
40
+ const encodedSignaturesLength = Array<number>();
41
+ shortvec.encodeLength(encodedSignaturesLength, this.signatures.length);
42
+
43
+ const transactionLayout = BufferLayout.struct<{
44
+ encodedSignaturesLength: Uint8Array;
45
+ signatures: Array<Uint8Array>;
46
+ serializedMessage: Uint8Array;
47
+ }>([
48
+ BufferLayout.blob(
49
+ encodedSignaturesLength.length,
50
+ 'encodedSignaturesLength',
51
+ ),
52
+ BufferLayout.seq(
53
+ Layout.signature(),
54
+ this.signatures.length,
55
+ 'signatures',
56
+ ),
57
+ BufferLayout.blob(serializedMessage.length, 'serializedMessage'),
58
+ ]);
59
+
60
+ const serializedTransaction = new Uint8Array(2048);
61
+ const serializedTransactionLength = transactionLayout.encode(
62
+ {
63
+ encodedSignaturesLength: new Uint8Array(encodedSignaturesLength),
64
+ signatures: this.signatures,
65
+ serializedMessage,
66
+ },
67
+ serializedTransaction,
68
+ );
69
+
70
+ return serializedTransaction.slice(0, serializedTransactionLength);
71
+ }
72
+
73
+ static deserialize(serializedTransaction: Uint8Array): VersionedTransaction {
74
+ let byteArray = [...serializedTransaction];
75
+
76
+ const signatures = [];
77
+ const signaturesLength = shortvec.decodeLength(byteArray);
78
+ for (let i = 0; i < signaturesLength; i++) {
79
+ signatures.push(
80
+ new Uint8Array(byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES)),
81
+ );
82
+ }
83
+
84
+ const message = VersionedMessage.deserialize(new Uint8Array(byteArray));
85
+ return new VersionedTransaction(message, signatures);
86
+ }
87
+
88
+ sign(signers: Array<Signer>) {
89
+ const messageData = this.message.serialize();
90
+ const signerPubkeys = this.message.staticAccountKeys.slice(
91
+ 0,
92
+ this.message.header.numRequiredSignatures,
93
+ );
94
+ for (const signer of signers) {
95
+ const signerIndex = signerPubkeys.findIndex(pubkey =>
96
+ pubkey.equals(signer.publicKey),
97
+ );
98
+ assert(
99
+ signerIndex >= 0,
100
+ `Cannot sign with non signer key ${signer.publicKey.toBase58()}`,
101
+ );
102
+ this.signatures[signerIndex] = nacl.sign.detached(
103
+ messageData,
104
+ signer.secretKey,
105
+ );
106
+ }
107
+ }
108
+ }
@@ -1,20 +1,26 @@
1
- import {URL} from './url-impl';
1
+ const URL_RE = /^[^:]+:\/\/([^:[]+|\[[^\]]+\])(:\d+)?(.*)/i;
2
2
 
3
3
  export function makeWebsocketUrl(endpoint: string) {
4
- let url = new URL(endpoint);
5
- const useHttps = url.protocol === 'https:';
6
-
7
- url.protocol = useHttps ? 'wss:' : 'ws:';
8
- url.host = '';
9
-
10
- // Only shift the port by +1 as a convention for ws(s) only if given endpoint
11
- // is explictly specifying the endpoint port (HTTP-based RPC), assuming
12
- // we're directly trying to connect to solana-validator's ws listening port.
13
- // When the endpoint omits the port, we're connecting to the protocol
14
- // default ports: http(80) or https(443) and it's assumed we're behind a reverse
15
- // proxy which manages WebSocket upgrade and backend port redirection.
16
- if (url.port !== '') {
17
- url.port = String(Number(url.port) + 1);
4
+ const matches = endpoint.match(URL_RE);
5
+ if (matches == null) {
6
+ throw TypeError(`Failed to validate endpoint URL \`${endpoint}\``);
18
7
  }
19
- return url.toString();
8
+ const [
9
+ _, // eslint-disable-line @typescript-eslint/no-unused-vars
10
+ hostish,
11
+ portWithColon,
12
+ rest,
13
+ ] = matches;
14
+ const protocol = endpoint.startsWith('https:') ? 'wss:' : 'ws:';
15
+ const startPort =
16
+ portWithColon == null ? null : parseInt(portWithColon.slice(1), 10);
17
+ const websocketPort =
18
+ // Only shift the port by +1 as a convention for ws(s) only if given endpoint
19
+ // is explictly specifying the endpoint port (HTTP-based RPC), assuming
20
+ // we're directly trying to connect to solana-validator's ws listening port.
21
+ // When the endpoint omits the port, we're connecting to the protocol
22
+ // default ports: http(80) or https(443) and it's assumed we're behind a reverse
23
+ // proxy which manages WebSocket upgrade and backend port redirection.
24
+ startPort == null ? '' : `:${startPort + 1}`;
25
+ return `${protocol}//${hostish}${websocketPort}${rest}`;
20
26
  }
@@ -8,7 +8,7 @@ import {
8
8
 
9
9
  import * as Layout from './layout';
10
10
  import * as shortvec from './utils/shortvec-encoding';
11
- import {PublicKey} from './publickey';
11
+ import {PublicKey, PUBLIC_KEY_LENGTH} from './publickey';
12
12
 
13
13
  export const VALIDATOR_INFO_KEY = new PublicKey(
14
14
  'Va1idator1nfo111111111111111111111111111111',
@@ -77,16 +77,14 @@ export class ValidatorInfo {
77
77
  static fromConfigData(
78
78
  buffer: Buffer | Uint8Array | Array<number>,
79
79
  ): ValidatorInfo | null {
80
- const PUBKEY_LENGTH = 32;
81
-
82
80
  let byteArray = [...buffer];
83
81
  const configKeyCount = shortvec.decodeLength(byteArray);
84
82
  if (configKeyCount !== 2) return null;
85
83
 
86
84
  const configKeys: Array<ConfigKey> = [];
87
85
  for (let i = 0; i < 2; i++) {
88
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
89
- byteArray = byteArray.slice(PUBKEY_LENGTH);
86
+ const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
87
+ byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
90
88
  const isSigner = byteArray.slice(0, 1)[0] === 1;
91
89
  byteArray = byteArray.slice(1);
92
90
  configKeys.push({publicKey, isSigner});
@@ -1,2 +0,0 @@
1
- export {default} from 'react-native-url-polyfill';
2
- export * from 'react-native-url-polyfill';
@@ -1,2 +0,0 @@
1
- export const URL = globalThis.URL;
2
- export const URLSearchParams = globalThis.URLSearchParams;