Skip to content

Commit

Permalink
refactor payment-rails signature generation (#904)
Browse files Browse the repository at this point in the history
* refactor payment-rails signature generation
  • Loading branch information
volodymyr-basiuk authored Jan 23, 2025
1 parent bc98cc4 commit e82c5e6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 71 deletions.
6 changes: 5 additions & 1 deletion cmd/platform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ func main() {
displayMethodService := services.NewDisplayMethod(repositories.NewDisplayMethod(*storage))
schemaService := services.NewSchema(schemaRepository, schemaLoader, displayMethodService)
linkService := services.NewLinkService(storage, claimsService, qrService, claimsRepository, linkRepository, schemaRepository, schemaLoader, sessionRepository, ps, identityService, *networkResolver, cfg.UniversalLinks)
paymentService := services.NewPaymentService(paymentsRepo, *networkResolver, schemaService, paymentSettings, keyStore)
paymentService, err := services.NewPaymentService(paymentsRepo, *networkResolver, schemaService, paymentSettings, keyStore)
if err != nil {
log.Error(ctx, "error creating payment service", "err", err)
return
}
keyService := services.NewKey(keyStore, claimsService, keyRepository)
transactionService, err := gateways.NewTransaction(*networkResolver)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func newTestServer(t *testing.T, st *db.Storage) *testServer {
connectionService := services.NewConnection(repos.connection, repos.claims, st)
displayMethodService := services.NewDisplayMethod(repos.displayMethod)
schemaService := services.NewSchema(repos.schemas, schemaLoader, displayMethodService)
paymentService := services.NewPaymentService(repos.payments, *networkResolver, schemaService, paymentSettings, keyStore)

paymentService, err := services.NewPaymentService(repos.payments, *networkResolver, schemaService, paymentSettings, keyStore)
require.NoError(t, err)
mediaTypeManager := services.NewMediaTypeManager(
map[iden3comm.ProtocolMessage][]string{
protocol.CredentialFetchRequestMessageType: {string(packers.MediaTypeZKPMessage)},
Expand Down
3 changes: 3 additions & 0 deletions internal/core/domain/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (

AuthBJJCredentialJSONLDContext = "https://schema.iden3.io/core/jsonld/auth.jsonld"
AuthBJJCredentialTypeID = "https://schema.iden3.io/core/jsonld/auth.jsonld#AuthBJJCredential"

Iden3PaymentRailsRequestV1SchemaJSON = `{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Iden3PaymentRailsRequestV1":[{"name":"recipient","type":"address"},{"name":"amount","type":"uint256"},{"name":"expirationDate","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"metadata","type":"bytes"}]}`
Iden3PaymentRailsERC20RequestV1SchemaJSON = `{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Iden3PaymentRailsERC20RequestV1":[{"name":"tokenAddress","type":"address"},{"name":"recipient","type":"address"},{"name":"amount","type":"uint256"},{"name":"expirationDate","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"metadata","type":"bytes"}]}`
)

// SchemaFormat type
Expand Down
109 changes: 41 additions & 68 deletions internal/core/services/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
b64 "encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strconv"
Expand Down Expand Up @@ -33,22 +34,38 @@ import (
)

type payment struct {
networkResolver network.Resolver
settings payments.Config
schemaService ports.SchemaService
paymentsStore ports.PaymentRepository
kms kms.KMSType
networkResolver network.Resolver
settings payments.Config
schemaService ports.SchemaService
paymentsStore ports.PaymentRepository
kms kms.KMSType
iden3PaymentRailsRequestV1Types apitypes.Types
iden3PaymentRailsERC20RequestV1Types apitypes.Types
}

// NewPaymentService creates a new payment service
func NewPaymentService(payOptsRepo ports.PaymentRepository, resolver network.Resolver, schemaSrv ports.SchemaService, settings *payments.Config, kms kms.KMSType) ports.PaymentService {
return &payment{
networkResolver: resolver,
settings: *settings,
schemaService: schemaSrv,
paymentsStore: payOptsRepo,
kms: kms,
func NewPaymentService(payOptsRepo ports.PaymentRepository, resolver network.Resolver, schemaSrv ports.SchemaService, settings *payments.Config, kms kms.KMSType) (ports.PaymentService, error) {
iden3PaymentRailsRequestV1Types := apitypes.Types{}
iden3PaymentRailsERC20RequestV1Types := apitypes.Types{}
err := json.Unmarshal([]byte(domain.Iden3PaymentRailsRequestV1SchemaJSON), &iden3PaymentRailsRequestV1Types)
if err != nil {
log.Error(context.Background(), "failed to unmarshal Iden3PaymentRailsRequestV1 schema", "err", err)
return nil, err
}
err = json.Unmarshal([]byte(domain.Iden3PaymentRailsERC20RequestV1SchemaJSON), &iden3PaymentRailsERC20RequestV1Types)
if err != nil {
log.Error(context.Background(), "failed to unmarshal Iden3PaymentRailsERC20RequestV1 schema", "err", err)
return nil, err
}
return &payment{
networkResolver: resolver,
settings: *settings,
schemaService: schemaSrv,
paymentsStore: payOptsRepo,
kms: kms,
iden3PaymentRailsRequestV1Types: iden3PaymentRailsRequestV1Types,
iden3PaymentRailsERC20RequestV1Types: iden3PaymentRailsERC20RequestV1Types,
}, nil
}

// CreatePaymentOption creates a payment option for a specific issuer
Expand Down Expand Up @@ -440,37 +457,19 @@ func (p *payment) paymentRequestSignature(
ID: string(decodedKeyID),
}

var types apitypes.Types
switch paymentType {
case string(protocol.Iden3PaymentRailsRequestV1Type):
types = p.iden3PaymentRailsRequestV1Types
case string(protocol.Iden3PaymentRailsERC20RequestV1Type):
types = p.iden3PaymentRailsERC20RequestV1Types
default:
log.Error(ctx, fmt.Sprintf("unsupported payment type '%s'", paymentType), "err", err)
return nil, fmt.Errorf("unsupported payment type '%s:'", paymentType)
}

typedData := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": []apitypes.Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
},
paymentType: []apitypes.Type{
{
Name: "recipient",
Type: "address",
},
{
Name: "amount",
Type: "uint256",
},
{
Name: "expirationDate",
Type: "uint256",
},
{
Name: "nonce",
Type: "uint256",
},
{
Name: "metadata",
Type: "bytes",
},
},
},
Types: types,
PrimaryType: paymentType,
Domain: apitypes.TypedDataDomain{
Name: "MCPayment",
Expand All @@ -487,32 +486,6 @@ func (p *payment) paymentRequestSignature(
},
}
if paymentType == string(protocol.Iden3PaymentRailsERC20RequestV1Type) {
typedData.Types[paymentType] = []apitypes.Type{
{
Name: "tokenAddress",
Type: "address",
},
{
Name: "recipient",
Type: "address",
},
{
Name: "amount",
Type: "uint256",
},
{
Name: "expirationDate",
Type: "uint256",
},
{
Name: "nonce",
Type: "uint256",
},
{
Name: "metadata",
Type: "bytes",
},
}
typedData.Message["tokenAddress"] = setting.PaymentOption.ContractAddress.String()
}
typedDataBytes, _, err := apitypes.TypedDataAndHash(typedData)
Expand Down

0 comments on commit e82c5e6

Please sign in to comment.