@solana/web3.js 1.78.0 → 2.0.0-development
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 +56 -112
- package/dist/index.browser.cjs +158 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.js +137 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.development.js +1044 -0
- package/dist/index.development.js.map +1 -0
- package/dist/index.native.js +139 -0
- package/dist/index.native.js.map +1 -0
- package/dist/index.node.cjs +158 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.js +139 -0
- package/dist/index.node.js.map +1 -0
- package/dist/index.production.min.js +25 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/rpc-default-config.d.ts +3 -0
- package/dist/types/rpc-integer-overflow-error.d.ts +8 -0
- package/dist/types/rpc-request-coalescer.d.ts +5 -0
- package/dist/types/rpc-request-deduplication.d.ts +2 -0
- package/dist/types/rpc-transport.d.ts +4 -0
- package/dist/types/rpc.d.ts +5 -0
- package/package.json +86 -111
- package/lib/index.browser.cjs.js +0 -10248
- package/lib/index.browser.cjs.js.map +0 -1
- package/lib/index.browser.esm.js +0 -10145
- package/lib/index.browser.esm.js.map +0 -1
- package/lib/index.cjs.js +0 -12748
- package/lib/index.cjs.js.map +0 -1
- package/lib/index.d.ts +0 -3904
- package/lib/index.esm.js +0 -12638
- package/lib/index.esm.js.map +0 -1
- package/lib/index.iife.js +0 -26757
- package/lib/index.iife.js.map +0 -1
- package/lib/index.iife.min.js +0 -20
- package/lib/index.iife.min.js.map +0 -1
- package/lib/index.native.js +0 -10248
- package/lib/index.native.js.map +0 -1
- package/src/__forks__/browser/fetch-impl.ts +0 -4
- package/src/__forks__/browser/rpc-websocket-factory.ts +0 -1
- package/src/__forks__/react-native/fetch-impl.ts +0 -4
- package/src/__forks__/react-native/rpc-websocket-factory.ts +0 -1
- package/src/account-data.ts +0 -39
- package/src/account.ts +0 -55
- package/src/blockhash.ts +0 -4
- package/src/bpf-loader-deprecated.ts +0 -5
- package/src/bpf-loader.ts +0 -45
- package/src/connection.ts +0 -6796
- package/src/epoch-schedule.ts +0 -102
- package/src/errors.ts +0 -50
- package/src/fee-calculator.ts +0 -18
- package/src/fetch-impl.ts +0 -13
- package/src/index.ts +0 -24
- package/src/instruction.ts +0 -58
- package/src/keypair.ts +0 -93
- package/src/layout.ts +0 -188
- package/src/loader.ts +0 -267
- package/src/message/account-keys.ts +0 -79
- package/src/message/compiled-keys.ts +0 -165
- package/src/message/index.ts +0 -47
- package/src/message/legacy.ts +0 -326
- package/src/message/v0.ts +0 -496
- package/src/message/versioned.ts +0 -36
- package/src/nonce-account.ts +0 -82
- package/src/programs/address-lookup-table/index.ts +0 -435
- package/src/programs/address-lookup-table/state.ts +0 -84
- package/src/programs/compute-budget.ts +0 -281
- package/src/programs/ed25519.ts +0 -157
- package/src/programs/index.ts +0 -7
- package/src/programs/secp256k1.ts +0 -228
- package/src/programs/stake.ts +0 -935
- package/src/programs/system.ts +0 -1048
- package/src/programs/vote.ts +0 -543
- package/src/publickey.ts +0 -259
- package/src/rpc-websocket-factory.ts +0 -4
- package/src/rpc-websocket.ts +0 -79
- package/src/sysvar.ts +0 -37
- package/src/timing.ts +0 -23
- package/src/transaction/constants.ts +0 -12
- package/src/transaction/expiry-custom-errors.ts +0 -48
- package/src/transaction/index.ts +0 -5
- package/src/transaction/legacy.ts +0 -918
- package/src/transaction/message.ts +0 -140
- package/src/transaction/versioned.ts +0 -126
- package/src/utils/assert.ts +0 -8
- package/src/utils/bigint.ts +0 -43
- package/src/utils/borsh-schema.ts +0 -38
- package/src/utils/cluster.ts +0 -31
- package/src/utils/ed25519.ts +0 -43
- package/src/utils/index.ts +0 -5
- package/src/utils/makeWebsocketUrl.ts +0 -26
- package/src/utils/promise-timeout.ts +0 -14
- package/src/utils/secp256k1.ts +0 -11
- package/src/utils/send-and-confirm-raw-transaction.ts +0 -102
- package/src/utils/send-and-confirm-transaction.ts +0 -98
- package/src/utils/shortvec-encoding.ts +0 -28
- package/src/utils/sleep.ts +0 -4
- package/src/utils/to-buffer.ts +0 -11
- package/src/validator-info.ts +0 -104
- package/src/vote-account.ts +0 -236
package/src/epoch-schedule.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
const MINIMUM_SLOT_PER_EPOCH = 32;
|
|
2
|
-
|
|
3
|
-
// Returns the number of trailing zeros in the binary representation of self.
|
|
4
|
-
function trailingZeros(n: number) {
|
|
5
|
-
let trailingZeros = 0;
|
|
6
|
-
while (n > 1) {
|
|
7
|
-
n /= 2;
|
|
8
|
-
trailingZeros++;
|
|
9
|
-
}
|
|
10
|
-
return trailingZeros;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Returns the smallest power of two greater than or equal to n
|
|
14
|
-
function nextPowerOfTwo(n: number) {
|
|
15
|
-
if (n === 0) return 1;
|
|
16
|
-
n--;
|
|
17
|
-
n |= n >> 1;
|
|
18
|
-
n |= n >> 2;
|
|
19
|
-
n |= n >> 4;
|
|
20
|
-
n |= n >> 8;
|
|
21
|
-
n |= n >> 16;
|
|
22
|
-
n |= n >> 32;
|
|
23
|
-
return n + 1;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Epoch schedule
|
|
28
|
-
* (see https://docs.solana.com/terminology#epoch)
|
|
29
|
-
* Can be retrieved with the {@link Connection.getEpochSchedule} method
|
|
30
|
-
*/
|
|
31
|
-
export class EpochSchedule {
|
|
32
|
-
/** The maximum number of slots in each epoch */
|
|
33
|
-
public slotsPerEpoch: number;
|
|
34
|
-
/** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
|
|
35
|
-
public leaderScheduleSlotOffset: number;
|
|
36
|
-
/** Indicates whether epochs start short and grow */
|
|
37
|
-
public warmup: boolean;
|
|
38
|
-
/** The first epoch with `slotsPerEpoch` slots */
|
|
39
|
-
public firstNormalEpoch: number;
|
|
40
|
-
/** The first slot of `firstNormalEpoch` */
|
|
41
|
-
public firstNormalSlot: number;
|
|
42
|
-
|
|
43
|
-
constructor(
|
|
44
|
-
slotsPerEpoch: number,
|
|
45
|
-
leaderScheduleSlotOffset: number,
|
|
46
|
-
warmup: boolean,
|
|
47
|
-
firstNormalEpoch: number,
|
|
48
|
-
firstNormalSlot: number,
|
|
49
|
-
) {
|
|
50
|
-
this.slotsPerEpoch = slotsPerEpoch;
|
|
51
|
-
this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
|
|
52
|
-
this.warmup = warmup;
|
|
53
|
-
this.firstNormalEpoch = firstNormalEpoch;
|
|
54
|
-
this.firstNormalSlot = firstNormalSlot;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
getEpoch(slot: number): number {
|
|
58
|
-
return this.getEpochAndSlotIndex(slot)[0];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
getEpochAndSlotIndex(slot: number): [number, number] {
|
|
62
|
-
if (slot < this.firstNormalSlot) {
|
|
63
|
-
const epoch =
|
|
64
|
-
trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) -
|
|
65
|
-
trailingZeros(MINIMUM_SLOT_PER_EPOCH) -
|
|
66
|
-
1;
|
|
67
|
-
|
|
68
|
-
const epochLen = this.getSlotsInEpoch(epoch);
|
|
69
|
-
const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
|
|
70
|
-
return [epoch, slotIndex];
|
|
71
|
-
} else {
|
|
72
|
-
const normalSlotIndex = slot - this.firstNormalSlot;
|
|
73
|
-
const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
|
|
74
|
-
const epoch = this.firstNormalEpoch + normalEpochIndex;
|
|
75
|
-
const slotIndex = normalSlotIndex % this.slotsPerEpoch;
|
|
76
|
-
return [epoch, slotIndex];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
getFirstSlotInEpoch(epoch: number): number {
|
|
81
|
-
if (epoch <= this.firstNormalEpoch) {
|
|
82
|
-
return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
|
|
83
|
-
} else {
|
|
84
|
-
return (
|
|
85
|
-
(epoch - this.firstNormalEpoch) * this.slotsPerEpoch +
|
|
86
|
-
this.firstNormalSlot
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
getLastSlotInEpoch(epoch: number): number {
|
|
92
|
-
return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
getSlotsInEpoch(epoch: number) {
|
|
96
|
-
if (epoch < this.firstNormalEpoch) {
|
|
97
|
-
return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
|
|
98
|
-
} else {
|
|
99
|
-
return this.slotsPerEpoch;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export class SendTransactionError extends Error {
|
|
2
|
-
logs: string[] | undefined;
|
|
3
|
-
|
|
4
|
-
constructor(message: string, logs?: string[]) {
|
|
5
|
-
super(message);
|
|
6
|
-
|
|
7
|
-
this.logs = logs;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// Keep in sync with client/src/rpc_custom_errors.rs
|
|
12
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
13
|
-
export const SolanaJSONRPCErrorCode = {
|
|
14
|
-
JSON_RPC_SERVER_ERROR_BLOCK_CLEANED_UP: -32001,
|
|
15
|
-
JSON_RPC_SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE: -32002,
|
|
16
|
-
JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE: -32003,
|
|
17
|
-
JSON_RPC_SERVER_ERROR_BLOCK_NOT_AVAILABLE: -32004,
|
|
18
|
-
JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY: -32005,
|
|
19
|
-
JSON_RPC_SERVER_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE: -32006,
|
|
20
|
-
JSON_RPC_SERVER_ERROR_SLOT_SKIPPED: -32007,
|
|
21
|
-
JSON_RPC_SERVER_ERROR_NO_SNAPSHOT: -32008,
|
|
22
|
-
JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: -32009,
|
|
23
|
-
JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: -32010,
|
|
24
|
-
JSON_RPC_SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE: -32011,
|
|
25
|
-
JSON_RPC_SCAN_ERROR: -32012,
|
|
26
|
-
JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH: -32013,
|
|
27
|
-
JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: -32014,
|
|
28
|
-
JSON_RPC_SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION: -32015,
|
|
29
|
-
JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: -32016,
|
|
30
|
-
} as const;
|
|
31
|
-
export type SolanaJSONRPCErrorCodeEnum =
|
|
32
|
-
(typeof SolanaJSONRPCErrorCode)[keyof typeof SolanaJSONRPCErrorCode];
|
|
33
|
-
|
|
34
|
-
export class SolanaJSONRPCError extends Error {
|
|
35
|
-
code: SolanaJSONRPCErrorCodeEnum | unknown;
|
|
36
|
-
data?: any;
|
|
37
|
-
constructor(
|
|
38
|
-
{
|
|
39
|
-
code,
|
|
40
|
-
message,
|
|
41
|
-
data,
|
|
42
|
-
}: Readonly<{code: unknown; message: string; data?: any}>,
|
|
43
|
-
customMessage?: string,
|
|
44
|
-
) {
|
|
45
|
-
super(customMessage != null ? `${customMessage}: ${message}` : message);
|
|
46
|
-
this.code = code;
|
|
47
|
-
this.data = data;
|
|
48
|
-
this.name = 'SolanaJSONRPCError';
|
|
49
|
-
}
|
|
50
|
-
}
|
package/src/fee-calculator.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import * as BufferLayout from '@solana/buffer-layout';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* https://github.com/solana-labs/solana/blob/90bedd7e067b5b8f3ddbb45da00a4e9cabb22c62/sdk/src/fee_calculator.rs#L7-L11
|
|
5
|
-
*
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export const FeeCalculatorLayout = BufferLayout.nu64('lamportsPerSignature');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Calculator for transaction fees.
|
|
12
|
-
*
|
|
13
|
-
* @deprecated Deprecated since Solana v1.8.0.
|
|
14
|
-
*/
|
|
15
|
-
export interface FeeCalculator {
|
|
16
|
-
/** Cost in lamports to validate a signature. */
|
|
17
|
-
lamportsPerSignature: number;
|
|
18
|
-
}
|
package/src/fetch-impl.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import * as nodeFetch from 'node-fetch';
|
|
2
|
-
|
|
3
|
-
export * from 'node-fetch';
|
|
4
|
-
export default async function (
|
|
5
|
-
input: nodeFetch.RequestInfo,
|
|
6
|
-
init?: nodeFetch.RequestInit,
|
|
7
|
-
): Promise<nodeFetch.Response> {
|
|
8
|
-
const processedInput =
|
|
9
|
-
typeof input === 'string' && input.slice(0, 2) === '//'
|
|
10
|
-
? 'https:' + input
|
|
11
|
-
: input;
|
|
12
|
-
return await nodeFetch.default(processedInput, init);
|
|
13
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export * from './account';
|
|
2
|
-
export * from './blockhash';
|
|
3
|
-
export * from './bpf-loader-deprecated';
|
|
4
|
-
export * from './bpf-loader';
|
|
5
|
-
export * from './connection';
|
|
6
|
-
export * from './epoch-schedule';
|
|
7
|
-
export * from './errors';
|
|
8
|
-
export * from './fee-calculator';
|
|
9
|
-
export * from './keypair';
|
|
10
|
-
export * from './loader';
|
|
11
|
-
export * from './message';
|
|
12
|
-
export * from './nonce-account';
|
|
13
|
-
export * from './programs';
|
|
14
|
-
export * from './publickey';
|
|
15
|
-
export * from './transaction';
|
|
16
|
-
export * from './validator-info';
|
|
17
|
-
export * from './vote-account';
|
|
18
|
-
export * from './sysvar';
|
|
19
|
-
export * from './utils';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* There are 1-billion lamports in one SOL
|
|
23
|
-
*/
|
|
24
|
-
export const LAMPORTS_PER_SOL = 1000000000;
|
package/src/instruction.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {Buffer} from 'buffer';
|
|
2
|
-
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
-
|
|
4
|
-
import * as Layout from './layout';
|
|
5
|
-
|
|
6
|
-
export interface IInstructionInputData {
|
|
7
|
-
readonly instruction: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
export type InstructionType<TInputData extends IInstructionInputData> = {
|
|
14
|
-
/** The Instruction index (from solana upstream program) */
|
|
15
|
-
index: number;
|
|
16
|
-
/** The BufferLayout to use to build data */
|
|
17
|
-
layout: BufferLayout.Layout<TInputData>;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Populate a buffer of instruction data using an InstructionType
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
export function encodeData<TInputData extends IInstructionInputData>(
|
|
25
|
-
type: InstructionType<TInputData>,
|
|
26
|
-
fields?: any,
|
|
27
|
-
): Buffer {
|
|
28
|
-
const allocLength =
|
|
29
|
-
type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
|
|
30
|
-
const data = Buffer.alloc(allocLength);
|
|
31
|
-
const layoutFields = Object.assign({instruction: type.index}, fields);
|
|
32
|
-
type.layout.encode(layoutFields, data);
|
|
33
|
-
return data;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Decode instruction data buffer using an InstructionType
|
|
38
|
-
* @internal
|
|
39
|
-
*/
|
|
40
|
-
export function decodeData<TInputData extends IInstructionInputData>(
|
|
41
|
-
type: InstructionType<TInputData>,
|
|
42
|
-
buffer: Buffer,
|
|
43
|
-
): TInputData {
|
|
44
|
-
let data: TInputData;
|
|
45
|
-
try {
|
|
46
|
-
data = type.layout.decode(buffer);
|
|
47
|
-
} catch (err) {
|
|
48
|
-
throw new Error('invalid instruction; ' + err);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (data.instruction !== type.index) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
`invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`,
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return data;
|
|
58
|
-
}
|
package/src/keypair.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import {generateKeypair, getPublicKey, Ed25519Keypair} from './utils/ed25519';
|
|
2
|
-
import {PublicKey} from './publickey';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Keypair signer interface
|
|
6
|
-
*/
|
|
7
|
-
export interface Signer {
|
|
8
|
-
publicKey: PublicKey;
|
|
9
|
-
secretKey: Uint8Array;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* An account keypair used for signing transactions.
|
|
14
|
-
*/
|
|
15
|
-
export class Keypair {
|
|
16
|
-
private _keypair: Ed25519Keypair;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Create a new keypair instance.
|
|
20
|
-
* Generate random keypair if no {@link Ed25519Keypair} is provided.
|
|
21
|
-
*
|
|
22
|
-
* @param keypair ed25519 keypair
|
|
23
|
-
*/
|
|
24
|
-
constructor(keypair?: Ed25519Keypair) {
|
|
25
|
-
this._keypair = keypair ?? generateKeypair();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Generate a new random keypair
|
|
30
|
-
*/
|
|
31
|
-
static generate(): Keypair {
|
|
32
|
-
return new Keypair(generateKeypair());
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Create a keypair from a raw secret key byte array.
|
|
37
|
-
*
|
|
38
|
-
* This method should only be used to recreate a keypair from a previously
|
|
39
|
-
* generated secret key. Generating keypairs from a random seed should be done
|
|
40
|
-
* with the {@link Keypair.fromSeed} method.
|
|
41
|
-
*
|
|
42
|
-
* @throws error if the provided secret key is invalid and validation is not skipped.
|
|
43
|
-
*
|
|
44
|
-
* @param secretKey secret key byte array
|
|
45
|
-
* @param options: skip secret key validation
|
|
46
|
-
*/
|
|
47
|
-
static fromSecretKey(
|
|
48
|
-
secretKey: Uint8Array,
|
|
49
|
-
options?: {skipValidation?: boolean},
|
|
50
|
-
): Keypair {
|
|
51
|
-
if (secretKey.byteLength !== 64) {
|
|
52
|
-
throw new Error('bad secret key size');
|
|
53
|
-
}
|
|
54
|
-
const publicKey = secretKey.slice(32, 64);
|
|
55
|
-
if (!options || !options.skipValidation) {
|
|
56
|
-
const privateScalar = secretKey.slice(0, 32);
|
|
57
|
-
const computedPublicKey = getPublicKey(privateScalar);
|
|
58
|
-
for (let ii = 0; ii < 32; ii++) {
|
|
59
|
-
if (publicKey[ii] !== computedPublicKey[ii]) {
|
|
60
|
-
throw new Error('provided secretKey is invalid');
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return new Keypair({publicKey, secretKey});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Generate a keypair from a 32 byte seed.
|
|
69
|
-
*
|
|
70
|
-
* @param seed seed byte array
|
|
71
|
-
*/
|
|
72
|
-
static fromSeed(seed: Uint8Array): Keypair {
|
|
73
|
-
const publicKey = getPublicKey(seed);
|
|
74
|
-
const secretKey = new Uint8Array(64);
|
|
75
|
-
secretKey.set(seed);
|
|
76
|
-
secretKey.set(publicKey, 32);
|
|
77
|
-
return new Keypair({publicKey, secretKey});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The public key for this keypair
|
|
82
|
-
*/
|
|
83
|
-
get publicKey(): PublicKey {
|
|
84
|
-
return new PublicKey(this._keypair.publicKey);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* The raw secret key for this keypair
|
|
89
|
-
*/
|
|
90
|
-
get secretKey(): Uint8Array {
|
|
91
|
-
return new Uint8Array(this._keypair.secretKey);
|
|
92
|
-
}
|
|
93
|
-
}
|
package/src/layout.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import {Buffer} from 'buffer';
|
|
2
|
-
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
-
|
|
4
|
-
import {VoteAuthorizeWithSeedArgs} from './programs/vote';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Layout for a public key
|
|
8
|
-
*/
|
|
9
|
-
export const publicKey = (property: string = 'publicKey') => {
|
|
10
|
-
return BufferLayout.blob(32, property);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Layout for a signature
|
|
15
|
-
*/
|
|
16
|
-
export const signature = (property: string = 'signature') => {
|
|
17
|
-
return BufferLayout.blob(64, property);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Layout for a 64bit unsigned value
|
|
22
|
-
*/
|
|
23
|
-
export const uint64 = (property: string = 'uint64') => {
|
|
24
|
-
return BufferLayout.blob(8, property);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
interface IRustStringShim
|
|
28
|
-
extends Omit<
|
|
29
|
-
BufferLayout.Structure<
|
|
30
|
-
Readonly<{
|
|
31
|
-
length: number;
|
|
32
|
-
lengthPadding: number;
|
|
33
|
-
chars: Uint8Array;
|
|
34
|
-
}>
|
|
35
|
-
>,
|
|
36
|
-
'decode' | 'encode' | 'replicate'
|
|
37
|
-
> {
|
|
38
|
-
alloc: (str: string) => number;
|
|
39
|
-
decode: (b: Uint8Array, offset?: number) => string;
|
|
40
|
-
encode: (str: string, b: Uint8Array, offset?: number) => number;
|
|
41
|
-
replicate: (property: string) => this;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Layout for a Rust String type
|
|
46
|
-
*/
|
|
47
|
-
export const rustString = (
|
|
48
|
-
property: string = 'string',
|
|
49
|
-
): BufferLayout.Layout<string> => {
|
|
50
|
-
const rsl = BufferLayout.struct<
|
|
51
|
-
Readonly<{
|
|
52
|
-
length?: number;
|
|
53
|
-
lengthPadding?: number;
|
|
54
|
-
chars: Uint8Array;
|
|
55
|
-
}>
|
|
56
|
-
>(
|
|
57
|
-
[
|
|
58
|
-
BufferLayout.u32('length'),
|
|
59
|
-
BufferLayout.u32('lengthPadding'),
|
|
60
|
-
BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(), -8), 'chars'),
|
|
61
|
-
],
|
|
62
|
-
property,
|
|
63
|
-
);
|
|
64
|
-
const _decode = rsl.decode.bind(rsl);
|
|
65
|
-
const _encode = rsl.encode.bind(rsl);
|
|
66
|
-
|
|
67
|
-
const rslShim = rsl as unknown as IRustStringShim;
|
|
68
|
-
|
|
69
|
-
rslShim.decode = (b: Uint8Array, offset?: number) => {
|
|
70
|
-
const data = _decode(b, offset);
|
|
71
|
-
return data['chars'].toString();
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
rslShim.encode = (str: string, b: Uint8Array, offset?: number) => {
|
|
75
|
-
const data = {
|
|
76
|
-
chars: Buffer.from(str, 'utf8'),
|
|
77
|
-
};
|
|
78
|
-
return _encode(data, b, offset);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
rslShim.alloc = (str: string) => {
|
|
82
|
-
return (
|
|
83
|
-
BufferLayout.u32().span +
|
|
84
|
-
BufferLayout.u32().span +
|
|
85
|
-
Buffer.from(str, 'utf8').length
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
return rslShim;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Layout for an Authorized object
|
|
94
|
-
*/
|
|
95
|
-
export const authorized = (property: string = 'authorized') => {
|
|
96
|
-
return BufferLayout.struct<
|
|
97
|
-
Readonly<{
|
|
98
|
-
staker: Uint8Array;
|
|
99
|
-
withdrawer: Uint8Array;
|
|
100
|
-
}>
|
|
101
|
-
>([publicKey('staker'), publicKey('withdrawer')], property);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Layout for a Lockup object
|
|
106
|
-
*/
|
|
107
|
-
export const lockup = (property: string = 'lockup') => {
|
|
108
|
-
return BufferLayout.struct<
|
|
109
|
-
Readonly<{
|
|
110
|
-
custodian: Uint8Array;
|
|
111
|
-
epoch: number;
|
|
112
|
-
unixTimestamp: number;
|
|
113
|
-
}>
|
|
114
|
-
>(
|
|
115
|
-
[
|
|
116
|
-
BufferLayout.ns64('unixTimestamp'),
|
|
117
|
-
BufferLayout.ns64('epoch'),
|
|
118
|
-
publicKey('custodian'),
|
|
119
|
-
],
|
|
120
|
-
property,
|
|
121
|
-
);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Layout for a VoteInit object
|
|
126
|
-
*/
|
|
127
|
-
export const voteInit = (property: string = 'voteInit') => {
|
|
128
|
-
return BufferLayout.struct<
|
|
129
|
-
Readonly<{
|
|
130
|
-
authorizedVoter: Uint8Array;
|
|
131
|
-
authorizedWithdrawer: Uint8Array;
|
|
132
|
-
commission: number;
|
|
133
|
-
nodePubkey: Uint8Array;
|
|
134
|
-
}>
|
|
135
|
-
>(
|
|
136
|
-
[
|
|
137
|
-
publicKey('nodePubkey'),
|
|
138
|
-
publicKey('authorizedVoter'),
|
|
139
|
-
publicKey('authorizedWithdrawer'),
|
|
140
|
-
BufferLayout.u8('commission'),
|
|
141
|
-
],
|
|
142
|
-
property,
|
|
143
|
-
);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Layout for a VoteAuthorizeWithSeedArgs object
|
|
148
|
-
*/
|
|
149
|
-
export const voteAuthorizeWithSeedArgs = (
|
|
150
|
-
property: string = 'voteAuthorizeWithSeedArgs',
|
|
151
|
-
) => {
|
|
152
|
-
return BufferLayout.struct<VoteAuthorizeWithSeedArgs>(
|
|
153
|
-
[
|
|
154
|
-
BufferLayout.u32('voteAuthorizationType'),
|
|
155
|
-
publicKey('currentAuthorityDerivedKeyOwnerPubkey'),
|
|
156
|
-
rustString('currentAuthorityDerivedKeySeed'),
|
|
157
|
-
publicKey('newAuthorized'),
|
|
158
|
-
],
|
|
159
|
-
property,
|
|
160
|
-
);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
export function getAlloc(type: any, fields: any): number {
|
|
164
|
-
const getItemAlloc = (item: any): number => {
|
|
165
|
-
if (item.span >= 0) {
|
|
166
|
-
return item.span;
|
|
167
|
-
} else if (typeof item.alloc === 'function') {
|
|
168
|
-
return item.alloc(fields[item.property]);
|
|
169
|
-
} else if ('count' in item && 'elementLayout' in item) {
|
|
170
|
-
const field = fields[item.property];
|
|
171
|
-
if (Array.isArray(field)) {
|
|
172
|
-
return field.length * getItemAlloc(item.elementLayout);
|
|
173
|
-
}
|
|
174
|
-
} else if ('fields' in item) {
|
|
175
|
-
// This is a `Structure` whose size needs to be recursively measured.
|
|
176
|
-
return getAlloc({layout: item}, fields[item.property]);
|
|
177
|
-
}
|
|
178
|
-
// Couldn't determine allocated size of layout
|
|
179
|
-
return 0;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
let alloc = 0;
|
|
183
|
-
type.layout.fields.forEach((item: any) => {
|
|
184
|
-
alloc += getItemAlloc(item);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
return alloc;
|
|
188
|
-
}
|