Skip to content

Commit

Permalink
feat: continue fedimint-go 0.3 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 24, 2024
1 parent 52c1dd9 commit 0119256
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 95 deletions.
3 changes: 3 additions & 0 deletions JUSTFILE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ mprocs:
test-ts:
bun run wrappers/fedimint-ts/test.ts

test-go:
cd wrappers/fedimint-go && go run cmd/main.go

alias b := build
alias c := check
alias t := test
Expand Down
139 changes: 114 additions & 25 deletions wrappers/fedimint-go/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,158 @@
package main

import (
"encoding/hex"
"fedimint-go-client/pkg/fedimint"
"fedimint-go-client/pkg/fedimint/types/modules"
"fmt"
"os"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/joho/godotenv"
)

func main() {
err := godotenv.Load()
func logMethod(method string) {
fmt.Println("--------------------")
fmt.Printf("Method: %s\n", method)
}

func logInputAndOutput(input interface{}, output interface{}) {
fmt.Println("Input: ", input)
fmt.Println("Output: ", output)
fmt.Println("--------------------")
}

type KeyPair struct {
PrivateKey string
PublicKey string
}

func newKeyPair() KeyPair {
privateKey, err := btcec.NewPrivateKey()
if err != nil {
panic("failed to generate a private key")
}

pubKey := privateKey.PubKey()
return KeyPair{
PrivateKey: hex.EncodeToString(privateKey.Serialize()),
PublicKey: hex.EncodeToString(pubKey.SerializeCompressed()),
}
}

func buildTestClient() *fedimint.FedimintClient {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}

baseUrl := os.Getenv("BASE_URL")
baseUrl := os.Getenv("FEDIMINT_CLIENTD_BASE_URL")
if baseUrl == "" {
baseUrl = "http://localhost:5000"
baseUrl = "http://127.0.0.1:3333"
}

password := os.Getenv("PASSWORD")
password := os.Getenv("FEDIMINT_CLIENTD_PASSWORD")
if password == "" {
password = "password"
}

federationId := os.Getenv("FEDERATION_ID")
federationId := os.Getenv("FEDIMINT_CLIENTD_ACTIVE_FEDERATION_ID")
if federationId == "" {
federationId = "defaultId"
federationId = "15db8cb4f1ec8e484d73b889372bec94812580f929e8148b7437d359af422cd3"
}

fedimintClient := fedimint.NewFedimintClient(baseUrl, password, federationId)
return fedimint.NewFedimintClient(baseUrl, password, federationId)
}

info, err := fedimintClient.Info()
func main() {
client := buildTestClient()
keyPair := newKeyPair()
fmt.Println("Generated key pair: ", keyPair)

// ADMIN METHODS
// `/v2/admin/config`
logMethod("/v2/admin/config")
data, err := client.Config()
if err != nil {
fmt.Println("Error getting info: ", err)
fmt.Println("Error calling config: ", err)
return
}
fmt.Println("Current Total Msats Ecash: ", info.TotalAmountMsat)
logInputAndOutput(nil, data)

invoiceRequest := modules.LnInvoiceRequest{
AmountMsat: 10000,
Description: "test",
// `/v2/admin/discover-version`
logMethod("/v2/admin/discover-version")
data, err = client.DiscoverVersion(1)
if err != nil {
fmt.Println("Error calling discoverVersion: ", err)
return
}
logInputAndOutput(nil, data)

invoiceResponse, err := fedimintClient.Ln.CreateInvoice(invoiceRequest, &federationId)
// `/v2/admin/federation-ids`
logMethod("/v2/admin/federation-ids")
federationIds, err := client.FederationIds()
if err != nil {
fmt.Println("Error creating invoice: ", err)
fmt.Println("Error calling federationIds: ", err)
return
}
logInputAndOutput(nil, federationIds)

fmt.Println("Created 10 sat Invoice: ", invoiceResponse.Invoice)
// `/v2/admin/info`
logMethod("/v2/admin/info")
infoData, err := client.Info()
if err != nil {
fmt.Println("Error calling info: ", err)
return
}
logInputAndOutput(nil, infoData)

fmt.Println("Waiting for payment...")
// `/v2/admin/join`
inviteCode := os.Getenv("FEDIMINT_CLIENTD_INVITE_CODE")
if inviteCode == "" {
inviteCode = "fed11qgqrgvnhwden5te0v9k8q6rp9ekh2arfdeukuet595cr2ttpd3jhq6rzve6zuer9wchxvetyd938gcewvdhk6tcqqysptkuvknc7erjgf4em3zfh90kffqf9srujn6q53d6r056e4apze5cw27h75"
}
logMethod("/v2/admin/join")
joinData, err := client.Join(inviteCode, true, true, false)
if err != nil {
fmt.Println("Error calling join: ", err)
return
}
logInputAndOutput(map[string]interface{}{"inviteCode": inviteCode}, joinData)

awaitInvoiceRequest := modules.LnAwaitInvoiceRequest{
OperationID: invoiceResponse.OperationID,
// `/v2/admin/list-operations`
logMethod("/v2/admin/list-operations")
listOperationsData, err := client.ListOperations(10, nil)
if err != nil {
fmt.Println("Error calling listOperations: ", err)
return
}
logInputAndOutput(map[string]interface{}{"limit": 10}, listOperationsData)

// LIGHTNING METHODS
// `/v2/ln/list-gateways`
logMethod("/v2/ln/list-gateways")
listGatewaysData, err := client.Ln.ListGateways()
if err != nil {
fmt.Println("Error calling listGateways: ", err)
return
}
logInputAndOutput(nil, listGatewaysData)

// `/v2/ln/invoice`
logMethod("/v2/ln/invoice")
invoiceData, err := client.Ln.CreateInvoice(10000, "test")
if err != nil {
fmt.Println("Error calling createInvoice: ", err)
return
}
logInputAndOutput(map[string]interface{}{"amountMsat": 10000, "description": "test"}, invoiceData)

_, err = fedimintClient.Ln.AwaitInvoice(awaitInvoiceRequest, &federationId)
// `/v2/ln/pay`
logMethod("/v2/ln/pay")
payData, err := client.Ln.Pay(invoiceData.Invoice, nil)
if err != nil {
fmt.Println("Error awaiting invoice: ", err)
fmt.Println("Error calling pay: ", err)
return
}
logInputAndOutput(map[string]interface{}{"paymentInfo": invoiceData.Invoice}, payData)

fmt.Println("Payment Received!")
// fmt.Println("New Total Msats Ecash: ", awaitInvoiceResponse.TotalAmountMsat)
}
6 changes: 5 additions & 1 deletion wrappers/fedimint-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module fedimint-go-client

go 1.21.3

require github.com/joho/godotenv v1.5.1 // indirect
require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
4 changes: 4 additions & 0 deletions wrappers/fedimint-go/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
Loading

0 comments on commit 0119256

Please sign in to comment.