Skip to content

Commit

Permalink
do not rely only on transaction signatures (#1066)
Browse files Browse the repository at this point in the history
Update `x/sigs` and `x/username` to not rely only on `x.AnySigner` but
use it as a fallback. Always allow to enforece an address by setting it
in the message and ignore signers order.
  • Loading branch information
husio authored Nov 29, 2019
1 parent 8f9397f commit fd99a32
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- deprecate and rename `x.MainSigner` to `x.AnySigner` to better describe
provided functionality.
- `x/sigs` allow to explicitly specify which user to bump the sequence for
instead of relying on the transaction signatures.
- `x/username` when registering a token, allow to explicitly specify the new
token owner instead of relying only on the transaction signatures.

## 0.22.0

Expand Down
1 change: 1 addition & 0 deletions cmd/bnscli/clitests/register_username.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -e
bnscli register-username \
-name alice \
-bc fairycoin \
-owner 92066456B2BE7F1934624087D98C203A87F7752C \
-addr 46722031342e204a756e2031303a35363a3337204345535420323031390a \
| bnscli view

Expand Down
3 changes: 2 additions & 1 deletion cmd/bnscli/clitests/register_username.test.gold
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"blockchain_id": "fairycoin",
"address": "46722031342e204a756e2031303a35363a3337204345535420323031390a"
}
]
],
"owner": "92066456B2BE7F1934624087D98C203A87F7752C"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/bnscli/cmd_username.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Create a transaction for registering a username.
}
var (
nameFl = fl.String("name", "", "Name part of the username. For example 'alice'")
ownerFl = flAddress(fl, "owner", "", "An address that the newly registered token will belong to. Transaction does not have to be signed by this address.")
namespaceFl = fl.String("ns", "iov", "Namespace (domain) part of the username. For example 'iov'")
blockchainFl = fl.String("bc", "", "Blockchain network ID.")
addressFl = fl.String("addr", "", "String representation of the blochain address on this network.")
Expand All @@ -46,6 +47,7 @@ Create a transaction for registering a username.
Metadata: &weave.Metadata{Schema: 1},
Username: *nameFl + "*" + *namespaceFl,
Targets: targets,
Owner: *ownerFl,
}
if err := msg.Validate(); err != nil {
return fmt.Errorf("given data produce an invalid message: %s", err)
Expand Down
117 changes: 85 additions & 32 deletions cmd/bnsd/x/username/codec.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cmd/bnsd/x/username/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ message RegisterTokenMsg {
string username = 2;
// Targets is a blockchain address list that this token should point to.
repeated BlockchainAddress targets = 3 [(gogoproto.nullable) = false];
// Owner is the address that the newly created token will belong to.
bytes owner = 4 [(gogoproto.casttype) = "github.com/iov-one/weave.Address"];
}

// TransferTokenMsg is a request to transfer an ownership of a token. The
Expand Down
12 changes: 9 additions & 3 deletions cmd/bnsd/x/username/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ func (h *registerTokenHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w
return nil, err
}

owner := x.AnySigner(ctx, h.auth).Address()
if len(owner) == 0 {
return nil, errors.Wrap(errors.ErrUnauthorized, "message must be signed")
var owner weave.Address
if len(msg.Owner) != 0 {
// Permission is not required.
owner = msg.Owner
} else {
owner = x.AnySigner(ctx, h.auth).Address()
if len(owner) == 0 {
return nil, errors.Wrap(errors.ErrUnauthorized, "message must be signed")
}
}

token := Token{
Expand Down
7 changes: 7 additions & 0 deletions cmd/bnsd/x/username/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func (m *RegisterTokenMsg) Validate() error {

// Username should but cannot be validated here.

// Owner field is optional.
if len(m.Owner) != 0 {
if err := m.Owner.Validate(); err != nil {
return errors.Wrap(err, "owner")
}
}

if err := validateTargets(m.Targets); err != nil {
return errors.Wrap(err, "targets")
}
Expand Down
16 changes: 15 additions & 1 deletion docs/proto/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,13 @@ <h3 id="username.RegisterTokenMsg">RegisterTokenMsg</h3>
<td><p>Targets is a blockchain address list that this token should point to. </p></td>
</tr>

<tr>
<td>owner</td>
<td><a href="#bytes">bytes</a></td>
<td></td>
<td><p>Owner is the address that the newly created token will belong to. </p></td>
</tr>

</tbody>
</table>

Expand Down Expand Up @@ -5750,7 +5757,7 @@ <h2 id="x/sigs/codec.proto">x/sigs/codec.proto</h2><a href="#title">Top</a>


<h3 id="sigs.BumpSequenceMsg">BumpSequenceMsg</h3>
<p>BumpSequenceMsg increments a sequence counter by given amount for a user</p><p>that signed the transaction.</p>
<p>BumpSequenceMsg increments a sequence counter by given amount for a user.</p>


<table class="field-table">
Expand All @@ -5777,6 +5784,13 @@ <h3 id="sigs.BumpSequenceMsg">BumpSequenceMsg</h3>
total increment value, including the default increment. </p></td>
</tr>

<tr>
<td>user</td>
<td><a href="#bytes">bytes</a></td>
<td></td>
<td><p>User is the address of a user that sequence is to be incremented for. </p></td>
</tr>

</tbody>
</table>

Expand Down
2 changes: 2 additions & 0 deletions spec/gogo/cmd/bnsd/x/username/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ message RegisterTokenMsg {
string username = 2;
// Targets is a blockchain address list that this token should point to.
repeated BlockchainAddress targets = 3 [(gogoproto.nullable) = false];
// Owner is the address that the newly created token will belong to.
bytes owner = 4 [(gogoproto.casttype) = "github.com/iov-one/weave.Address"];
}

// TransferTokenMsg is a request to transfer an ownership of a token. The
Expand Down
6 changes: 4 additions & 2 deletions spec/gogo/x/sigs/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sigs;

import "codec.proto";
import "crypto/models.proto";
import "gogoproto/gogo.proto";

// UserData just stores the data and is used for serialization.
// Key is the Address (PubKey.Permission().Address())
Expand All @@ -28,8 +29,7 @@ message StdSignature {
crypto.Signature signature = 4;
}

// BumpSequenceMsg increments a sequence counter by given amount for a user
// that signed the transaction.
// BumpSequenceMsg increments a sequence counter by given amount for a user.
message BumpSequenceMsg {
weave.Metadata metadata = 1;
// Increment represents the value by which a sequence value will be
Expand All @@ -38,4 +38,6 @@ message BumpSequenceMsg {
// Each transaction increments the sequence by one. This value represents the
// total increment value, including the default increment.
uint32 increment = 2;
// User is the address of a user that sequence is to be incremented for.
bytes user = 3 [(gogoproto.casttype) = "github.com/iov-one/weave.Address"];
}
2 changes: 2 additions & 0 deletions spec/proto/cmd/bnsd/x/username/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ message RegisterTokenMsg {
string username = 2;
// Targets is a blockchain address list that this token should point to.
repeated BlockchainAddress targets = 3 ;
// Owner is the address that the newly created token will belong to.
bytes owner = 4 ;
}

// TransferTokenMsg is a request to transfer an ownership of a token. The
Expand Down
5 changes: 3 additions & 2 deletions spec/proto/x/sigs/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ message StdSignature {
crypto.Signature signature = 4;
}

// BumpSequenceMsg increments a sequence counter by given amount for a user
// that signed the transaction.
// BumpSequenceMsg increments a sequence counter by given amount for a user.
message BumpSequenceMsg {
weave.Metadata metadata = 1;
// Increment represents the value by which a sequence value will be
Expand All @@ -38,4 +37,6 @@ message BumpSequenceMsg {
// Each transaction increments the sequence by one. This value represents the
// total increment value, including the default increment.
uint32 increment = 2;
// User is the address of a user that sequence is to be incremented for.
bytes user = 3 ;
}
Loading

0 comments on commit fd99a32

Please sign in to comment.