Skip to content

Commit

Permalink
Merge pull request #21 from yemmyharry/go-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow authored Apr 15, 2024
2 parents ac8c2cb + 0db059c commit c6f8d9b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ fm_client_db
.vscode
.DS_Store
fm_db

20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions flake.lock

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

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
export RUSTDOCFLAGS="--cfg tokio_unstable"
export RUST_LOG="info"
'';

};
});
}
1 change: 1 addition & 0 deletions wrappers/fedimint-go/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.idea
47 changes: 40 additions & 7 deletions wrappers/fedimint-go/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func main() {

jsonBytes, err = json.Marshal(infoDataResponse)
if err != nil {
fmt.Println("Error marshaling JSON(discover-version):", err)
fmt.Println("Error marshaling JSON(info):", err)
return
}
var infoResponseData interface{}
err = json.Unmarshal(jsonBytes, &infoResponseData)
if err != nil {
fmt.Println("Error unmarshalling JSON(discover-version):", err)
fmt.Println("Error unmarshalling JSON(info):", err)
return
}

Expand Down Expand Up @@ -168,7 +168,7 @@ func main() {
logMethod("/v2/admin/list-operations")
listOperationsData, err := fc.ListOperations(10, nil)
if err != nil {
fmt.Println("Error calling JOIN: ", err)
fmt.Println("Error calling LIST OPERATIONS: ", err)
return
}

Expand Down Expand Up @@ -214,7 +214,7 @@ func main() {

// `/v2/ln/invoice`
logMethod("/v2/ln/invoice")
invoiceData, err := fc.Ln.CreateInvoice(10000, "test", nil, nil, nil)
invoiceData, err := fc.Ln.CreateInvoice(10000, "test_INVOICE", nil, fc.GetActiveGatewayId(), nil)
if err != nil {
fmt.Println("Error calling INVOICE: ", err)
return
Expand All @@ -236,6 +236,9 @@ func main() {

// `/v2/ln/pay`
logMethod("/v2/ln/pay")
if invoiceData == nil {
fmt.Println("invoice data is empty")
}
payData, err := fc.Ln.Pay(invoiceData.Invoice, fc.GetActiveGatewayId(), nil, nil, nil)
if err != nil {
fmt.Println("Error calling PAY: ", err)
Expand All @@ -258,6 +261,9 @@ func main() {

// /v2/ln/await-invoice
logMethod("/v2/ln/await-invoice")
if invoiceData == nil {
fmt.Println("invoice data is empty")
}
awaitInvoiceData, err := fc.Ln.AwaitInvoice(invoiceData.OperationId, fc.GetActiveGatewayId(), nil)
if err != nil {
fmt.Println("Error calling AWAIT_INVOICE: ", err)
Expand Down Expand Up @@ -305,7 +311,7 @@ func main() {

// `/v1/ln/claim-external-pubkey-tweaked`
logMethod("/v1/ln/claim-external-pubkey-tweaked")
claimInvoice, err := fc.Ln.ClaimPubkeyTweakReceive(keyPair.PrivateKey, []uint64{1}, fc.GetActiveFederationId(), fc.GetActiveFederationId())
claimInvoice, err := fc.Ln.ClaimPubkeyTweakReceive(keyPair.PrivateKey, []uint64{1}, fc.GetActiveGatewayId(), fc.GetActiveFederationId())
if err != nil {
fmt.Println("Error calling CLAIM_PUBKEY_RECEIVE_TWEAKED: ", err)
return
Expand Down Expand Up @@ -353,6 +359,10 @@ func main() {

// `/v2/mint/decode-notes`
logMethod("/v2/mint/decode-notes")
if mintData == nil {
fmt.Println("mintData is nil.")
return
}
decodedData, err := fc.Mint.DecodeNotes(mintData.Notes)
if err != nil {
fmt.Println("Error calling DECODE_NOTES: ", err)
Expand All @@ -375,6 +385,10 @@ func main() {

// `/v2/mint/encode-notes`
logMethod("/v2/mint/encode-notes")
if decodedData == nil {
fmt.Println("decodedData is nil.")
return
}
encodedData, err := fc.Mint.EncodeNotes(decodedData.NotesJson)
if err != nil {
fmt.Println("Error calling DECODE_NOTES: ", err)
Expand All @@ -397,6 +411,11 @@ func main() {

// `/v2/mint/validate`
logMethod("/v2/mint/validate")
if mintData == nil {
fmt.Println("mintData is nil.")
return
}

validateData, err := fc.Mint.Validate(mintData.Notes, nil)
if err != nil {
fmt.Println("Error calling VALIDATE: ", err)
Expand All @@ -419,6 +438,11 @@ func main() {

// `/v2/mint/reissue`
logMethod("/v2/mint/reissue")
if mintData == nil {
fmt.Println("mintData is nil.")
return
}

reissueData, err := fc.Mint.Reissue(mintData.Notes, nil)
if err != nil {
fmt.Println("Error calling REISSUE: ", err)
Expand All @@ -441,6 +465,11 @@ func main() {

// `/v2/mint/split`
logMethod("/v2/mint/split")
if mintData == nil {
fmt.Println("mintData is nil.")
return
}

splitData, err := fc.Mint.Split(mintData.Notes)
if err != nil {
fmt.Println("Error calling SPLIT: ", err)
Expand All @@ -464,6 +493,10 @@ func main() {
// `/v2/mint/combine`
logMethod("/v2/mint/combine")
notesVec := func() []string {
if splitData == nil || splitData.Notes == nil {
fmt.Println("splitData or splitData.Notes is nil")
return nil
}
result := make([]string, 0, len(splitData.Notes))
for _, value := range splitData.Notes {
result = append(result, value)
Expand All @@ -478,13 +511,13 @@ func main() {

jsonBytes, err = json.Marshal(combineData)
if err != nil {
fmt.Println("Error marshaling JSON(split):", err)
fmt.Println("Error marshaling JSON(combine-data):", err)
return
}
var combineResponseData interface{}
err = json.Unmarshal(jsonBytes, &combineResponseData)
if err != nil {
fmt.Println("Error unmarshalling JSON(split):", err)
fmt.Println("Error unmarshalling JSON(combine-data):", err)
return
}

Expand Down
15 changes: 11 additions & 4 deletions wrappers/fedimint-go/pkg/fedimint/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (fc *FedimintClient) post(endpoint string, body interface{}) ([]byte, error
return fc.fetchWithAuth(endpoint, "POST", jsonBody)
}


// postWithFederationId takes any request object, marshals it to JSON, optionally adds a federationId, and makes a POST request.
func (fc *FedimintClient) postWithFederationId(endpoint string, requestBody interface{}, federationId *string) ([]byte, error) {
// Initialize an empty map for the request body.
Expand All @@ -143,9 +144,13 @@ func (fc *FedimintClient) postWithFederationId(endpoint string, requestBody inte
// Determine the effective federationId to use
effectiveFederationId := fc.ActiveFederationId
fmt.Printf("effectiveFederationId: %s\n", effectiveFederationId)

if federationId != nil {
effectiveFederationId = *federationId
bodyMap["federationId"] = *federationId
} else {
bodyMap["federationId"] = fc.ActiveFederationId
}

fmt.Printf("effectiveFederationId: %s\n", effectiveFederationId)

// Add federationId to the map, which is now guaranteed to be initialized.
Expand All @@ -157,6 +162,7 @@ func (fc *FedimintClient) postWithFederationId(endpoint string, requestBody inte
return nil, fmt.Errorf("failed to marshal modified request map: %w", err)
}


fmt.Printf("modifiedRequestJSON: %s\n", modifiedRequestJSON)

// Proceed to make the POST request with the modified JSON body.
Expand Down Expand Up @@ -313,7 +319,7 @@ func (fc *FedimintClient) Join(inviteCode string, setActiveFederationId bool, us

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)
resp, err := onchain.Client.postWithFederationId("/wallet/deposit-address", request, federationId)
if err != nil {
return nil, err
}
Expand All @@ -327,7 +333,7 @@ func (onchain *OnchainModule) CreateDepositAddress(timeout int, federationId *st

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)
resp, err := onchain.Client.postWithFederationId("/wallet/await-deposit", request, federationId)
if err != nil {
return nil, err
}
Expand All @@ -341,7 +347,7 @@ func (onchain *OnchainModule) AwaitDeposit(operationId string, federationId *str

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)
resp, err := onchain.Client.postWithFederationId("/wallet/withdraw", request, federationId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -541,6 +547,7 @@ func (ln *LnModule) ClaimPubkeyReceive(privateKey string, gatewayId string, fede
return &infoResp, nil
}


func (ln *LnModule) ClaimPubkeyTweakReceive(privateKey string, tweaks []uint64, gatewayId string, federationId string) (*types.InfoResponse, error) {
request := modules.LnClaimPubkeyTweakedRequest{PrivateKey: privateKey, Tweaks: tweaks}
resp, err := ln.Client.postWithGatewayIdAndFederationId("/ln/claim-external-receive-tweaked", request, &gatewayId, &federationId)
Expand Down

0 comments on commit c6f8d9b

Please sign in to comment.