-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
txnbuild: SEP 10 challenge builder #1468
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b75db47
SEP 10 challenge builder - test failing
ire-and-curses 0e9087e
fix challenge txbuild
poliha da63650
add example
poliha 8fc9939
add default values to txnbuild.BuildChallengeTx
poliha c7c23f7
Merge branch 'master' into es-1466-sep10-helper
poliha 2dedf51
fix timebounds and tests
poliha 6dab03a
update txnbuild changelog
poliha d0897b0
review changes
poliha 291b953
Merge branch 'master' into es-1466-sep10-helper
poliha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -11,10 +11,12 @@ package txnbuild | |
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/stellar/go/keypair" | ||
"github.com/stellar/go/network" | ||
|
@@ -198,6 +200,85 @@ func (tx *Transaction) BuildSignEncode(keypairs ...*keypair.Full) (string, error | |
return txeBase64, err | ||
} | ||
|
||
// BuildChallengeTx is a factory method that creates a valid SEP 10 challenge, for use in web authentication. | ||
// "timebound" is the time duration the transaction should be valid for, O means infinity. | ||
// More details on SEP 10: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md | ||
func BuildChallengeTx(serverSignerSecret, clientAccountID, anchorName, network string, timebound time.Duration) (string, error) { | ||
serverKP, err := keypair.Parse(serverSignerSecret) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
randomNonce, err := generateRandomString(64) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
randomNonceBytes, err := base64.StdEncoding.DecodeString(randomNonce) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to decode random nonce") | ||
} | ||
|
||
if len(randomNonceBytes) != 64 { | ||
return "", errors.New("64 byte long random nonce required") | ||
} | ||
|
||
// represent server signing account as SimpleAccount | ||
sa := SimpleAccount{ | ||
AccountID: serverKP.Address(), | ||
// Action needed in release: v2.0.0 | ||
// TODO: remove this and use "Sequence: 0" and build transaction with optional argument | ||
// (https://github.com/stellar/go/issues/1259) | ||
Sequence: int64(-1), | ||
} | ||
|
||
// represent client account as SimpleAccount | ||
ca := SimpleAccount{ | ||
AccountID: clientAccountID, | ||
} | ||
|
||
txTimebound := NewInfiniteTimeout() | ||
if timebound > 0 { | ||
currentTime := time.Now().UTC() | ||
maxTime := currentTime.Add(timebound) | ||
txTimebound = NewTimebounds(currentTime.Unix(), maxTime.Unix()) | ||
} | ||
|
||
// Create a SEP 10 compatible response. See | ||
// https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md#response | ||
tx := Transaction{ | ||
SourceAccount: &sa, | ||
Operations: []Operation{ | ||
&ManageData{ | ||
SourceAccount: &ca, | ||
Name: anchorName + " auth", | ||
Value: randomNonceBytes, | ||
}, | ||
}, | ||
Timebounds: txTimebound, | ||
Network: network, | ||
BaseFee: uint32(100), | ||
} | ||
|
||
txeB64, err := tx.BuildSignEncode(serverKP.(*keypair.Full)) | ||
if err != nil { | ||
return "", err | ||
} | ||
return txeB64, nil | ||
poliha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// generateRandomString creates a base-64 encoded, cryptographically secure random string of `n` bytes. | ||
func generateRandomString(n int) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this function return |
||
bytes := make([]byte, n) | ||
_, err := rand.Read(bytes) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(bytes), err | ||
poliha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// HashHex returns the hex-encoded hash of the transaction. | ||
func (tx *Transaction) HashHex() (string, error) { | ||
hashByte, err := tx.Hash() | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a simple sanity check (if tx contains
manage_data
operation with a correct name) would be great to have.