Skip to content

Commit

Permalink
feat: p2pk and claim methods + refactor module types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 23, 2024
1 parent b3f504f commit 5bfecf8
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 86 deletions.
213 changes: 165 additions & 48 deletions wrappers/fedimint-ts/FedimintClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ import type {
DiscoverVersionRequest,
DiscoverVersionResponse,
JoinRequest,
} from "./types";
import type {
AwaitInvoiceRequest,
LnInvoiceExternalPubkeyRequest,
LnInvoiceExternalPubkeyResponse,
LnInvoiceExternalPubkeyTweakedRequest,
LnInvoiceExternalPubkeyTweakedResponse,
LnClaimPubkeyReceiveRequest,
LnClaimPubkeyReceiveTweakedRequest,
LnAwaitInvoiceRequest,
Gateway,
LnInvoiceRequest,
LnInvoiceResponse,
LnPayRequest,
LnPayResponse,
} from "./types";
import type {
CombineRequest,
CombineResponse,
ReissueRequest,
ReissueResponse,
SpendRequest,
SpendResponse,
SplitRequest,
SplitResponse,
ValidateRequest,
ValidateResponse,
} from "./types";
import type {
AwaitDepositRequest,
AwaitDepositResponse,
DepositAddressRequest,
DepositAddressResponse,
WithdrawRequest,
WithdrawResponse,
MintCombineRequest,
MintCombineResponse,
MintReissueRequest,
MintReissueResponse,
MintSpendRequest,
MintSpendResponse,
MintSplitRequest,
MintSplitResponse,
MintValidateRequest,
MintValidateResponse,
OnchainAwaitDepositRequest,
OnchainAwaitDepositResponse,
OnchainDepositAddressRequest,
OnchainDepositAddressResponse,
OnchainWithdrawRequest,
OnchainWithdrawResponse,
} from "./types";

type FedimintResponse<T> = Promise<T>;
Expand Down Expand Up @@ -266,14 +266,129 @@ class FedimintClient {
);
},

/**
* Creates a lightning invoice where the gateway contract locks the ecash to a specific pubkey
* Useful for creating invoices that pay to another user besides yourself
*/
createInvoiceForPubkey: async (
pubkey: string,
amountMsat: number,
description: string,
expiryTime?: number,
federationId?: string
): FedimintResponse<LnInvoiceResponse> => {
const request: LnInvoiceExternalPubkeyRequest = {
externalPubkey: pubkey,
amountMsat,
description,
expiryTime,
};

return await this.postWithId<LnInvoiceExternalPubkeyResponse>(
"/ln/invoice-external-pubkey",
request,
federationId
);
},

/**
* Creates a lightning invoice where the gateway contract locks the ecash to a tweakedpubkey
* Fedimint-clientd tweaks the provided pubkey by the provided tweak, provide the pubkey and tweak
* Useful for creating invoices that pay to another user besides yourself
*/
createInvoiceForPubkeyTweak: async (
pubkey: string,
tweak: number,
amountMsat: number,
description: string,
expiryTime?: number,
federationId?: string
): FedimintResponse<LnInvoiceResponse> => {
const request: LnInvoiceExternalPubkeyTweakedRequest = {
externalPubkey: pubkey,
tweak,
amountMsat,
description,
expiryTime,
};

return await this.postWithId<LnInvoiceExternalPubkeyTweakedResponse>(
"/ln/invoice-external-pubkey-tweaked",
request,
federationId
);
},

/**
* Claims a lightning contract that was paid to a specific pubkey
*/
claimPubkeyReceive: async (
privateKey: string,
federationId?: string
): FedimintResponse<InfoResponse> => {
const request: LnClaimPubkeyReceiveRequest = { privateKey };

return await this.postWithId<InfoResponse>(
"/ln/claim-external-receive",
request,
federationId
);
},

/**
* Claims lightning contracts paid to tweaks of a pubkey
* Provide all the tweaks that were used to create the invoices
*/
claimPubkeyReceiveTweaked: async (
privateKey: string,
tweaks: number[],
federationId?: string
): FedimintResponse<InfoResponse> => {
const request: LnClaimPubkeyReceiveTweakedRequest = {
privateKey,
tweaks,
};

return await this.postWithId<InfoResponse>(
"/ln/claim-external-receive-tweaked",
request,
federationId
);
},

/**
* Creates a lightning invoice where the gateway contract locks the ecash to a specific pubkey
* Useful for creating invoices that pay to another user besides yourself
*/
createInvoicePubkey: async (
pubkey: string,
amountMsat: number,
description: string,
expiryTime?: number,
federationId?: string
): FedimintResponse<LnInvoiceResponse> => {
const request: LnInvoiceExternalPubkeyRequest = {
externalPubkey: pubkey,
amountMsat,
description,
expiryTime,
};

return await this.postWithId<LnInvoiceExternalPubkeyResponse>(
"/ln/invoice-external-pubkey",
request,
federationId
);
},

