2018-10-23 20:56:54 -07:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import elfy from 'elfy';
|
|
|
|
|
|
|
|
import {Account, PublicKey, Loader, SystemProgram} from '.';
|
|
|
|
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
|
|
|
import type {Connection} from '.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
return new PublicKey('0x0606060606060606060606060606060606060606060606060606060606060606');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
* @param elfBytes the entire ELF containing the BPF program in its .text.entrypoint section
|
2018-10-23 20:56:54 -07:00
|
|
|
*/
|
|
|
|
static async load(
|
|
|
|
connection: Connection,
|
|
|
|
owner: Account,
|
2018-10-25 10:41:18 -07:00
|
|
|
elfBytes: Array<number>,
|
2018-10-23 20:56:54 -07:00
|
|
|
): Promise<PublicKey> {
|
|
|
|
const programAccount = new Account();
|
|
|
|
|
2018-10-25 10:41:18 -07:00
|
|
|
const elf = elfy.parse(elfBytes);
|
2018-10-23 20:56:54 -07:00
|
|
|
const section = elf.body.sections.find(section => section.name === '.text.entrypoint');
|
|
|
|
|
|
|
|
const transaction = SystemProgram.createAccount(
|
|
|
|
owner.publicKey,
|
|
|
|
programAccount.publicKey,
|
|
|
|
1,
|
|
|
|
section.data.length,
|
|
|
|
BpfLoader.programId,
|
|
|
|
);
|
|
|
|
await sendAndConfirmTransaction(connection, owner, transaction);
|
|
|
|
|
|
|
|
const loader = new Loader(connection, BpfLoader.programId);
|
|
|
|
await loader.load(programAccount, section.data);
|
|
|
|
await loader.finalize(programAccount);
|
|
|
|
|
|
|
|
return programAccount.publicKey;
|
|
|
|
}
|
|
|
|
}
|