From 52c1dd97c83fac2c8c18df282f9a79e58bd4bfa5 Mon Sep 17 00:00:00 2001 From: "kody.low" Date: Sat, 23 Mar 2024 16:01:11 -0700 Subject: [PATCH] feat: finish updates to fedimint-go for 0.3 --- wrappers/fedimint-go/cmd/main.go | 2 +- wrappers/fedimint-go/pkg/fedimint/client.go | 183 ++++++++++++++---- .../pkg/fedimint/types/modules/ln.go | 73 ++++--- .../pkg/fedimint/types/modules/mint.go | 75 ++++--- .../types/modules/{wallet.go => onchain.go} | 12 +- 5 files changed, 245 insertions(+), 100 deletions(-) rename wrappers/fedimint-go/pkg/fedimint/types/modules/{wallet.go => onchain.go} (58%) diff --git a/wrappers/fedimint-go/cmd/main.go b/wrappers/fedimint-go/cmd/main.go index 77a6e24..4753806 100644 --- a/wrappers/fedimint-go/cmd/main.go +++ b/wrappers/fedimint-go/cmd/main.go @@ -54,7 +54,7 @@ func main() { fmt.Println("Waiting for payment...") - awaitInvoiceRequest := modules.AwaitInvoiceRequest{ + awaitInvoiceRequest := modules.LnAwaitInvoiceRequest{ OperationID: invoiceResponse.OperationID, } diff --git a/wrappers/fedimint-go/pkg/fedimint/client.go b/wrappers/fedimint-go/pkg/fedimint/client.go index 0ad21ef..1f15d1a 100644 --- a/wrappers/fedimint-go/pkg/fedimint/client.go +++ b/wrappers/fedimint-go/pkg/fedimint/client.go @@ -237,13 +237,13 @@ func (fc *FedimintClient) Join(inviteCode string, setActiveFederationId bool, us // Onchain // //////////// -func (onchain *OnchainModule) createDepositAddress(timeout int, federationId *string) (*modules.DepositAddressResponse, error) { - request := modules.DepositAddressRequest{Timeout: timeout} +func (onchain *OnchainModule) createDepositAddress(timeout int, federationId *string) (*modules.OnchainDepositAddressResponse, error) { + request := modules.OnchainDepositAddressRequest{Timeout: timeout} resp, err := onchain.Client.postWithFederationId("/onchain/deposit-address", request, *federationId) if err != nil { return nil, err } - var depositAddressResp modules.DepositAddressResponse + var depositAddressResp modules.OnchainDepositAddressResponse err = json.Unmarshal(resp, &depositAddressResp) if err != nil { return nil, err @@ -251,13 +251,13 @@ func (onchain *OnchainModule) createDepositAddress(timeout int, federationId *st return &depositAddressResp, nil } -func (onchain *OnchainModule) awaitDeposit(operationId string, federationId *string) (*modules.AwaitDepositResponse, error) { - request := modules.AwaitDepositRequest{OperationId: operationId} +func (onchain *OnchainModule) awaitDeposit(operationId string, federationId *string) (*modules.OnchainAwaitDepositResponse, error) { + request := modules.OnchainAwaitDepositRequest{OperationId: operationId} resp, err := onchain.Client.postWithFederationId("/onchain/await-deposit", request, *federationId) if err != nil { return nil, err } - var depositResp modules.AwaitDepositResponse + var depositResp modules.OnchainAwaitDepositResponse err = json.Unmarshal(resp, &depositResp) if err != nil { return nil, err @@ -265,13 +265,13 @@ func (onchain *OnchainModule) awaitDeposit(operationId string, federationId *str return &depositResp, nil } -func (onchain *OnchainModule) withdraw(address string, amountSat int, federationId *string) (*modules.WithdrawResponse, error) { - request := modules.WithdrawRequest{Address: address, AmountSat: amountSat} +func (onchain *OnchainModule) withdraw(address string, amountSat int, federationId *string) (*modules.OnchainWithdrawResponse, error) { + request := modules.OnchainWithdrawRequest{Address: address, AmountSat: amountSat} resp, err := onchain.Client.postWithFederationId("/onchain/withdraw", request, *federationId) if err != nil { return nil, err } - var withdrawResp modules.WithdrawResponse + var withdrawResp modules.OnchainWithdrawResponse err = json.Unmarshal(resp, &withdrawResp) if err != nil { return nil, err @@ -283,12 +283,45 @@ func (onchain *OnchainModule) withdraw(address string, amountSat int, federation // mint // ////////// -func (mint *MintModule) Reissue(request modules.ReissueRequest, federationId *string) (*modules.ReissueResponse, error) { +func (mint *MintModule) DecodeNotes(notes string, federationId *string) (*modules.DecodeNotesResponse, error) { + request := modules.DecodeNotesRequest{Notes: notes} + resp, err := mint.Client.postWithFederationId("/mint/decode-notes", request, *federationId) + if err != nil { + return nil, err + } + var decodeResp modules.DecodeNotesResponse + err = json.Unmarshal(resp, &decodeResp) + if err != nil { + return nil, err + } + return &decodeResp, nil +} + +func (mint *MintModule) EncodeNotes(notesJson modules.NotesJson, federationId *string) (*modules.EncodeNotesResponse, error) { + notesJsonStr, err := json.Marshal(notesJson) + if err != nil { + return nil, err + } + request := modules.EncodeNotesRequest{NotesJsonStr: string(notesJsonStr)} + resp, err := mint.Client.postWithFederationId("/mint/encode-notes", request, *federationId) + if err != nil { + return nil, err + } + var encodeResp modules.EncodeNotesResponse + err = json.Unmarshal(resp, &encodeResp) + if err != nil { + return nil, err + } + return &encodeResp, nil +} + +func (mint *MintModule) Reissue(notes string, federationId *string) (*modules.MintReissueResponse, error) { + request := modules.MintReissueRequest{Notes: notes} resp, err := mint.Client.postWithFederationId("/mint/reissue", request, *federationId) if err != nil { return nil, err } - var reissueResp modules.ReissueResponse + var reissueResp modules.MintReissueResponse err = json.Unmarshal(resp, &reissueResp) if err != nil { return nil, err @@ -296,12 +329,18 @@ func (mint *MintModule) Reissue(request modules.ReissueRequest, federationId *st return &reissueResp, nil } -func (mint *MintModule) Spend(request modules.SpendRequest, federationId *string) (*modules.SpendResponse, error) { +func (mint *MintModule) Spend(amountMsat uint64, allowOverpay bool, timeout uint64, includeInvite bool, federationId *string) (*modules.MintSpendResponse, error) { + request := modules.MintSpendRequest{ + AmountMsat: amountMsat, + AllowOverpay: allowOverpay, + Timeout: timeout, + IncludeInvite: includeInvite, + } resp, err := mint.Client.postWithFederationId("/mint/spend", request, *federationId) if err != nil { return nil, err } - var spendResp modules.SpendResponse + var spendResp modules.MintSpendResponse err = json.Unmarshal(resp, &spendResp) if err != nil { return nil, err @@ -309,12 +348,13 @@ func (mint *MintModule) Spend(request modules.SpendRequest, federationId *string return &spendResp, nil } -func (mint *MintModule) Validate(request modules.ValidateRequest, federationId *string) (*modules.ValidateResponse, error) { +func (mint *MintModule) Validate(notes string, federationId *string) (*modules.MintValidateResponse, error) { + request := modules.MintValidateRequest{Notes: notes} resp, err := mint.Client.postWithFederationId("/mint/validate", request, *federationId) if err != nil { return nil, err } - var validateResp modules.ValidateResponse + var validateResp modules.MintValidateResponse err = json.Unmarshal(resp, &validateResp) if err != nil { return nil, err @@ -322,12 +362,13 @@ func (mint *MintModule) Validate(request modules.ValidateRequest, federationId * return &validateResp, nil } -func (mint *MintModule) Split(request modules.SplitRequest) (*modules.SplitResponse, error) { +func (mint *MintModule) Split(notes string) (*modules.MintSplitResponse, error) { + request := modules.MintSplitRequest{Notes: notes} resp, err := mint.Client.post("/mint/split", request) if err != nil { return nil, err } - var splitResp modules.SplitResponse + var splitResp modules.MintSplitResponse err = json.Unmarshal(resp, &splitResp) if err != nil { return nil, err @@ -335,12 +376,13 @@ func (mint *MintModule) Split(request modules.SplitRequest) (*modules.SplitRespo return &splitResp, nil } -func (mint *MintModule) Combine(request modules.CombineRequest) (*modules.CombineResponse, error) { +func (mint *MintModule) Combine(notesVec []string) (*modules.MintCombineResponse, error) { + request := modules.MintCombineRequest{NotesVec: notesVec} resp, err := mint.Client.post("/mint/combine", request) if err != nil { return nil, err } - var combineResp modules.CombineResponse + var combineResp modules.MintCombineResponse err = json.Unmarshal(resp, &combineResp) if err != nil { return nil, err @@ -352,9 +394,14 @@ func (mint *MintModule) Combine(request modules.CombineRequest) (*modules.Combin // Ln // //////// -func (ln *LnModule) CreateInvoice(request modules.LnInvoiceRequest, federationId *string) (*modules.LnInvoiceResponse, error) { +func (ln *LnModule) CreateInvoice(amountMsat uint64, description string, expiryTime *int, gatewayId *string, federationId *string) (*modules.LnInvoiceResponse, error) { + request := modules.LnInvoiceRequest{ + AmountMsat: amountMsat, + Description: description, + ExpiryTime: expiryTime, + } fmt.Println("request: ", request) - resp, err := ln.Client.postWithFederationId("/ln/invoice", request, *federationId) + resp, err := ln.Client.postWithGatewayIdAndFederationId("/ln/invoice", request, *gatewayId, *federationId) if err != nil { return nil, err } @@ -366,8 +413,50 @@ func (ln *LnModule) CreateInvoice(request modules.LnInvoiceRequest, federationId return &invoiceResp, nil } -func (ln *LnModule) AwaitInvoice(request modules.AwaitInvoiceRequest, federationId *string) (*types.InfoResponse, error) { - resp, err := ln.Client.postWithFederationId("/ln/await-invoice", request, *federationId) +func (ln *LnModule) CreateInvoiceForPubkey(pubkey string, amountMsat uint64, description string, expiryTime *int, gatewayId *string, federationId *string) (*modules.LnInvoiceResponse, error) { + request := modules.LnInvoiceExternalPubkeyRequest{ + AmountMsat: amountMsat, + Description: description, + ExpiryTime: expiryTime, + ExternalPubkey: pubkey, + } + fmt.Println("request: ", request) + resp, err := ln.Client.postWithGatewayIdAndFederationId("/ln/invoice-external-pubkey", request, *gatewayId, *federationId) + if err != nil { + return nil, err + } + var invoiceResp modules.LnInvoiceResponse + err = json.Unmarshal(resp, &invoiceResp) + if err != nil { + return nil, err + } + return &invoiceResp, nil +} + +func (ln *LnModule) CreateInvoiceForPubkeyTweak(pubkey string, tweak uint64, amountMsat uint64, description string, expiryTime *int, gatewayId *string, federationId *string) (*modules.LnInvoiceResponse, error) { + request := modules.LnInvoiceExternalPubkeyTweakedRequest{ + AmountMsat: amountMsat, + Description: description, + ExpiryTime: expiryTime, + ExternalPubkey: pubkey, + Tweak: tweak, + } + fmt.Println("request: ", request) + resp, err := ln.Client.postWithGatewayIdAndFederationId("/ln/invoice-external-pubkey-tweaked", request, *gatewayId, *federationId) + if err != nil { + return nil, err + } + var invoiceResp modules.LnInvoiceResponse + err = json.Unmarshal(resp, &invoiceResp) + if err != nil { + return nil, err + } + return &invoiceResp, nil +} + +func (ln *LnModule) ClaimPubkeyReceive(privateKey string, federationId *string) (*types.InfoResponse, error) { + request := modules.LnClaimPubkeyReceiveRequest{PrivateKey: privateKey} + resp, err := ln.Client.postWithFederationId("/ln/claim-external-receive", request, *federationId) if err != nil { return nil, err } @@ -379,54 +468,62 @@ func (ln *LnModule) AwaitInvoice(request modules.AwaitInvoiceRequest, federation return &infoResp, nil } -func (ln *LnModule) Pay(request modules.LnPayRequest, federationId *string) (*modules.LnPayResponse, error) { - resp, err := ln.Client.postWithFederationId("/ln/pay", request, *federationId) +func (ln *LnModule) ClaimPubkeyReceiveTweaked(privateKey string, tweaks []uint64, federationId *string) (*types.InfoResponse, error) { + request := modules.LnClaimPubkeyTweakedRequest{PrivateKey: privateKey, Tweaks: tweaks} + resp, err := ln.Client.postWithFederationId("/ln/claim-external-receive-tweaked", request, *federationId) if err != nil { return nil, err } - var payResp modules.LnPayResponse - err = json.Unmarshal(resp, &payResp) + var infoResp types.InfoResponse + err = json.Unmarshal(resp, &infoResp) if err != nil { return nil, err } - return &payResp, nil + return &infoResp, nil } -func (ln *LnModule) AwaitPay(request modules.AwaitLnPayRequest, federationId *string) (*modules.LnPayResponse, error) { - resp, err := ln.Client.postWithFederationId("/ln/await-pay", request, *federationId) +func (ln *LnModule) AwaitInvoice(operationId string, federationId *string) (*types.InfoResponse, error) { + request := modules.LnAwaitInvoiceRequest{OperationId: operationId} + resp, err := ln.Client.postWithFederationId("/ln/await-invoice", request, *federationId) if err != nil { return nil, err } - var payResp modules.LnPayResponse - err = json.Unmarshal(resp, &payResp) + var infoResp types.InfoResponse + err = json.Unmarshal(resp, &infoResp) if err != nil { return nil, err } - return &payResp, nil + return &infoResp, nil } -func (ln *LnModule) ListGateways() ([]modules.Gateway, error) { - resp, err := ln.Client.get("/ln/list-gateways") +func (ln *LnModule) Pay(paymentInfo string, amountMsat *uint64, lnurlComment *string, gatewayId *string, federationId *string) (*modules.LnPayResponse, error) { + request := modules.LnPayRequest{ + PaymentInfo: paymentInfo, + AmountMsat: amountMsat, + LnurlComment: lnurlComment, + } + fmt.Println("request: ", request) + resp, err := ln.Client.postWithGatewayIdAndFederationId("/ln/pay", request, *gatewayId, *federationId) if err != nil { return nil, err } - var gateways []modules.Gateway - err = json.Unmarshal(resp, &gateways) + var payResp modules.LnPayResponse + err = json.Unmarshal(resp, &payResp) if err != nil { return nil, err } - return gateways, nil + return &payResp, nil } -func (ln *LnModule) SwitchGateway(request modules.SwitchGatewayRequest, federationId *string) (*modules.Gateway, error) { - resp, err := ln.Client.postWithFederationId("/ln/switch-gateway", request, *federationId) +func (ln *LnModule) ListGateways() ([]modules.Gateway, error) { + resp, err := ln.Client.get("/ln/list-gateways") if err != nil { return nil, err } - var gateway modules.Gateway - err = json.Unmarshal(resp, &gateway) + var gateways []modules.Gateway + err = json.Unmarshal(resp, &gateways) if err != nil { return nil, err } - return &gateway, nil + return gateways, nil } diff --git a/wrappers/fedimint-go/pkg/fedimint/types/modules/ln.go b/wrappers/fedimint-go/pkg/fedimint/types/modules/ln.go index 4864c98..2f1466c 100644 --- a/wrappers/fedimint-go/pkg/fedimint/types/modules/ln.go +++ b/wrappers/fedimint-go/pkg/fedimint/types/modules/ln.go @@ -1,53 +1,76 @@ package modules type LnInvoiceRequest struct { - AmountMsat int `json:"amount_msat"` + AmountMsat uint64 `json:"amountMsat"` Description string `json:"description"` - ExpiryTime *int `json:"expiry_time"` + ExpiryTime *int `json:"expiryTime"` +} + +type LnInvoiceExternalPubkeyRequest struct { + AmountMsat uint64 `json:"amountMsat"` + Description string `json:"description"` + ExpiryTime *int `json:"expiryTime"` + ExternalPubkey string `json:"externalPubkey"` +} + +type LnInvoiceExternalPubkeyTweakedRequest struct { + AmountMsat uint64 `json:"amountMsat"` + Description string `json:"description"` + ExpiryTime *int `json:"expiryTime"` + ExternalPubkey string `json:"externalPubkey"` + Tweak uint64 `json:"tweak"` } type LnInvoiceResponse struct { - OperationID string `json:"operation_id"` + OperationId string `json:"operationId"` Invoice string `json:"invoice"` } -type AwaitInvoiceRequest struct { - OperationID string `json:"operation_id"` +type LnClaimPubkeyReceiveRequest struct { + PrivateKey string `json:"privateKey"` +} + +type LnClaimPubkeyTweakedRequest struct { + PrivateKey string `json:"privateKey"` + Tweaks []uint64 `json:"tweaks"` +} + +type LnAwaitInvoiceRequest struct { + OperationId string `json:"operationId"` } type LnPayRequest struct { - Payment_info string `json:"payment_info"` - Amount_msat *int `json:"amount_msat"` - Finish_in_background bool `json:"finish_in_background"` - Lnurl_comment *string `json:"lnurl_comment"` + PaymentInfo string `json:"paymentInfo"` + AmountMsat *uint64 `json:"amountMsat"` + LnurlComment *string `json:"lnurlComment"` } type LnPayResponse struct { - Pperation_id string `json:"operation_id"` - Payment_type string `json:"payment_type"` - Contract_id string `json:"contract_id"` - Fee int `json:"fee"` + PperationId string `json:"operationId"` + PaymentType string `json:"paymentType"` + ContractId string `json:"contractId"` + Fee int `json:"fee"` } type AwaitLnPayRequest struct { - Operation_id string `json:"operation_id"` + OperationId string `json:"operationId"` } type GatewayInfo struct { API string `json:"api"` Fees GatewayFees `json:"fees"` - GatewayID string `json:"gateway_id"` - GatewayRedeemKey string `json:"gateway_redeem_key"` - LightningAlias string `json:"lightning_alias"` - MintChannelID int `json:"mint_channel_id"` - NodePubKey string `json:"node_pub_key"` - RouteHints []interface{} `json:"route_hints"` // Adjust the type according to the actual structure of route hints - SupportsPrivatePayments bool `json:"supports_private_payments"` + GatewayID string `json:"gatewayId"` + GatewayRedeemKey string `json:"gatewayRedeemKey"` + LightningAlias string `json:"lightningAlias"` + MintChannelID int `json:"mintChannelId"` + NodePubKey string `json:"nodePubKey"` + RouteHints []interface{} `json:"routeHints"` // Adjust the type according to the actual structure of route hints + SupportsPrivatePayments bool `json:"supportsPrivatePayments"` } type GatewayFees struct { - BaseMsat int `json:"base_msat"` - ProportionalMillionths int `json:"proportional_millionths"` + BaseMsat int `json:"baseMsat"` + ProportionalMillionths int `json:"proportionalMillionths"` } type GatewayTTL struct { @@ -56,7 +79,7 @@ type GatewayTTL struct { } type Gateway struct { - FederationID string `json:"federation_id"` + FederationID string `json:"federationId"` Info GatewayInfo `json:"info"` TTL GatewayTTL `json:"ttl"` Vetted bool `json:"vetted"` @@ -66,5 +89,5 @@ type Gateway struct { type ListGatewaysResponse map[string][]Gateway type SwitchGatewayRequest struct { - Gateway_id string `json:"gateway_id"` + GatewayId string `json:"gatewayId"` } diff --git a/wrappers/fedimint-go/pkg/fedimint/types/modules/mint.go b/wrappers/fedimint-go/pkg/fedimint/types/modules/mint.go index 7d3cbff..1b6c5a5 100644 --- a/wrappers/fedimint-go/pkg/fedimint/types/modules/mint.go +++ b/wrappers/fedimint-go/pkg/fedimint/types/modules/mint.go @@ -45,49 +45,74 @@ type OOBNotes struct { type SpendableNote struct { Signature Signature `json:"signature"` - SpendKey KeyPair `json:"spend_key"` + SpendKey KeyPair `json:"spendKey"` } -// @> `ReissueRequest` notes should be string? as fedimint-ts does uses string. -type ReissueRequest struct { - Notes OOBNotes `json:"notes"` +type NotesJson struct { + FederationIdPrefix string `json:"federation_id_prefix"` + Notes map[string][]SignatureSpendKeyPair `json:"notes"` } -type ReissueResponse struct { - AmountMsat uint64 `json:"amount_msat"` +type SignatureSpendKeyPair struct { + Signature string `json:"signature"` + SpendKey string `json:"spend_key"` } -type SpendRequest struct { - AmountMsat uint64 `json:"amount_msat"` - AllowOverpay bool `json:"allow_overpay"` - Timeout uint64 `json:"timeout"` +type DecodeNotesRequest struct { + Notes string `json:"notes"` } -type SpendResponse struct { - Operation string `json:"operation"` - Notes OOBNotes `json:"notes"` +type DecodeNotesResponse struct { + NotesJson string `json:"notesJson"` } -type ValidateRequest struct { - Notes OOBNotes `json:"notes"` +type EncodeNotesRequest struct { + NotesJsonStr string `json:"notesJsonStr"` } -type ValidateResponse struct { - AmountMsat uint64 `json:"amount_msat"` +type EncodeNotesResponse struct { + Notes string `json:"notes"` +} +type MintReissueRequest struct { + Notes string `json:"notes"` +} + +type MintReissueResponse struct { + AmountMsat uint64 `json:"amountMsat"` +} + +type MintSpendRequest struct { + AmountMsat uint64 `json:"amountMsat"` + AllowOverpay bool `json:"allowOverpay"` + Timeout uint64 `json:"timeout"` + IncludeInvite bool `json:"includeInvite"` +} + +type MintSpendResponse struct { + OperationId string `json:"operationId"` + Notes OOBNotes `json:"notes"` +} + +type MintValidateRequest struct { + Notes string `json:"notes"` +} + +type MintValidateResponse struct { + AmountMsat uint64 `json:"amountMsat"` } -type SplitRequest struct { - Notes OOBNotes `json:"notes"` +type MintSplitRequest struct { + Notes string `json:"notes"` } -type SplitResponse struct { - Notes map[uint64]OOBNotes `json:"notes"` +type MintSplitResponse struct { + Notes map[uint64]string `json:"notes"` } -type CombineRequest struct { - Notes []OOBNotes `json:"notes"` +type MintCombineRequest struct { + NotesVec []string `json:"notesVec"` } -type CombineResponse struct { - Notes OOBNotes `json:"notes"` +type MintCombineResponse struct { + Notes string `json:"notes"` } diff --git a/wrappers/fedimint-go/pkg/fedimint/types/modules/wallet.go b/wrappers/fedimint-go/pkg/fedimint/types/modules/onchain.go similarity index 58% rename from wrappers/fedimint-go/pkg/fedimint/types/modules/wallet.go rename to wrappers/fedimint-go/pkg/fedimint/types/modules/onchain.go index 9f48c15..6c64512 100644 --- a/wrappers/fedimint-go/pkg/fedimint/types/modules/wallet.go +++ b/wrappers/fedimint-go/pkg/fedimint/types/modules/onchain.go @@ -1,28 +1,28 @@ package modules -type DepositAddressRequest struct { +type OnchainDepositAddressRequest struct { Timeout int `json:"timeout"` } -type DepositAddressResponse struct { +type OnchainDepositAddressResponse struct { OperationId string `json:"operationId"` Address string `json:"address"` } -type AwaitDepositRequest struct { +type OnchainAwaitDepositRequest struct { OperationId string `json:"operationId"` } -type AwaitDepositResponse struct { +type OnchainAwaitDepositResponse struct { Status string `json:"status"` } -type WithdrawRequest struct { +type OnchainWithdrawRequest struct { Address string `json:"address"` AmountSat int `json:"amountSat"` } -type WithdrawResponse struct { +type OnchainWithdrawResponse struct { Txid string `json:"txid"` FeesSat int `json:"feesSat"` }