Skip to content

Commit d86570f

Browse files
committed
refactor: simplify mpp-go public API and tooling
1 parent b8fc4bb commit d86570f

26 files changed

Lines changed: 254 additions & 246 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
*.out
3+
cover.html

Makefile

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,61 @@
1-
.PHONY: test integration
1+
.PHONY: all build_examples clean test test-coverage check fix integration docs help
22

3+
# Default target
4+
all: check
5+
6+
# Builds all runnable examples
7+
build_examples:
8+
@mkdir -p bin
9+
go build -o bin/charge-basic ./examples/charge-basic
10+
go build -o bin/charge-hash ./examples/charge-hash
11+
go build -o bin/charge-fee-payer ./examples/charge-fee-payer
12+
13+
# Cleans generated artifacts
14+
clean:
15+
go clean
16+
rm -rf bin/
17+
rm -f cover.out cover.html coverage.out
18+
19+
# Run unit tests only (integration tests use a build tag and are excluded)
320
test:
421
go test ./...
522

23+
# Run unit tests with coverage
24+
test-coverage:
25+
go test -coverprofile=coverage.out ./...
26+
go tool cover -html=coverage.out -o cover.html
27+
28+
# Run formatting, vet, module verification, and unit tests
29+
check:
30+
go mod verify
31+
test -z "$$(gofmt -l .)" || (echo "Code needs formatting. Run 'make fix'" && gofmt -l . && exit 1)
32+
go vet ./...
33+
go test ./...
34+
35+
# Format code and tidy dependencies
36+
fix:
37+
gofmt -s -w .
38+
go mod tidy
39+
40+
# Run integration tests against the local Tempo devnet by default
641
integration:
742
@TEMPO_RPC_URL=$${TEMPO_RPC_URL:-http://localhost:8545} go test -tags=integration -run TestIntegration -timeout=5m ./tests
43+
44+
# Start a local godoc server for this module
45+
docs:
46+
which godoc > /dev/null || (echo "Installing godoc..." && go install golang.org/x/tools/cmd/godoc@latest)
47+
echo "Documentation available at http://localhost:6060/pkg/github.com/tempoxyz/mpp-go/"
48+
echo "Press Ctrl+C to stop the server"
49+
@godoc -http=:6060
50+
51+
# Show available targets
52+
help:
53+
@echo "Available targets:"
54+
@echo " build_examples Build the example binaries into ./bin"
55+
@echo " clean Remove build artifacts and coverage files"
56+
@echo " test Run unit tests"
57+
@echo " test-coverage Run unit tests with coverage report"
58+
@echo " check Run formatting checks, vet, and tests"
59+
@echo " fix Format code and tidy modules"
60+
@echo " integration Run integration tests against TEMPO_RPC_URL"
61+
@echo " docs Start a local godoc server"

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ Go SDK for the [**Machine Payments Protocol**](https://mpp.dev)
2222
[![License](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue)](LICENSE)
2323

2424
[MPP](https://mpp.dev) lets any client — agents, apps, or humans — pay for any service in the same HTTP request. It standardizes [HTTP 402](https://mpp.dev/protocol/http-402) with an open [IETF specification](https://paymentauth.org), so servers can charge and clients can pay without API keys, billing accounts, or checkout flows.
25+
## Documentation
2526

2627
You can get started today by reading the [Go SDK docs](https://mpp.dev/sdk/go), exploring the [protocol overview](https://mpp.dev/protocol/), or jumping straight to the [quickstart](https://mpp.dev/quickstart/).
2728

29+
Package docs:
30+
31+
- [`pkg/client`](https://pkg.go.dev/github.com/tempoxyz/mpp-go/pkg/client)
32+
- [`pkg/server`](https://pkg.go.dev/github.com/tempoxyz/mpp-go/pkg/server)
33+
- [`pkg/tempo`](https://pkg.go.dev/github.com/tempoxyz/mpp-go/pkg/tempo)
34+
- [`pkg/tempo/client`](https://pkg.go.dev/github.com/tempoxyz/mpp-go/pkg/tempo/client)
35+
- [`pkg/tempo/server`](https://pkg.go.dev/github.com/tempoxyz/mpp-go/pkg/tempo/server)
36+
2837
## Install
2938

3039
```bash
@@ -42,13 +51,13 @@ import (
4251
"encoding/json"
4352
"net/http"
4453

45-
mppserver "github.com/tempoxyz/mpp-go/pkg/server"
54+
"github.com/tempoxyz/mpp-go/pkg/server"
4655
"github.com/tempoxyz/mpp-go/pkg/tempo"
4756
charge "github.com/tempoxyz/mpp-go/pkg/tempo/server"
4857
)
4958

5059
func main() {
51-
intent, _ := charge.NewChargeIntent(charge.ChargeIntentConfig{
60+
intent, _ := charge.NewIntent(charge.IntentConfig{
5261
RPCURL: "https://rpc.moderato.tempo.xyz",
5362
})
5463

@@ -59,15 +68,15 @@ func main() {
5968
Recipient: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
6069
})
6170

62-
payment := mppserver.New(method, "api.example.com", "replace-me")
71+
payment := server.New(method, "api.example.com", "replace-me")
6372

64-
handler := mppserver.ChargeMiddleware(payment, mppserver.ChargeParams{
73+
handler := server.ChargeMiddleware(payment, server.ChargeParams{
6574
Amount: "0.50",
6675
Description: "Paid content",
6776
})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6877
_ = json.NewEncoder(w).Encode(map[string]any{
6978
"data": "paid content",
70-
"payer": mppserver.CredentialFromContext(r.Context()).Source,
79+
"payer": server.CredentialFromContext(r.Context()).Source,
7180
})
7281
}))
7382

@@ -85,7 +94,7 @@ import (
8594
"encoding/json"
8695
"fmt"
8796

88-
mppclient "github.com/tempoxyz/mpp-go/pkg/client"
97+
"github.com/tempoxyz/mpp-go/pkg/client"
8998
"github.com/tempoxyz/mpp-go/pkg/mpp"
9099
charge "github.com/tempoxyz/mpp-go/pkg/tempo/client"
91100
)
@@ -97,8 +106,8 @@ func main() {
97106
RPCURL: "https://rpc.moderato.tempo.xyz",
98107
})
99108

100-
client := mppclient.New([]mppclient.Method{method})
101-
response, err := client.Get(context.Background(), "https://api.example.com/paid")
109+
paymentClient := client.New([]client.Method{method})
110+
response, err := paymentClient.Get(context.Background(), "https://api.example.com/paid")
102111
if err != nil {
103112
panic(err)
104113
}

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Start the local Tempo devnet before running them:
1212
docker compose up -d
1313
```
1414

15-
The examples use `mppclient` and `mppserver` for the generic HTTP 402 packages.
16-
Each file aliases the Tempo charge package to `charge`, since it only uses one
15+
The examples import the generic HTTP 402 packages as `client` and `server`.
16+
Each file aliases the Tempo package it uses to `charge`, since it only uses one
1717
side of the Tempo flow at a time.
1818

1919
Run any example directly from the repo root:

examples/charge-basic/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66

77
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
8-
mppclient "github.com/tempoxyz/mpp-go/pkg/client"
8+
"github.com/tempoxyz/mpp-go/pkg/client"
99
"github.com/tempoxyz/mpp-go/pkg/mpp"
1010
charge "github.com/tempoxyz/mpp-go/pkg/tempo/client"
1111
)
@@ -25,9 +25,9 @@ func runClient(ctx context.Context, url, rpcURL string, chainID int64) (*clientR
2525
if err != nil {
2626
return nil, err
2727
}
28-
client := mppclient.New([]mppclient.Method{method})
28+
paymentClient := client.New([]client.Method{method})
2929

30-
response, err := client.Get(ctx, url+"/paid")
30+
response, err := paymentClient.Get(ctx, url+"/paid")
3131
if err != nil {
3232
return nil, err
3333
}

examples/charge-basic/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http/httptest"
77

88
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
9-
mppserver "github.com/tempoxyz/mpp-go/pkg/server"
9+
"github.com/tempoxyz/mpp-go/pkg/server"
1010
charge "github.com/tempoxyz/mpp-go/pkg/tempo/server"
1111
)
1212

@@ -16,7 +16,7 @@ type exampleServer struct {
1616
}
1717

1818
func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
19-
intent, err := charge.NewChargeIntent(charge.ChargeIntentConfig{RPCURL: rpcURL})
19+
intent, err := charge.NewIntent(charge.IntentConfig{RPCURL: rpcURL})
2020
if err != nil {
2121
return nil, err
2222
}
@@ -26,13 +26,13 @@ func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
2626
Currency: devnet.Currency,
2727
Recipient: devnet.Recipient,
2828
})
29-
payment := mppserver.New(method, devnet.Realm, "example-secret")
29+
payment := server.New(method, devnet.Realm, "example-secret")
3030

31-
handler := mppserver.ChargeMiddleware(payment, mppserver.ChargeParams{
31+
handler := server.ChargeMiddleware(payment, server.ChargeParams{
3232
Amount: "0.50",
3333
Description: "Basic Tempo charge example",
3434
})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35-
credential := mppserver.CredentialFromContext(r.Context())
35+
credential := server.CredentialFromContext(r.Context())
3636
_ = json.NewEncoder(w).Encode(map[string]any{
3737
"data": "paid content",
3838
"payer": credential.Source,

examples/charge-fee-payer/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66

77
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
8-
mppclient "github.com/tempoxyz/mpp-go/pkg/client"
8+
"github.com/tempoxyz/mpp-go/pkg/client"
99
"github.com/tempoxyz/mpp-go/pkg/mpp"
1010
charge "github.com/tempoxyz/mpp-go/pkg/tempo/client"
1111
)
@@ -24,9 +24,9 @@ func runClient(ctx context.Context, url, rpcURL string, chainID int64) (*clientR
2424
if err != nil {
2525
return nil, err
2626
}
27-
client := mppclient.New([]mppclient.Method{method})
27+
paymentClient := client.New([]client.Method{method})
2828

29-
response, err := client.Get(ctx, url+"/paid")
29+
response, err := paymentClient.Get(ctx, url+"/paid")
3030
if err != nil {
3131
return nil, err
3232
}

examples/charge-fee-payer/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http/httptest"
77

88
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
9-
mppserver "github.com/tempoxyz/mpp-go/pkg/server"
9+
"github.com/tempoxyz/mpp-go/pkg/server"
1010
charge "github.com/tempoxyz/mpp-go/pkg/tempo/server"
1111
)
1212

@@ -16,7 +16,7 @@ type exampleServer struct {
1616
}
1717

1818
func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
19-
intent, err := charge.NewChargeIntent(charge.ChargeIntentConfig{
19+
intent, err := charge.NewIntent(charge.IntentConfig{
2020
FeePayerPrivateKey: devnet.FeePayerPrivateKey,
2121
RPCURL: rpcURL,
2222
})
@@ -30,15 +30,15 @@ func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
3030
FeePayer: true,
3131
Recipient: devnet.Recipient,
3232
})
33-
payment := mppserver.New(method, devnet.Realm, "example-secret")
33+
payment := server.New(method, devnet.Realm, "example-secret")
3434

35-
handler := mppserver.ChargeMiddleware(payment, mppserver.ChargeParams{
35+
handler := server.ChargeMiddleware(payment, server.ChargeParams{
3636
Amount: "0.50",
3737
Description: "Fee-payer Tempo charge example",
3838
FeePayer: true,
3939
})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4040
_ = json.NewEncoder(w).Encode(map[string]any{
41-
"tx": mppserver.ReceiptFromContext(r.Context()).Reference,
41+
"tx": server.ReceiptFromContext(r.Context()).Reference,
4242
})
4343
}))
4444

examples/charge-hash/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66

77
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
8-
mppclient "github.com/tempoxyz/mpp-go/pkg/client"
8+
"github.com/tempoxyz/mpp-go/pkg/client"
99
"github.com/tempoxyz/mpp-go/pkg/mpp"
1010
"github.com/tempoxyz/mpp-go/pkg/tempo"
1111
charge "github.com/tempoxyz/mpp-go/pkg/tempo/client"
@@ -26,9 +26,9 @@ func runClient(ctx context.Context, url, rpcURL string, chainID int64) (*clientR
2626
if err != nil {
2727
return nil, err
2828
}
29-
client := mppclient.New([]mppclient.Method{method})
29+
paymentClient := client.New([]client.Method{method})
3030

31-
response, err := client.Get(ctx, url+"/paid")
31+
response, err := paymentClient.Get(ctx, url+"/paid")
3232
if err != nil {
3333
return nil, err
3434
}

examples/charge-hash/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http/httptest"
77

88
"github.com/tempoxyz/mpp-go/examples/internal/devnet"
9-
mppserver "github.com/tempoxyz/mpp-go/pkg/server"
9+
"github.com/tempoxyz/mpp-go/pkg/server"
1010
"github.com/tempoxyz/mpp-go/pkg/tempo"
1111
charge "github.com/tempoxyz/mpp-go/pkg/tempo/server"
1212
)
@@ -17,7 +17,7 @@ type exampleServer struct {
1717
}
1818

1919
func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
20-
intent, err := charge.NewChargeIntent(charge.ChargeIntentConfig{RPCURL: rpcURL})
20+
intent, err := charge.NewIntent(charge.IntentConfig{RPCURL: rpcURL})
2121
if err != nil {
2222
return nil, err
2323
}
@@ -28,15 +28,15 @@ func startServer(rpcURL string, chainID int64) (*exampleServer, error) {
2828
Recipient: devnet.Recipient,
2929
SupportedModes: []tempo.ChargeMode{tempo.ChargeModePush},
3030
})
31-
payment := mppserver.New(method, devnet.Realm, "example-secret")
31+
payment := server.New(method, devnet.Realm, "example-secret")
3232

33-
handler := mppserver.ChargeMiddleware(payment, mppserver.ChargeParams{
33+
handler := server.ChargeMiddleware(payment, server.ChargeParams{
3434
Amount: "0.50",
3535
Description: "Hash credential Tempo charge example",
3636
SupportedModes: []tempo.ChargeMode{tempo.ChargeModePush},
3737
})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3838
_ = json.NewEncoder(w).Encode(map[string]any{
39-
"tx": mppserver.ReceiptFromContext(r.Context()).Reference,
39+
"tx": server.ReceiptFromContext(r.Context()).Reference,
4040
})
4141
}))
4242

0 commit comments

Comments
 (0)