Add initial wasm bindings for Instruction, SystemProgram and Transaction

This commit is contained in:
Michael Vines
2021-10-18 08:39:21 -07:00
parent 03a956e8d9
commit a35df1cb02
10 changed files with 272 additions and 4 deletions

View File

@ -15,6 +15,7 @@ use {
short_vec,
signature::{Signature, SignerError},
signers::Signers,
wasm_bindgen,
},
serde::Serialize,
solana_program::{system_instruction::SystemInstruction, system_program},
@ -38,6 +39,7 @@ pub enum TransactionVerificationMode {
pub type Result<T> = result::Result<T, TransactionError>;
/// An atomic transaction
#[wasm_bindgen]
#[frozen_abi(digest = "FZtncnS1Xk8ghHfKiXE5oGiUbw2wJhmfXQuNgQR3K6Mc")]
#[derive(Debug, PartialEq, Default, Eq, Clone, Serialize, Deserialize, AbiExample)]
pub struct Transaction {
@ -47,10 +49,12 @@ pub struct Transaction {
/// [`account_keys`]: Message::account_keys
///
// NOTE: Serialization-related changes must be paired with the direct read at sigverify.
#[wasm_bindgen(skip)]
#[serde(with = "short_vec")]
pub signatures: Vec<Signature>,
/// The message to sign.
#[wasm_bindgen(skip)]
pub message: Message,
}

View File

@ -2,3 +2,4 @@
#![cfg(target_arch = "wasm32")]
pub mod keypair;
pub mod transaction;

View File

@ -0,0 +1,58 @@
//! `Transaction` Javascript interface
#![cfg(target_arch = "wasm32")]
#![allow(non_snake_case)]
use {
crate::{
hash::Hash,
signer::keypair::Keypair,
{message::Message, transaction::Transaction},
},
solana_program::{
pubkey::Pubkey,
wasm::{display_to_jsvalue, instructions::Instructions},
},
wasm_bindgen::prelude::*,
};
#[wasm_bindgen]
impl Transaction {
/// Create a new `Transaction`
#[wasm_bindgen(constructor)]
pub fn constructor(instructions: Instructions, payer: Option<Pubkey>) -> Transaction {
let instructions: Vec<_> = instructions.into();
Transaction::new_with_payer(&instructions, payer.as_ref())
}
/// Return a message containing all data that should be signed.
#[wasm_bindgen(js_name = message)]
pub fn js_message(&self) -> Message {
self.message.clone()
}
/// Return the serialized message data to sign.
pub fn messageData(&self) -> Box<[u8]> {
self.message_data().into()
}
/// Verify the transaction
#[wasm_bindgen(js_name = verify)]
pub fn js_verify(&self) -> Result<(), JsValue> {
self.verify().map_err(display_to_jsvalue)
}
pub fn partialSign(&mut self, keypair: &Keypair, recent_blockhash: &Hash) {
self.partial_sign(&[keypair], *recent_blockhash);
}
pub fn isSigned(&self) -> bool {
self.is_signed()
}
pub fn toBytes(&self) -> Box<[u8]> {
bincode::serialize(self).unwrap().into()
}
pub fn fromBytes(bytes: &[u8]) -> Result<Transaction, JsValue> {
bincode::deserialize(bytes).map_err(display_to_jsvalue)
}
}