Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
goga-m committed Apr 2, 2024
1 parent a0bc774 commit 5d7e5c5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/mainsail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@mainsail/crypto-key-pair-ecdsa": "0.0.1-alpha.13",
"@mainsail/crypto-signature-schnorr-secp256k1": "0.0.1-alpha.13",
"@mainsail/crypto-transaction": "0.0.1-alpha.13",
"@mainsail/crypto-transaction-multi-payment": "0.0.1-alpha.13",
"@mainsail/crypto-transaction-transfer": "0.0.1-alpha.13",
"@mainsail/crypto-validation": "0.0.1-alpha.13",
"@mainsail/fees": "0.0.1-alpha.13",
Expand Down
36 changes: 26 additions & 10 deletions packages/mainsail/source/client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,38 @@ export class ClientService extends Services.AbstractClientService {
// @TODO: For the moment only transfer transactions are sent to the
// `transaction-pool` once rest of the transaction types are supported
// we are likely send all of them to the same endpoint.
const isTransfer = transactions.some((t) => t.isTransfer());
const endpointUrl = isTransfer ? "transaction-pool" : "transactions";
const networkHostyType = isTransfer ? "tx" : "full";
// const isTransfer = transactions.some((t) => t.isTransfer());
// const endpointUrl = isTransfer ? "transaction-pool" : "transactions";
// const networkHostyType = isTransfer ? "tx" : "full";

const body = {
transactions: transactions.map((transaction) => transaction.toBroadcast()),
};
// const body = {
// transactions: transactions.map((transaction) => transaction.toBroadcast()),
// };

try {
response = await this.#request.post(
endpointUrl,
const result = await fetch(
// @TODO: Move base url in manifest instead of hardcoded data here.
new URL("https://dwallets.mainsailhq.com/tx/api/transaction-pool"),
{
body,
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
transactions: transactions.map((transaction) => transaction.toBroadcast()),
}),
},
networkHostyType,
);

response = await result.json();

// response = await this.#request.post(
// endpointUrl,
// {
// body,
// },
// networkHostyType,
// );
} catch (error) {
response = (error as any).response.json();
}
Expand Down
74 changes: 61 additions & 13 deletions packages/mainsail/source/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ServiceProvider as CoreCryptoTransactionTransfer,
TransferBuilder,
} from "@mainsail/crypto-transaction-transfer";
import { MultiPaymentBuilder } from "@mainsail/crypto-transaction-multi-payment";
import { Container } from "@mainsail/container";

import { milestones } from "./crypto/networks/devnet/milestones.js";
Expand Down Expand Up @@ -96,10 +97,6 @@ export class TransactionService extends Services.AbstractTransactionService {
* @ledgerS
*/
public override async transfer(input: Services.TransferInput): Promise<Contracts.SignedTransactionData> {
if (!this.#isBooted) {
await this.#boot();
}

return this.#createTransferFromData(input, ({ transaction, data }) => {
transaction.recipientId(data.to);

Expand Down Expand Up @@ -209,15 +206,7 @@ export class TransactionService extends Services.AbstractTransactionService {
* @musig
*/
public override async multiPayment(input: Services.MultiPaymentInput): Promise<Contracts.SignedTransactionData> {
return this.#createFromData("multiPayment", input, ({ transaction, data }) => {
for (const payment of data.payments) {
transaction.addPayment(payment.to, this.toSatoshi(payment.amount).toString());
}

if (data.memo) {
transaction.vendorField(data.memo);
}
});
return this.#createMultipaymentFromData(input);
}

public override async delegateResignation(
Expand Down Expand Up @@ -396,10 +385,69 @@ export class TransactionService extends Services.AbstractTransactionService {
return this.dataTransferObjectService.signedTransaction(signedTransaction.id, signedTransaction);
}

async #createMultipaymentFromData(
input: Services.TransferInput,
callback?: Function,
): Promise<Contracts.SignedTransactionData> {
console.log({ input });
if (!this.#isBooted) {
await this.#boot();
}

applyCryptoConfiguration(this.#configCrypto);

const mnemonic = input.signatory.signingKey();
console.log({ mnemonic });

const transactionWallet = await this.clientService.wallet({
type: "address",
value: input.signatory.address(),
});

// const transactionWallet = await this.clientService.wallet({ type: "address", value: address });
let builder = this.#app
.resolve(MultiPaymentBuilder)
.fee(input.data.fee)
.nonce(transactionWallet.nonce().plus(1).toFixed(0));

console.log({ builder });

if (input.data.memo) {
builder.vendorField(input.data.memo);
}

for (const { amount, to } of input.data.payments) {
console.log("adding payment", { amount, to });
builder = builder.addPayment(to, amount);
}

try {
const signed = await builder.sign(mnemonic);
console.log({ signed });
} catch (error) {
console.log({ error });
}

const signedTransactionBuilder = await builder.sign(mnemonic);

const signedTransaction = await signedTransactionBuilder.build();
console.log({ signedTransaction });

return this.dataTransferObjectService.signedTransaction(
signedTransaction.id!,
signedTransaction.data,
signedTransaction.serialized.toString("hex"),
);
}

async #createTransferFromData(
input: Services.TransferInput,
callback?: Function,
): Promise<Contracts.SignedTransactionData> {
console.log("#createTransferFromData", { input });
if (!this.#isBooted) {
await this.#boot();
}
applyCryptoConfiguration(this.#configCrypto);

// @TODO: update `TransferInput` definition globally once everything
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d7e5c5

Please sign in to comment.