/**
* Waits for a lightning invoice to be paid
*/
awaitInvoice: async (
operationId: string,
federationId?: string
): FedimintResponse<InfoResponse> => {
const request: AwaitInvoiceRequest = { operationId };
const request: LnAwaitInvoiceRequest = { operationId };

return await this.postWithId<InfoResponse>(
"/ln/await-invoice",
Expand Down Expand Up @@ -321,10 +436,10 @@ class FedimintClient {
reissue: async (
notes: string,
federationId?: string
): FedimintResponse<ReissueResponse> => {
const request: ReissueRequest = { notes };
): FedimintResponse<MintReissueResponse> => {
const request: MintReissueRequest = { notes };

return await this.postWithId<ReissueResponse>(
return await this.postWithId<MintReissueResponse>(
"/mint/reissue",
request,
federationId
Expand All @@ -340,15 +455,15 @@ class FedimintClient {
timeout: number,
includeInvite: boolean,
federationId?: string
): FedimintResponse<SpendResponse> => {
const request: SpendRequest = {
): FedimintResponse<MintSpendResponse> => {
const request: MintSpendRequest = {
amountMsat,
allowOverpay,
timeout,
includeInvite,
};

return await this.postWithId<SpendResponse>(
return await this.postWithId<MintSpendResponse>(
"/mint/spend",
request,
federationId
Expand All @@ -361,10 +476,10 @@ class FedimintClient {
validate: async (
notes: string,
federationId?: string
): FedimintResponse<ValidateResponse> => {
const request: ValidateRequest = { notes };
): FedimintResponse<MintValidateResponse> => {
const request: MintValidateRequest = { notes };

return await this.postWithId<ValidateResponse>(
return await this.postWithId<MintValidateResponse>(
"/mint/validate",
request,
federationId
Expand All @@ -374,19 +489,21 @@ class FedimintClient {
/**
* Splits an ecash note into smaller notes
*/
split: async (notes: string): FedimintResponse<SplitResponse> => {
const request: SplitRequest = { notes };
split: async (notes: string): FedimintResponse<MintSplitResponse> => {
const request: MintSplitRequest = { notes };

return await this.post<SplitResponse>("/mint/split", request);
return await this.post<MintSplitResponse>("/mint/split", request);
},

/**
* Combines ecash notes
*/
combine: async (notesVec: string[]): FedimintResponse<CombineResponse> => {
const request: CombineRequest = { notesVec };
combine: async (
notesVec: string[]
): FedimintResponse<MintCombineResponse> => {
const request: MintCombineRequest = { notesVec };

return await this.post<CombineResponse>("/mint/combine", request);
return await this.post<MintCombineResponse>("/mint/combine", request);
},
};

Expand All @@ -400,10 +517,10 @@ class FedimintClient {
createDepositAddress: async (
timeout: number,
federationId?: string
): FedimintResponse<DepositAddressResponse> => {
const request: DepositAddressRequest = { timeout };
): FedimintResponse<OnchainDepositAddressResponse> => {
const request: OnchainDepositAddressRequest = { timeout };

return await this.postWithId<DepositAddressResponse>(
return await this.postWithId<OnchainDepositAddressResponse>(
"/wallet/deposit-address",
request,
federationId
Expand All @@ -416,10 +533,10 @@ class FedimintClient {
awaitDeposit: async (
operationId: string,
federationId?: string
): FedimintResponse<AwaitDepositResponse> => {
const request: AwaitDepositRequest = { operationId };
): FedimintResponse<OnchainAwaitDepositResponse> => {
const request: OnchainAwaitDepositRequest = { operationId };

return await this.postWithId<AwaitDepositResponse>(
return await this.postWithId<OnchainAwaitDepositResponse>(
"/wallet/await-deposit",
request,
federationId
Expand All @@ -433,10 +550,10 @@ class FedimintClient {
address: string,
amountSat: number | "all",
federationId?: string
): FedimintResponse<WithdrawResponse> => {
const request: WithdrawRequest = { address, amountSat };
): FedimintResponse<OnchainWithdrawResponse> => {
const request: OnchainWithdrawRequest = { address, amountSat };

return await this.postWithId<WithdrawResponse>(
return await this.postWithId<OnchainWithdrawResponse>(
"/wallet/withdraw",
request,
federationId
Expand Down
Binary file modified wrappers/fedimint-ts/bun.lockb
Binary file not shown.
7 changes: 5 additions & 2 deletions wrappers/fedimint-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
"build": "tsc"
},
"devDependencies": {
"bun-types": "latest",
"@types/secp256k1": "^4.0.6",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"bun-types": "latest",
"eslint": "^8",
"typescript": "^5.0.0"
},
"dependencies": {
"crypto": "^1.0.1",
"dotenv": "^16.3.1",
"fedimint-ts": "^0.2.7"
"fedimint-ts": "^0.2.7",
"secp256k1": "^5.0.0"
}
}
26 changes: 26 additions & 0 deletions wrappers/fedimint-ts/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { randomBytes } from "crypto";
import { FedimintClientBuilder } from "./FedimintClient";
import dotenv from "dotenv";
import * as secp256k1 from "secp256k1";

dotenv.config();

Expand All @@ -14,6 +16,25 @@ const logInputAndOutput = (input: any, output: any) => {
console.log("--------------------");
};

interface KeyPair {
privateKey: string;
publicKey: string;
}

const newKeyPair = (): KeyPair => {
let privateKey: Buffer;
do {
privateKey = randomBytes(32);
} while (!secp256k1.privateKeyVerify(privateKey));

const publicKey = secp256k1.publicKeyCreate(privateKey);

return {
privateKey: privateKey.toString("hex"),
publicKey: Buffer.from(publicKey).toString("hex"),
};
};

async function buildTestClient() {
const baseUrl = process.env.BASE_URL || "http://127.0.0.1:3333";
const password = process.env.PASSWORD || "password";
Expand All @@ -29,6 +50,11 @@ async function buildTestClient() {
async function main() {
const fedimintClient = await buildTestClient();

const keyPair = newKeyPair();
console.log("Generated key pair: ", keyPair);

return;

// ADMIN METHODS
// `/v2/admin/config`
logMethod("/v2/admin/config");
Expand Down
Loading

0 comments on commit 5bfecf8

Please sign in to comment.