From abd523ae64017fada63d5ef3746a66bdb31905e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=ADghearn=C3=A1n=20Carroll?= Date: Fri, 18 Mar 2022 15:05:23 +0000 Subject: [PATCH] working with peer channel path (#136) Co-authored-by: Mark Robson --- config/config.go | 3 +++ config/defaults.go | 1 + config/viper.go | 1 + data/sqlite/migrations/1_initial.up.sql | 1 + data/sqlite/peerchannels.go | 6 +++--- peerchannels.go | 2 ++ service/pay.go | 2 ++ service/payments.go | 2 ++ service/peerchannels.go | 4 ++++ service/peerchannels_notify.go | 4 ++-- 10 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 36a7743e..d026935b 100644 --- a/config/config.go +++ b/config/config.go @@ -40,6 +40,7 @@ const ( EnvTransportHTTPEnabled = "transport.http.enabled" EnvTransportSocketsEnabled = "transport.sockets.enabled" EnvPeerChannelsHost = "peerchannels.host" + EnvPeerChannelsPath = "peerchannels.path" EnvPeerChannelsTTL = "peerchannels.ttl.minutes" LogDebug = "debug" @@ -164,6 +165,8 @@ type Wallet struct { type PeerChannels struct { // Host the peer channels host. Host string + // Path to peer channels. + Path string // TTL the life of the peer channel. TTL time.Duration } diff --git a/config/defaults.go b/config/defaults.go index 82bbadcd..7eb82f8c 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -58,4 +58,5 @@ func SetupDefaults() { // Peer channels viper.SetDefault(EnvPeerChannelsTTL, 120) + viper.SetDefault(EnvPeerChannelsPath, "") } diff --git a/config/viper.go b/config/viper.go index 4509bbac..a1bbbe32 100644 --- a/config/viper.go +++ b/config/viper.go @@ -121,6 +121,7 @@ func (v *ViperConfig) WithTransports() ConfigurationLoader { func (v *ViperConfig) WithPeerChannels() ConfigurationLoader { v.PeerChannels = &PeerChannels{ Host: viper.GetString(EnvPeerChannelsHost), + Path: viper.GetString(EnvPeerChannelsPath), TTL: time.Duration(viper.GetInt64(EnvPeerChannelsTTL)) * time.Minute, } return v diff --git a/data/sqlite/migrations/1_initial.up.sql b/data/sqlite/migrations/1_initial.up.sql index d8108619..9708c009 100644 --- a/data/sqlite/migrations/1_initial.up.sql +++ b/data/sqlite/migrations/1_initial.up.sql @@ -45,6 +45,7 @@ CREATE TABLE peerchannels( ,peerchannels_account_id INTEGER NOT NULL ,channel_id VARCHAR NOT NULL ,channel_host VARCHAR NOT NULL + ,channel_path VARCHAR NOT NULL ,channel_type VARCHAR NOT NULL ,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,closed BOOLEAN NOT NULL DEFAULT 0 diff --git a/data/sqlite/peerchannels.go b/data/sqlite/peerchannels.go index b9ca679d..a6a7bdd4 100644 --- a/data/sqlite/peerchannels.go +++ b/data/sqlite/peerchannels.go @@ -14,8 +14,8 @@ const sqlPeerChannelAccountSelect = ` ` const sqlPeerChannelInsert = ` - INSERT INTO peerchannels (peerchannels_account_id, channel_id, channel_host, channel_type, created_at) - VALUES (:peerchannels_account_id, :channel_id, :channel_host, :channel_type, :created_at) + INSERT INTO peerchannels (peerchannels_account_id, channel_id, channel_host, channel_path, channel_type, created_at) + VALUES (:peerchannels_account_id, :channel_id, :channel_host, :channel_path, :channel_type, :created_at) ` const sqlPeerChannelsAPITokInsert = ` @@ -30,7 +30,7 @@ const sqlPeerChannelsCloseUpdate = ` ` const sqlPeerChannelsOpenSelect = ` - SELECT pc.channel_host, pc.channel_id, pc.channel_type, pc.created_at, pt.tok + SELECT pc.channel_host, pc.channel_path, pc.channel_id, pc.channel_type, pc.created_at, pt.tok FROM peerchannels pc JOIN peerchannels_toks pt ON pc.channel_id = pt.peerchannels_channel_id WHERE pc.closed = 0 AND pc.channel_type = :channel_type diff --git a/peerchannels.go b/peerchannels.go index 276db6c2..4983c481 100644 --- a/peerchannels.go +++ b/peerchannels.go @@ -31,6 +31,7 @@ type PeerChannel struct { ID string `db:"channel_id"` Token string `db:"tok"` Host string `db:"channel_host"` + Path string `db:"channel_path"` CreatedAt time.Time `db:"created_at"` Type PeerChannelHandlerType `db:"channel_type"` } @@ -52,6 +53,7 @@ type PeerChannelCreateArgs struct { PeerChannelAccountID int64 `db:"peerchannels_account_id"` ChannelType PeerChannelHandlerType `db:"channel_type"` ChannelHost string `db:"channel_host"` + ChannelPath string `db:"channel_path"` ChannelID string `db:"channel_id"` CreatedAt time.Time `db:"created_at"` } diff --git a/service/pay.go b/service/pay.go index ba64468e..65d1fd13 100644 --- a/service/pay.go +++ b/service/pay.go @@ -129,6 +129,7 @@ func (p *pay) Pay(ctx context.Context, req payd.PayRequest) (*dpp.PaymentACK, er PeerChannelAccountID: 0, ChannelID: ack.PeerChannel.ChannelID, ChannelHost: ack.PeerChannel.Host, + ChannelPath: ack.PeerChannel.Path, ChannelType: payd.PeerChannelHandlerTypeProof, }); err != nil { return nil, errors.Wrapf(err, "failed to store channel %s/%s in db", ack.PeerChannel.Host, ack.PeerChannel.ChannelID) @@ -147,6 +148,7 @@ func (p *pay) Pay(ctx context.Context, req payd.PayRequest) (*dpp.PaymentACK, er ID: ack.PeerChannel.ChannelID, Token: ack.PeerChannel.Token, Host: ack.PeerChannel.Host, + Path: ack.PeerChannel.Path, Type: payd.PeerChannelHandlerTypeProof, }); err != nil { log.Err(err) diff --git a/service/payments.go b/service/payments.go index b02b20db..192f2dab 100644 --- a/service/payments.go +++ b/service/payments.go @@ -241,6 +241,7 @@ func (p *payments) PaymentCreate(ctx context.Context, args payd.PaymentCreateArg if err := p.pcNotif.Subscribe(ctx, &payd.PeerChannel{ ID: ch.ID, Token: tokens[1].Token, + Host: p.pCfg.Host, CreatedAt: ch.CreatedAt, Type: payd.PeerChannelHandlerTypeProof, }); err != nil { @@ -301,6 +302,7 @@ func (p *payments) Ack(ctx context.Context, args payd.AckArgs, req payd.Ack) err if err := p.pcStr.PeerChannelCreate(ctx, &payd.PeerChannelCreateArgs{ PeerChannelAccountID: 0, ChannelHost: args.PeerChannel.Host, + ChannelPath: args.PeerChannel.Path, ChannelID: args.PeerChannel.ID, ChannelType: args.PeerChannel.Type, }); err != nil { diff --git a/service/peerchannels.go b/service/peerchannels.go index e239353f..0dff8bd8 100644 --- a/service/peerchannels.go +++ b/service/peerchannels.go @@ -29,6 +29,7 @@ func (p *peerChannelsSvc) PeerChannelCreate(ctx context.Context, req spvchannels spvchannels.WithPassword("password"), spvchannels.WithVersion("v1"), spvchannels.WithBaseURL(p.cfg.Host), + spvchannels.WithPath(p.cfg.Path), spvchannels.WithNoTLS(), ) ch, err := c.ChannelCreate(ctx, req) @@ -40,6 +41,7 @@ func (p *peerChannelsSvc) PeerChannelCreate(ctx context.Context, req spvchannels if err := p.str.PeerChannelCreate(ctx, &payd.PeerChannelCreateArgs{ PeerChannelAccountID: req.AccountID, ChannelHost: p.cfg.Host, + ChannelPath: p.cfg.Path, ChannelID: ch.ID, ChannelType: payd.PeerChannelHandlerTypeProof, CreatedAt: createdAt, @@ -61,6 +63,7 @@ func (p peerChannelsSvc) PeerChannelAPITokensCreate(ctx context.Context, reqs .. spvchannels.WithPassword("password"), spvchannels.WithVersion("v1"), spvchannels.WithBaseURL(p.cfg.Host), + spvchannels.WithPath(p.cfg.Path), spvchannels.WithNoTLS(), ) @@ -94,6 +97,7 @@ func (p *peerChannelsSvc) PeerChannelsMessage(ctx context.Context, args *payd.Pe spvchannels.WithChannelID(args.ChannelID), spvchannels.WithVersion("v1"), spvchannels.WithBaseURL(p.cfg.Host), + spvchannels.WithPath(p.cfg.Path), spvchannels.WithNoTLS(), ) msgs, err := c.Messages(ctx, spvchannels.MessagesRequest{ diff --git a/service/peerchannels_notify.go b/service/peerchannels_notify.go index 66a0d4ce..e516fe62 100644 --- a/service/peerchannels_notify.go +++ b/service/peerchannels_notify.go @@ -49,8 +49,8 @@ func (p *peerChannelsNotifySvc) Subscribe(ctx context.Context, channel *payd.Pee u := url.URL{ Scheme: "ws", - Host: p.cfg.Host, - Path: path.Join("/api/v1/channel", channel.ID, "/notify"), + Host: channel.Host, + Path: path.Join(channel.Path, "/api/v1/channel", channel.ID, "/notify"), } q := u.Query() q.Set("token", channel.Token)