2018-10-23 20:56:54 -07:00
|
|
|
// @flow
|
|
|
|
|
2018-10-26 13:19:47 -07:00
|
|
|
import {Account} from './account';
|
|
|
|
import {PublicKey} from './publickey';
|
|
|
|
import {Loader} from './loader';
|
|
|
|
import {SystemProgram} from './system-program';
|
2018-10-23 20:56:54 -07:00
|
|
|
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
2018-10-26 13:19:47 -07:00
|
|
|
import type {Connection} from './connection';
|
2018-10-23 20:56:54 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory class for transactions to interact with a program loader
|
|
|
|
*/
|
|
|
|
export class BpfLoader {
|
|
|
|
/**
|
2018-10-25 10:41:18 -07:00
|
|
|
* Public key that identifies the BpfLoader
|
2018-10-23 20:56:54 -07:00
|
|
|
*/
|
|
|
|
static get programId(): PublicKey {
|
2018-11-04 11:41:21 -08:00
|
|
|
return new PublicKey(
|
2019-04-25 17:43:19 -07:00
|
|
|
'BPFLoader1111111111111111111111111111111111',
|
2018-11-04 11:41:21 -08:00
|
|
|
);
|
2018-10-23 20:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-25 10:41:18 -07:00
|
|
|
* Load a BPF program
|
2018-10-23 20:56:54 -07:00
|
|
|
*
|
|
|
|
* @param connection The connection to use
|
2018-10-25 10:41:18 -07:00
|
|
|
* @param owner User account to load the program into
|
2018-11-06 15:14:59 -08:00
|
|
|
* @param elfBytes The entire ELF containing the BPF program
|
2018-10-23 20:56:54 -07:00
|
|
|
*/
|
|
|
|
static async load(
|
|
|
|
connection: Connection,
|
|
|
|
owner: Account,
|
2018-11-06 15:14:59 -08:00
|
|
|
elf: Array<number>,
|
2018-10-23 20:56:54 -07:00
|
|
|
): Promise<PublicKey> {
|
|
|
|
const programAccount = new Account();
|
|
|
|
|
|
|
|
const transaction = SystemProgram.createAccount(
|
|
|
|
owner.publicKey,
|
|
|
|
programAccount.publicKey,
|
2018-11-27 08:31:06 -08:00
|
|
|
1 + Math.ceil(elf.length / Loader.chunkSize) + 1,
|
2018-11-06 15:14:59 -08:00
|
|
|
elf.length,
|
2018-10-23 20:56:54 -07:00
|
|
|
BpfLoader.programId,
|
|
|
|
);
|
2018-11-18 08:48:14 -08:00
|
|
|
await sendAndConfirmTransaction(connection, transaction, owner);
|
2018-10-23 20:56:54 -07:00
|
|
|
|
|
|
|
const loader = new Loader(connection, BpfLoader.programId);
|
2018-11-06 15:14:59 -08:00
|
|
|
await loader.load(programAccount, elf);
|
2018-10-23 20:56:54 -07:00
|
|
|
await loader.finalize(programAccount);
|
|
|
|
|
|
|
|
return programAccount.publicKey;
|
|
|
|
}
|
|
|
|
}
|