Skip to content

Commit

Permalink
fix and bump
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Feb 22, 2025
1 parent 34ac663 commit 3abe934
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"nodeModulesDir": "none",

"name": "@spacebudz/lucid",
"version": "0.20.5",
"version": "0.20.6",
"license": "MIT",
"exports": {
".": "./mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion lib/lucid/lucid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export class Lucid {
sign: async (
instructionSigner: InstructionSigner,
) => {
const tx = instructionSigner.commit();
const { tx } = instructionSigner.commit();
const witnessSet = await api.signTx(tx, true);
instructionSigner.signWithWitnessSet(witnessSet);
return witnessSet;
Expand Down
4 changes: 2 additions & 2 deletions lib/lucid/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class Tx {
}

/** Add a payment or stake key hash as a required signer of the transaction. */
addSigner(keyHash: string | "{{own.payment}}" | "{{own.stake}}"): Tx {
addSigner(keyHash: string | "{{own.payment}}" | "{{own.delegation}}"): Tx {
this.tasks.push(() => ({ type: "AddSigner", keyHash }));
return this;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ export function resolveInstructions(
if (instruction.keyHash === "{{own.payment}}") {
instruction.keyHash = payment!.hash;
}
if (instruction.keyHash === "{{own.stake}}") {
if (instruction.keyHash === "{{own.delegation}}") {
if (!delegation?.hash) {
throw new Error("Wallet does not have a reward address");
}
Expand Down
11 changes: 8 additions & 3 deletions lib/lucid/tx_complete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Hasher, type InstructionSigner, type Lucid, TxSigned } from "../mod.ts";
import {
Hasher,
type InstructionSigner,
type Lucid,
TxSigned,
} from "../mod.ts";

export class TxComplete {
private instructionSigner: InstructionSigner;
Expand Down Expand Up @@ -75,10 +80,10 @@ export class TxComplete {
}

toString(): string {
return this.instructionSigner.commit();
return this.instructionSigner.commit().tx;
}

toHash(): string {
return Hasher.hashTransaction(this.instructionSigner.commit());
return Hasher.hashTransaction(this.instructionSigner.commit().tx);
}
}
18 changes: 11 additions & 7 deletions lib/lucid/tx_signed.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { Hasher, type Lucid } from "../mod.ts";
import { Hasher, type Lucid, type SignerResult } from "../mod.ts";

export class TxSigned {
tx: string;
private signerResult: SignerResult;
private lucid: Lucid;
constructor(lucid: Lucid, tx: string) {
constructor(lucid: Lucid, signerResult: SignerResult) {
this.lucid = lucid;
this.tx = tx;
this.signerResult = signerResult;
}

async submit(): Promise<string> {
const provider = this.lucid.wallet || this.lucid.provider;
return await provider.submit(
this.tx,
this.signerResult.tx,
);
}

toWitnessSet(): string {
return this.signerResult.witnessSet;
}

toString(): string {
return this.tx;
return this.signerResult.tx;
}

toHash(): string {
return Hasher.hashTransaction(this.tx);
return Hasher.hashTransaction(this.signerResult.tx);
}
}
2 changes: 1 addition & 1 deletion lib/misc/sign_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function signMessage(
),
),
MessageSigning.CBORValue.new_bytes(
fromHex(Crypto.privateKeyToDetails(privateKey).privateKey),
fromHex(Crypto.privateKeyToDetails(privateKey).publicKey),
),
); // x (-2) set to public key

Expand Down
7 changes: 6 additions & 1 deletion rs_lib/pkg/lucid_core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export interface RelevantProtocolParameters {
minfeeRefscriptCostPerByte: number;
}

export interface SignerResult {
tx: string;
witnessSet: string;
}

export class Addresses {
private constructor();
free(): void;
Expand Down Expand Up @@ -207,7 +212,7 @@ export class InstructionSigner {
signWithWitness(witness: string): InstructionSigner;
signWithWitnessSet(set: string): InstructionSigner;
getPartialWitnessSet(): string;
commit(): string;
commit(): SignerResult;
}
export class Utils {
private constructor();
Expand Down
14 changes: 3 additions & 11 deletions rs_lib/pkg/lucid_core_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,19 +1118,11 @@ export class InstructionSigner {
}
}
/**
* @returns {string}
* @returns {SignerResult}
*/
commit() {
let deferred1_0;
let deferred1_1;
try {
const ret = wasm.instructionsigner_commit(this.__wbg_ptr);
deferred1_0 = ret[0];
deferred1_1 = ret[1];
return getStringFromWasm0(ret[0], ret[1]);
} finally {
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
}
const ret = wasm.instructionsigner_commit(this.__wbg_ptr);
return ret;
}
}

Expand Down
Binary file modified rs_lib/pkg/lucid_core_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion rs_lib/pkg/lucid_core_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const instructionsigner_signWithSeed: (a: number, b: number, c: number, d
export const instructionsigner_signWithWitness: (a: number, b: number, c: number) => [number, number, number];
export const instructionsigner_signWithWitnessSet: (a: number, b: number, c: number) => [number, number, number];
export const instructionsigner_getPartialWitnessSet: (a: number) => [number, number, number, number];
export const instructionsigner_commit: (a: number) => [number, number];
export const instructionsigner_commit: (a: number) => any;
export const utils_applyParamsToScript: (a: number, b: number, c: number, d: number) => [number, number, number, number];
export const utils_encodeBech32: (a: number, b: number, c: number, d: number) => [number, number, number, number];
export const utils_applySingleCborEncoding: (a: number, b: number) => [number, number, number, number];
Expand Down
16 changes: 15 additions & 1 deletion rs_lib/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,21 @@ impl Crypto {

#[wasm_bindgen(js_name = xpubToPublicKey)]
pub fn xpub_to_public_key(xpub: &str, part: Part) -> CoreResult<String> {
let bip32_public = Bip32PublicKey::from_bech32(xpub.to_string()).map_err(CoreError::msg)?;
let raw = bech32::decode(xpub)
.map(|(_, raw)| raw)
.or_else(|bech32_err| {
hex::decode(xpub).map_err(|hex_err| {
CoreError::msg(format!(
"Both decoding attempts failed:\n Bech32 error: {}\n Hex error: {}",
bech32_err, hex_err
))
})
})?;

let bip32_public = Bip32PublicKey::from_bytes(
<[u8; 64]>::try_from(raw)
.map_err(|_| CoreError::msg("Invalid xpub size: expected 64 bytes"))?,
);

let public_key = bip32_public
.derive(match part {
Expand Down
8 changes: 4 additions & 4 deletions rs_lib/src/emulator_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ mod tests {

let builder = setup_builder(start_utxos, &address);

let tx = builder
let signed = builder
.commit(Instructions(vec![Instruction::PayTo {
assets: Assets::from_lovelace(10000000),
address: address_other.to_string(),
Expand All @@ -1216,7 +1216,7 @@ mod tests {
.sign_with_key(&private_key)?
.commit();

assert!(state.validate(&tx).is_ok());
assert!(state.validate(&signed.tx).is_ok());

Ok(())
}
Expand Down Expand Up @@ -1262,9 +1262,9 @@ mod tests {

let mut tx = InstructionSigner::from_tx(tx, Some(Utxos(start_utxos)))?;

let signed_tx = tx.sign_with_key(&private_key)?.commit();
let signed = tx.sign_with_key(&private_key)?.commit();

state.validate(&signed_tx).unwrap();
state.validate(&signed.tx).unwrap();
Ok(())
}
}
17 changes: 15 additions & 2 deletions rs_lib/src/instruction_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ use pallas_primitives::{
AddrKeyhash, Fragment, NonEmptySet, TransactionInput,
};
use pallas_traverse::ComputeHash;
use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
collections::{BTreeSet, HashSet},
};
use tsify::Tsify;
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
Expand Down Expand Up @@ -353,18 +355,29 @@ impl InstructionSigner {
}
}

pub fn commit(&mut self) -> String {
pub fn commit(&mut self) -> SignerResult {
if self.vkey_witnesses.len() > 0 {
self.tx.transaction_witness_set.vkeywitness = Some(
NonEmptySet::from_vec(self.vkey_witnesses.iter().map(|s| s.0.clone()).collect())
.unwrap(),
)
}

hex::encode(self.tx.encode_fragment().unwrap())
SignerResult {
tx: hex::encode(self.tx.encode_fragment().unwrap()),
witness_set: hex::encode(self.tx.transaction_witness_set.encode_fragment().unwrap()),
}
}
}

#[derive(Tsify, Serialize, Deserialize, Debug, Clone)]
#[tsify(into_wasm_abi, from_wasm_abi)]
#[serde(rename_all = "camelCase")]
pub struct SignerResult {
pub tx: String,
pub witness_set: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct SignerVKeyWitness(VKeyWitness);

Expand Down

0 comments on commit 3abe934

Please sign in to comment.