@solana/web3.js 0.0.0-next
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 +20 -0
- package/README.md +158 -0
- package/lib/index.browser.cjs.js +10062 -0
- package/lib/index.browser.cjs.js.map +1 -0
- package/lib/index.browser.esm.js +9976 -0
- package/lib/index.browser.esm.js.map +1 -0
- package/lib/index.cjs.js +9568 -0
- package/lib/index.cjs.js.map +1 -0
- package/lib/index.d.ts +3311 -0
- package/lib/index.esm.js +9479 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.iife.js +30136 -0
- package/lib/index.iife.js.map +1 -0
- package/lib/index.iife.min.js +40 -0
- package/lib/index.iife.min.js.map +1 -0
- package/package.json +140 -0
- package/src/account.ts +46 -0
- package/src/agent-manager.ts +44 -0
- package/src/blockhash.ts +4 -0
- package/src/bpf-loader-deprecated.ts +5 -0
- package/src/bpf-loader.ts +45 -0
- package/src/connection.ts +4935 -0
- package/src/ed25519-program.ts +157 -0
- package/src/epoch-schedule.ts +102 -0
- package/src/errors.ts +9 -0
- package/src/fee-calculator.ts +16 -0
- package/src/index.ts +31 -0
- package/src/instruction.ts +58 -0
- package/src/keypair.ts +98 -0
- package/src/layout.ts +147 -0
- package/src/loader.ts +236 -0
- package/src/message.ts +271 -0
- package/src/nonce-account.ts +78 -0
- package/src/publickey.ts +296 -0
- package/src/secp256k1-program.ts +229 -0
- package/src/stake-program.ts +923 -0
- package/src/system-program.ts +1007 -0
- package/src/sysvar.ts +37 -0
- package/src/timing.ts +23 -0
- package/src/transaction.ts +808 -0
- package/src/util/assert.ts +8 -0
- package/src/util/borsh-schema.ts +38 -0
- package/src/util/cluster.ts +31 -0
- package/src/util/promise-timeout.ts +14 -0
- package/src/util/send-and-confirm-raw-transaction.ts +46 -0
- package/src/util/send-and-confirm-transaction.ts +50 -0
- package/src/util/shortvec-encoding.ts +28 -0
- package/src/util/sleep.ts +4 -0
- package/src/util/to-buffer.ts +11 -0
- package/src/util/url.ts +18 -0
- package/src/validator-info.ts +106 -0
- package/src/vote-account.ts +236 -0
- package/src/vote-program.ts +413 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {Buffer} from 'buffer';
|
|
2
|
+
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
+
import nacl from 'tweetnacl';
|
|
4
|
+
|
|
5
|
+
import {Keypair} from './keypair';
|
|
6
|
+
import {PublicKey} from './publickey';
|
|
7
|
+
import {TransactionInstruction} from './transaction';
|
|
8
|
+
import assert from './util/assert';
|
|
9
|
+
|
|
10
|
+
const PRIVATE_KEY_BYTES = 64;
|
|
11
|
+
const PUBLIC_KEY_BYTES = 32;
|
|
12
|
+
const SIGNATURE_BYTES = 64;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Params for creating an ed25519 instruction using a public key
|
|
16
|
+
*/
|
|
17
|
+
export type CreateEd25519InstructionWithPublicKeyParams = {
|
|
18
|
+
publicKey: Uint8Array;
|
|
19
|
+
message: Uint8Array;
|
|
20
|
+
signature: Uint8Array;
|
|
21
|
+
instructionIndex?: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Params for creating an ed25519 instruction using a private key
|
|
26
|
+
*/
|
|
27
|
+
export type CreateEd25519InstructionWithPrivateKeyParams = {
|
|
28
|
+
privateKey: Uint8Array;
|
|
29
|
+
message: Uint8Array;
|
|
30
|
+
instructionIndex?: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const ED25519_INSTRUCTION_LAYOUT = BufferLayout.struct<
|
|
34
|
+
Readonly<{
|
|
35
|
+
messageDataOffset: number;
|
|
36
|
+
messageDataSize: number;
|
|
37
|
+
messageInstructionIndex: number;
|
|
38
|
+
numSignatures: number;
|
|
39
|
+
padding: number;
|
|
40
|
+
publicKeyInstructionIndex: number;
|
|
41
|
+
publicKeyOffset: number;
|
|
42
|
+
signatureInstructionIndex: number;
|
|
43
|
+
signatureOffset: number;
|
|
44
|
+
}>
|
|
45
|
+
>([
|
|
46
|
+
BufferLayout.u8('numSignatures'),
|
|
47
|
+
BufferLayout.u8('padding'),
|
|
48
|
+
BufferLayout.u16('signatureOffset'),
|
|
49
|
+
BufferLayout.u16('signatureInstructionIndex'),
|
|
50
|
+
BufferLayout.u16('publicKeyOffset'),
|
|
51
|
+
BufferLayout.u16('publicKeyInstructionIndex'),
|
|
52
|
+
BufferLayout.u16('messageDataOffset'),
|
|
53
|
+
BufferLayout.u16('messageDataSize'),
|
|
54
|
+
BufferLayout.u16('messageInstructionIndex'),
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
export class Ed25519Program {
|
|
58
|
+
/**
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
constructor() {}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Public key that identifies the ed25519 program
|
|
65
|
+
*/
|
|
66
|
+
static programId: PublicKey = new PublicKey(
|
|
67
|
+
'Ed25519SigVerify111111111111111111111111111',
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create an ed25519 instruction with a public key and signature. The
|
|
72
|
+
* public key must be a buffer that is 32 bytes long, and the signature
|
|
73
|
+
* must be a buffer of 64 bytes.
|
|
74
|
+
*/
|
|
75
|
+
static createInstructionWithPublicKey(
|
|
76
|
+
params: CreateEd25519InstructionWithPublicKeyParams,
|
|
77
|
+
): TransactionInstruction {
|
|
78
|
+
const {publicKey, message, signature, instructionIndex} = params;
|
|
79
|
+
|
|
80
|
+
assert(
|
|
81
|
+
publicKey.length === PUBLIC_KEY_BYTES,
|
|
82
|
+
`Public Key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
assert(
|
|
86
|
+
signature.length === SIGNATURE_BYTES,
|
|
87
|
+
`Signature must be ${SIGNATURE_BYTES} bytes but received ${signature.length} bytes`,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const publicKeyOffset = ED25519_INSTRUCTION_LAYOUT.span;
|
|
91
|
+
const signatureOffset = publicKeyOffset + publicKey.length;
|
|
92
|
+
const messageDataOffset = signatureOffset + signature.length;
|
|
93
|
+
const numSignatures = 1;
|
|
94
|
+
|
|
95
|
+
const instructionData = Buffer.alloc(messageDataOffset + message.length);
|
|
96
|
+
|
|
97
|
+
const index =
|
|
98
|
+
instructionIndex == null
|
|
99
|
+
? 0xffff // An index of `u16::MAX` makes it default to the current instruction.
|
|
100
|
+
: instructionIndex;
|
|
101
|
+
|
|
102
|
+
ED25519_INSTRUCTION_LAYOUT.encode(
|
|
103
|
+
{
|
|
104
|
+
numSignatures,
|
|
105
|
+
padding: 0,
|
|
106
|
+
signatureOffset,
|
|
107
|
+
signatureInstructionIndex: index,
|
|
108
|
+
publicKeyOffset,
|
|
109
|
+
publicKeyInstructionIndex: index,
|
|
110
|
+
messageDataOffset,
|
|
111
|
+
messageDataSize: message.length,
|
|
112
|
+
messageInstructionIndex: index,
|
|
113
|
+
},
|
|
114
|
+
instructionData,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
instructionData.fill(publicKey, publicKeyOffset);
|
|
118
|
+
instructionData.fill(signature, signatureOffset);
|
|
119
|
+
instructionData.fill(message, messageDataOffset);
|
|
120
|
+
|
|
121
|
+
return new TransactionInstruction({
|
|
122
|
+
keys: [],
|
|
123
|
+
programId: Ed25519Program.programId,
|
|
124
|
+
data: instructionData,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Create an ed25519 instruction with a private key. The private key
|
|
130
|
+
* must be a buffer that is 64 bytes long.
|
|
131
|
+
*/
|
|
132
|
+
static createInstructionWithPrivateKey(
|
|
133
|
+
params: CreateEd25519InstructionWithPrivateKeyParams,
|
|
134
|
+
): TransactionInstruction {
|
|
135
|
+
const {privateKey, message, instructionIndex} = params;
|
|
136
|
+
|
|
137
|
+
assert(
|
|
138
|
+
privateKey.length === PRIVATE_KEY_BYTES,
|
|
139
|
+
`Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${privateKey.length} bytes`,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const keypair = Keypair.fromSecretKey(privateKey);
|
|
144
|
+
const publicKey = keypair.publicKey.toBytes();
|
|
145
|
+
const signature = nacl.sign.detached(message, keypair.secretKey);
|
|
146
|
+
|
|
147
|
+
return this.createInstructionWithPublicKey({
|
|
148
|
+
publicKey,
|
|
149
|
+
message,
|
|
150
|
+
signature,
|
|
151
|
+
instructionIndex,
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
throw new Error(`Error creating instruction; ${error}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
export interface FeeCalculator {
|
|
14
|
+
/** Cost in lamports to validate a signature. */
|
|
15
|
+
lamportsPerSignature: number;
|
|
16
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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 './ed25519-program';
|
|
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 './publickey';
|
|
14
|
+
export * from './stake-program';
|
|
15
|
+
export * from './system-program';
|
|
16
|
+
export * from './secp256k1-program';
|
|
17
|
+
export * from './transaction';
|
|
18
|
+
export * from './validator-info';
|
|
19
|
+
export * from './vote-account';
|
|
20
|
+
export * from './vote-program';
|
|
21
|
+
export * from './sysvar';
|
|
22
|
+
export * from './errors';
|
|
23
|
+
export * from './util/borsh-schema';
|
|
24
|
+
export * from './util/send-and-confirm-transaction';
|
|
25
|
+
export * from './util/send-and-confirm-raw-transaction';
|
|
26
|
+
export * from './util/cluster';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* There are 1-billion lamports in one SOL
|
|
30
|
+
*/
|
|
31
|
+
export const LAMPORTS_PER_SOL = 1000000000;
|
|
@@ -0,0 +1,58 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import nacl from 'tweetnacl';
|
|
2
|
+
|
|
3
|
+
import {PublicKey} from './publickey';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Keypair signer interface
|
|
7
|
+
*/
|
|
8
|
+
export interface Signer {
|
|
9
|
+
publicKey: PublicKey;
|
|
10
|
+
secretKey: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Ed25519 Keypair
|
|
15
|
+
*/
|
|
16
|
+
export interface Ed25519Keypair {
|
|
17
|
+
publicKey: Uint8Array;
|
|
18
|
+
secretKey: Uint8Array;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An account keypair used for signing transactions.
|
|
23
|
+
*/
|
|
24
|
+
export class Keypair {
|
|
25
|
+
private _keypair: Ed25519Keypair;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create a new keypair instance.
|
|
29
|
+
* Generate random keypair if no {@link Ed25519Keypair} is provided.
|
|
30
|
+
*
|
|
31
|
+
* @param keypair ed25519 keypair
|
|
32
|
+
*/
|
|
33
|
+
constructor(keypair?: Ed25519Keypair) {
|
|
34
|
+
if (keypair) {
|
|
35
|
+
this._keypair = keypair;
|
|
36
|
+
} else {
|
|
37
|
+
this._keypair = nacl.sign.keyPair();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Generate a new random keypair
|
|
43
|
+
*/
|
|
44
|
+
static generate(): Keypair {
|
|
45
|
+
return new Keypair(nacl.sign.keyPair());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a keypair from a raw secret key byte array.
|
|
50
|
+
*
|
|
51
|
+
* This method should only be used to recreate a keypair from a previously
|
|
52
|
+
* generated secret key. Generating keypairs from a random seed should be done
|
|
53
|
+
* with the {@link Keypair.fromSeed} method.
|
|
54
|
+
*
|
|
55
|
+
* @throws error if the provided secret key is invalid and validation is not skipped.
|
|
56
|
+
*
|
|
57
|
+
* @param secretKey secret key byte array
|
|
58
|
+
* @param options: skip secret key validation
|
|
59
|
+
*/
|
|
60
|
+
static fromSecretKey(
|
|
61
|
+
secretKey: Uint8Array,
|
|
62
|
+
options?: {skipValidation?: boolean},
|
|
63
|
+
): Keypair {
|
|
64
|
+
const keypair = nacl.sign.keyPair.fromSecretKey(secretKey);
|
|
65
|
+
if (!options || !options.skipValidation) {
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
const signData = encoder.encode('@solana/web3.js-validation-v1');
|
|
68
|
+
const signature = nacl.sign.detached(signData, keypair.secretKey);
|
|
69
|
+
if (!nacl.sign.detached.verify(signData, signature, keypair.publicKey)) {
|
|
70
|
+
throw new Error('provided secretKey is invalid');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return new Keypair(keypair);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Generate a keypair from a 32 byte seed.
|
|
78
|
+
*
|
|
79
|
+
* @param seed seed byte array
|
|
80
|
+
*/
|
|
81
|
+
static fromSeed(seed: Uint8Array): Keypair {
|
|
82
|
+
return new Keypair(nacl.sign.keyPair.fromSeed(seed));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The public key for this keypair
|
|
87
|
+
*/
|
|
88
|
+
get publicKey(): PublicKey {
|
|
89
|
+
return new PublicKey(this._keypair.publicKey);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The raw secret key for this keypair
|
|
94
|
+
*/
|
|
95
|
+
get secretKey(): Uint8Array {
|
|
96
|
+
return this._keypair.secretKey;
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/layout.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {Buffer} from 'buffer';
|
|
2
|
+
import * as BufferLayout from '@solana/buffer-layout';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Layout for a public key
|
|
6
|
+
*/
|
|
7
|
+
export const publicKey = (property: string = 'publicKey') => {
|
|
8
|
+
return BufferLayout.blob(32, property);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Layout for a 64bit unsigned value
|
|
13
|
+
*/
|
|
14
|
+
export const uint64 = (property: string = 'uint64') => {
|
|
15
|
+
return BufferLayout.blob(8, property);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
interface IRustStringShim
|
|
19
|
+
extends Omit<
|
|
20
|
+
BufferLayout.Structure<
|
|
21
|
+
Readonly<{
|
|
22
|
+
length: number;
|
|
23
|
+
lengthPadding: number;
|
|
24
|
+
chars: Uint8Array;
|
|
25
|
+
}>
|
|
26
|
+
>,
|
|
27
|
+
'decode' | 'encode' | 'replicate'
|
|
28
|
+
> {
|
|
29
|
+
alloc: (str: string) => number;
|
|
30
|
+
decode: (b: Uint8Array, offset?: number) => string;
|
|
31
|
+
encode: (str: string, b: Uint8Array, offset?: number) => number;
|
|
32
|
+
replicate: (property: string) => this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Layout for a Rust String type
|
|
37
|
+
*/
|
|
38
|
+
export const rustString = (
|
|
39
|
+
property: string = 'string',
|
|
40
|
+
): BufferLayout.Layout<string> => {
|
|
41
|
+
const rsl = BufferLayout.struct<
|
|
42
|
+
Readonly<{
|
|
43
|
+
length?: number;
|
|
44
|
+
lengthPadding?: number;
|
|
45
|
+
chars: Uint8Array;
|
|
46
|
+
}>
|
|
47
|
+
>(
|
|
48
|
+
[
|
|
49
|
+
BufferLayout.u32('length'),
|
|
50
|
+
BufferLayout.u32('lengthPadding'),
|
|
51
|
+
BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(), -8), 'chars'),
|
|
52
|
+
],
|
|
53
|
+
property,
|
|
54
|
+
);
|
|
55
|
+
const _decode = rsl.decode.bind(rsl);
|
|
56
|
+
const _encode = rsl.encode.bind(rsl);
|
|
57
|
+
|
|
58
|
+
const rslShim = rsl as unknown as IRustStringShim;
|
|
59
|
+
|
|
60
|
+
rslShim.decode = (b: Uint8Array, offset?: number) => {
|
|
61
|
+
const data = _decode(b, offset);
|
|
62
|
+
return data['chars'].toString();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
rslShim.encode = (str: string, b: Uint8Array, offset?: number) => {
|
|
66
|
+
const data = {
|
|
67
|
+
chars: Buffer.from(str, 'utf8'),
|
|
68
|
+
};
|
|
69
|
+
return _encode(data, b, offset);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
rslShim.alloc = (str: string) => {
|
|
73
|
+
return (
|
|
74
|
+
BufferLayout.u32().span +
|
|
75
|
+
BufferLayout.u32().span +
|
|
76
|
+
Buffer.from(str, 'utf8').length
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return rslShim;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Layout for an Authorized object
|
|
85
|
+
*/
|
|
86
|
+
export const authorized = (property: string = 'authorized') => {
|
|
87
|
+
return BufferLayout.struct<
|
|
88
|
+
Readonly<{
|
|
89
|
+
staker: Uint8Array;
|
|
90
|
+
withdrawer: Uint8Array;
|
|
91
|
+
}>
|
|
92
|
+
>([publicKey('staker'), publicKey('withdrawer')], property);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Layout for a Lockup object
|
|
97
|
+
*/
|
|
98
|
+
export const lockup = (property: string = 'lockup') => {
|
|
99
|
+
return BufferLayout.struct<
|
|
100
|
+
Readonly<{
|
|
101
|
+
custodian: Uint8Array;
|
|
102
|
+
epoch: number;
|
|
103
|
+
unixTimestamp: number;
|
|
104
|
+
}>
|
|
105
|
+
>(
|
|
106
|
+
[
|
|
107
|
+
BufferLayout.ns64('unixTimestamp'),
|
|
108
|
+
BufferLayout.ns64('epoch'),
|
|
109
|
+
publicKey('custodian'),
|
|
110
|
+
],
|
|
111
|
+
property,
|
|
112
|
+
);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Layout for a VoteInit object
|
|
117
|
+
*/
|
|
118
|
+
export const voteInit = (property: string = 'voteInit') => {
|
|
119
|
+
return BufferLayout.struct<
|
|
120
|
+
Readonly<{
|
|
121
|
+
authorizedVoter: Uint8Array;
|
|
122
|
+
authorizedWithdrawer: Uint8Array;
|
|
123
|
+
commission: number;
|
|
124
|
+
nodePubkey: Uint8Array;
|
|
125
|
+
}>
|
|
126
|
+
>(
|
|
127
|
+
[
|
|
128
|
+
publicKey('nodePubkey'),
|
|
129
|
+
publicKey('authorizedVoter'),
|
|
130
|
+
publicKey('authorizedWithdrawer'),
|
|
131
|
+
BufferLayout.u8('commission'),
|
|
132
|
+
],
|
|
133
|
+
property,
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export function getAlloc(type: any, fields: any): number {
|
|
138
|
+
let alloc = 0;
|
|
139
|
+
type.layout.fields.forEach((item: any) => {
|
|
140
|
+
if (item.span >= 0) {
|
|
141
|
+
alloc += item.span;
|
|
142
|
+
} else if (typeof item.alloc === 'function') {
|
|
143
|
+
alloc += item.alloc(fields[item.property]);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return alloc;
|
|
147
|
+
}
|