81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package plugin
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
json "github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/vincenzopalazzo/cln4go/plugin"
|
|
)
|
|
|
|
func OceanPay(cln *plugin.Plugin[*State], request map[string]any) (map[string]any, error) {
|
|
cln.Log("debug", fmt.Sprintf("ocean-pay: %s", request))
|
|
clnRequest := struct {
|
|
Invstr string `mapstructure:"invstr"`
|
|
Amount_msat any `mapstructure:"amount_msat"`
|
|
}{}
|
|
if err := json.Decode(request, &clnRequest); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cln.Log("debug", fmt.Sprintf("decode the string %s", clnRequest.Invstr))
|
|
invstrDecode, err := cln.State.Rpc("decode", map[string]any{
|
|
"string": clnRequest.Invstr,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
clnDecode := struct {
|
|
InvType string `mapstructure:"type"`
|
|
Valid bool `mapstructure:"valid"`
|
|
}{}
|
|
|
|
if err := json.Decode(invstrDecode, &clnDecode); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !clnDecode.Valid {
|
|
return nil, fmt.Errorf("invoice not valid")
|
|
}
|
|
|
|
cln.Log("debug", fmt.Sprintf("decode offer: %s", invstrDecode))
|
|
var payInvoice map[string]any
|
|
var invoice string
|
|
switch clnDecode.InvType {
|
|
case "bolt12 offer", "bolt12 invoice_request", "bolt12 invoice":
|
|
payInvoice, err = FetchOffer(cln, map[string]any{
|
|
"offer": clnRequest.Invstr,
|
|
"amount_msat": clnRequest.Amount_msat,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
invoice = payInvoice["bolt11"].(string)
|
|
default:
|
|
return nil, fmt.Errorf("Invoice String %s not supported: %s", clnDecode.InvType, clnRequest.Invstr)
|
|
}
|
|
|
|
cln.Log("debug", fmt.Sprintf("paying the offer %s", payInvoice))
|
|
payResponse, err := cln.State.Rpc("pay", payInvoice)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
invoiceDecode, err := cln.State.Rpc("decode", map[string]any{
|
|
"string": invoice,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := map[string]any{
|
|
"offer_invoice": clnRequest.Invstr,
|
|
"offer_decode": invstrDecode,
|
|
"invoice": invoice,
|
|
"invoice_decode": invoiceDecode,
|
|
"pay": payResponse,
|
|
}
|
|
return response, nil
|
|
}
|