-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from SiaFoundation/nate/add-host-key
Add host key to account token
- Loading branch information
Showing
6 changed files
with
134 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
default: major | ||
--- | ||
|
||
# Add host public key to AccountToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package rhp | ||
|
||
import ( | ||
"bytes" | ||
"math" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"go.sia.tech/core/types" | ||
"lukechampine.com/frand" | ||
) | ||
|
||
type rhpEncodable[T any] interface { | ||
*T | ||
encodeTo(*types.Encoder) | ||
decodeFrom(*types.Decoder) | ||
} | ||
|
||
func testRoundtrip[T any, PT rhpEncodable[T]](a PT) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
buf := bytes.NewBuffer(nil) | ||
enc := types.NewEncoder(buf) | ||
|
||
a.encodeTo(enc) | ||
if err := enc.Flush(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
b := new(T) | ||
dec := types.NewBufDecoder(buf.Bytes()) | ||
PT(b).decodeFrom(dec) | ||
|
||
if !reflect.DeepEqual(a, b) { | ||
t.Log(a) | ||
t.Log(reflect.ValueOf(b).Elem()) | ||
t.Fatal("expected rountrip to match") | ||
} | ||
} | ||
} | ||
|
||
func TestEncodingRoundtrip(t *testing.T) { | ||
t.Run("AccountToken", testRoundtrip(&AccountToken{ | ||
HostKey: frand.Entropy256(), | ||
Account: frand.Entropy256(), | ||
ValidUntil: time.Unix(int64(frand.Intn(math.MaxInt)), 0), | ||
Signature: types.Signature(frand.Bytes(64)), | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package rhp | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"go.sia.tech/core/types" | ||
"lukechampine.com/frand" | ||
) | ||
|
||
func TestValidateAccountToken(t *testing.T) { | ||
hostKey := types.GeneratePrivateKey().PublicKey() | ||
renterKey := types.GeneratePrivateKey() | ||
account := Account(renterKey.PublicKey()) | ||
|
||
ac := AccountToken{ | ||
HostKey: hostKey, | ||
Account: account, | ||
ValidUntil: time.Now().Add(-time.Minute), | ||
} | ||
|
||
if err := ac.Validate(frand.Entropy256()); !strings.Contains(err.Error(), "host key mismatch") { | ||
t.Fatalf("expected host key mismatch, got %v", err) | ||
} else if err := ac.Validate(hostKey); !strings.Contains(err.Error(), "token expired") { | ||
t.Fatalf("expected token expired, got %v", err) | ||
} | ||
|
||
ac.ValidUntil = time.Now().Add(time.Minute) | ||
if err := ac.Validate(hostKey); !errors.Is(err, ErrInvalidSignature) { | ||
t.Fatalf("expected ErrInvalidSignature, got %v", err) | ||
} | ||
|
||
ac.Signature = renterKey.SignHash(ac.SigHash()) | ||
|
||
if err := ac.Validate(hostKey); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |