From 159c82eca59fbca8c5f7985acc26815821cd0d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Prohaszka?= Date: Wed, 17 Jul 2024 08:49:13 +0200 Subject: [PATCH] feat(xlm): add combine function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Prohaszka --- .../coin-stellar/src/api/index.integ.test.ts | 20 +++++++++---------- .../coin-stellar/src/api/index.ts | 14 ++++++++----- .../coin-stellar/src/logic/combine.ts | 14 +++++++++++-- .../coin-stellar/src/logic/index.ts | 2 +- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts b/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts index 0ea3b6ef83cd..214944310d97 100644 --- a/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts +++ b/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts @@ -46,17 +46,17 @@ describe("Stellar Api", () => { }); }); - // describe("lastBlock", () => { - // it("returns last block info", async () => { - // // When - // const result = await module.lastBlock(); + describe("lastBlock", () => { + it("returns last block info", async () => { + // When + const result = await module.lastBlock(); - // // Then - // expect(result.hash).toBeDefined(); - // expect(result.height).toBeDefined(); - // expect(result.time).toBeInstanceOf(Date); - // }); - // }); + // Then + expect(result.hash).toBeDefined(); + expect(result.height).toBeDefined(); + expect(result.time).toBeInstanceOf(Date); + }); + }); describe("getBalance", () => { it("returns a list regarding address parameter", async () => { diff --git a/libs/coin-modules/coin-stellar/src/api/index.ts b/libs/coin-modules/coin-stellar/src/api/index.ts index 3976b4a661d1..d1b00f4aa8b7 100644 --- a/libs/coin-modules/coin-stellar/src/api/index.ts +++ b/libs/coin-modules/coin-stellar/src/api/index.ts @@ -2,13 +2,12 @@ import type { Api } from "@ledgerhq/coin-framework/api/index"; import coinConfig, { type StellarConfig } from "../config"; import { broadcast, - // combine, + combine, craftTransaction, // estimateFees, getBalance, listOperations, lastBlock, - // rawEncode, } from "../logic"; export function createApi(config: StellarConfig): Api { @@ -16,9 +15,7 @@ export function createApi(config: StellarConfig): Api { return { broadcast, - combine: () => { - throw new Error("Method not supported"); - }, + combine: compose, craftTransaction: craft, estimateFees: () => Promise.reject(new Error("Method not supported")), getBalance, @@ -66,3 +63,10 @@ async function craft( ); return tx.xdr; } + +function compose(tx: string, signature: string, pubkey?: string): string { + if (!pubkey) { + throw new Error("Missing pubkey"); + } + return combine(tx, signature, pubkey); +} diff --git a/libs/coin-modules/coin-stellar/src/logic/combine.ts b/libs/coin-modules/coin-stellar/src/logic/combine.ts index 40bd5104d2f0..abbc9ac001bc 100644 --- a/libs/coin-modules/coin-stellar/src/logic/combine.ts +++ b/libs/coin-modules/coin-stellar/src/logic/combine.ts @@ -1,3 +1,13 @@ +import { Transaction as StellarSdkTransaction } from "@stellar/stellar-sdk"; -// export function combine(transaction: string, signature: string, publicKey?: string): string { -// } +// https://developers.stellar.org/docs/learn/encyclopedia/network-configuration/network-passphrases +// Mainnet +const NETWORK_PASSPHRASE = "Public Global Stellar Network ; September 2015"; +// Testnet +// const NETWORK_PASSPHRASE = "Test SDF Network ; September 2015"; + +export function combine(transaction: string, signature: string, publicKey: string): string { + const unsignedTx = new StellarSdkTransaction(transaction, NETWORK_PASSPHRASE); + unsignedTx.addSignature(publicKey, signature); + return unsignedTx.toXDR(); +} diff --git a/libs/coin-modules/coin-stellar/src/logic/index.ts b/libs/coin-modules/coin-stellar/src/logic/index.ts index d3954ac7a826..5605cbf5e923 100644 --- a/libs/coin-modules/coin-stellar/src/logic/index.ts +++ b/libs/coin-modules/coin-stellar/src/logic/index.ts @@ -1,5 +1,5 @@ export { broadcast } from "./broadcast"; -// export { combine } from "./combine"; +export { combine } from "./combine"; export { craftTransaction } from "./craftTransaction"; // export { estimateFees } from "./estimateFees"; export { getBalance } from "./getBalance";