Skip to content

Commit

Permalink
working with peer channel path (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Robson <m.robson@8-lm-00154.home>
  • Loading branch information
Tíghearnán Carroll and Mark Robson authored Mar 18, 2022
1 parent af6f3bc commit abd523a
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ func SetupDefaults() {

// Peer channels
viper.SetDefault(EnvPeerChannelsTTL, 120)
viper.SetDefault(EnvPeerChannelsPath, "")
}
1 change: 1 addition & 0 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions data/sqlite/migrations/1_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions data/sqlite/peerchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions peerchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
}
Expand Down
2 changes: 2 additions & 0 deletions service/pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions service/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions service/peerchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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(),
)

Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions service/peerchannels_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit abd523a

Please sign in to comment.