From d5de87e8a92a6c8c09a5686b2f9260a7f47a77a5 Mon Sep 17 00:00:00 2001 From: oliha Date: Wed, 7 Aug 2019 11:17:22 -0400 Subject: [PATCH 1/3] fix manage data value --- txnbuild/transaction.go | 6 +++--- txnbuild/transaction_test.go | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/txnbuild/transaction.go b/txnbuild/transaction.go index 80eeb7c8c7..7c9143d760 100644 --- a/txnbuild/transaction.go +++ b/txnbuild/transaction.go @@ -210,13 +210,13 @@ func BuildChallengeTx(serverSignerSecret, clientAccountID, anchorName, network s return "", err } - randomNonce, err := generateRandomNonce(64) + randomNonce, err := generateRandomNonce(48) if err != nil { return "", err } - if len(randomNonce) != 64 { - return "", errors.New("64 byte long random nonce required") + if len(randomNonce) != 48 { + return "", errors.New("48 byte long random nonce required") } // represent server signing account as SimpleAccount diff --git a/txnbuild/transaction_test.go b/txnbuild/transaction_test.go index de93556e33..5e5987ebff 100644 --- a/txnbuild/transaction_test.go +++ b/txnbuild/transaction_test.go @@ -2,6 +2,7 @@ package txnbuild import ( "crypto/sha256" + "encoding/base64" "testing" "time" @@ -754,7 +755,9 @@ func TestBuildChallengeTx(t *testing.T) { op := txXDR.Tx.Operations[0] assert.Equal(t, xdr.OperationTypeManageData, op.Body.Type, "operation type should be manage data") assert.Equal(t, xdr.String64("SDF auth"), op.Body.ManageDataOp.DataName, "DataName should be 'SDF auth'") - assert.Equal(t, 64, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 64 bytes") + assert.Equal(t, 48, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") + dvB64 := base64.StdEncoding.EncodeToString(*op.Body.ManageDataOp.DataValue) + assert.Equal(t, 64, len(dvB64), "DataValue as base64 should be 64 bytes") // 5 minutes timebound txeBase64, err = BuildChallengeTx(kp0.Seed(), kp0.Address(), "SDF1", network.TestNetworkPassphrase, time.Duration(5*time.Minute)) @@ -772,7 +775,9 @@ func TestBuildChallengeTx(t *testing.T) { op1 := txXDR1.Tx.Operations[0] assert.Equal(t, xdr.OperationTypeManageData, op1.Body.Type, "operation type should be manage data") assert.Equal(t, xdr.String64("SDF1 auth"), op1.Body.ManageDataOp.DataName, "DataName should be 'SDF1 auth'") - assert.Equal(t, 64, len(*op1.Body.ManageDataOp.DataValue), "DataValue should be 64 bytes") + assert.Equal(t, 48, len(*op1.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") + dvB64 = base64.StdEncoding.EncodeToString(*op.Body.ManageDataOp.DataValue) + assert.Equal(t, 64, len(dvB64), "DataValue as base64 should be 64 bytes") } func TestHashHex(t *testing.T) { From 427f9f283695b1f16fa15a90addab6868050bb93 Mon Sep 17 00:00:00 2001 From: oliha Date: Wed, 7 Aug 2019 12:46:27 -0400 Subject: [PATCH 2/3] encode nonce to base64 --- txnbuild/transaction.go | 10 ++++++---- txnbuild/transaction_test.go | 9 ++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/txnbuild/transaction.go b/txnbuild/transaction.go index 7c9143d760..ac35205749 100644 --- a/txnbuild/transaction.go +++ b/txnbuild/transaction.go @@ -210,13 +210,15 @@ func BuildChallengeTx(serverSignerSecret, clientAccountID, anchorName, network s return "", err } + // SEP10 spec requires 48 byte cryptographic-quality random string randomNonce, err := generateRandomNonce(48) if err != nil { return "", err } - - if len(randomNonce) != 48 { - return "", errors.New("48 byte long random nonce required") + // Encode 48-byte nonce to base64 for a total of 64-bytes + randomNonceToString := base64.StdEncoding.EncodeToString(randomNonce) + if len(randomNonceToString) != 64 { + return "", errors.New("64 byte long random nonce required") } // represent server signing account as SimpleAccount @@ -248,7 +250,7 @@ func BuildChallengeTx(serverSignerSecret, clientAccountID, anchorName, network s &ManageData{ SourceAccount: &ca, Name: anchorName + " auth", - Value: randomNonce, + Value: []byte(randomNonceToString), }, }, Timebounds: txTimebound, diff --git a/txnbuild/transaction_test.go b/txnbuild/transaction_test.go index 5e5987ebff..25e6c81249 100644 --- a/txnbuild/transaction_test.go +++ b/txnbuild/transaction_test.go @@ -2,7 +2,6 @@ package txnbuild import ( "crypto/sha256" - "encoding/base64" "testing" "time" @@ -755,9 +754,7 @@ func TestBuildChallengeTx(t *testing.T) { op := txXDR.Tx.Operations[0] assert.Equal(t, xdr.OperationTypeManageData, op.Body.Type, "operation type should be manage data") assert.Equal(t, xdr.String64("SDF auth"), op.Body.ManageDataOp.DataName, "DataName should be 'SDF auth'") - assert.Equal(t, 48, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") - dvB64 := base64.StdEncoding.EncodeToString(*op.Body.ManageDataOp.DataValue) - assert.Equal(t, 64, len(dvB64), "DataValue as base64 should be 64 bytes") + assert.Equal(t, 64, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") // 5 minutes timebound txeBase64, err = BuildChallengeTx(kp0.Seed(), kp0.Address(), "SDF1", network.TestNetworkPassphrase, time.Duration(5*time.Minute)) @@ -775,9 +772,7 @@ func TestBuildChallengeTx(t *testing.T) { op1 := txXDR1.Tx.Operations[0] assert.Equal(t, xdr.OperationTypeManageData, op1.Body.Type, "operation type should be manage data") assert.Equal(t, xdr.String64("SDF1 auth"), op1.Body.ManageDataOp.DataName, "DataName should be 'SDF1 auth'") - assert.Equal(t, 48, len(*op1.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") - dvB64 = base64.StdEncoding.EncodeToString(*op.Body.ManageDataOp.DataValue) - assert.Equal(t, 64, len(dvB64), "DataValue as base64 should be 64 bytes") + assert.Equal(t, 64, len(*op1.Body.ManageDataOp.DataValue), "DataValue should be 64 bytes") } func TestHashHex(t *testing.T) { From 6daa385906b44420c4bc0680db54ae6da20ac78a Mon Sep 17 00:00:00 2001 From: oliha Date: Wed, 7 Aug 2019 13:18:47 -0400 Subject: [PATCH 3/3] fix typo --- txnbuild/transaction_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/txnbuild/transaction_test.go b/txnbuild/transaction_test.go index 25e6c81249..de93556e33 100644 --- a/txnbuild/transaction_test.go +++ b/txnbuild/transaction_test.go @@ -754,7 +754,7 @@ func TestBuildChallengeTx(t *testing.T) { op := txXDR.Tx.Operations[0] assert.Equal(t, xdr.OperationTypeManageData, op.Body.Type, "operation type should be manage data") assert.Equal(t, xdr.String64("SDF auth"), op.Body.ManageDataOp.DataName, "DataName should be 'SDF auth'") - assert.Equal(t, 64, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 48 bytes") + assert.Equal(t, 64, len(*op.Body.ManageDataOp.DataValue), "DataValue should be 64 bytes") // 5 minutes timebound txeBase64, err = BuildChallengeTx(kp0.Seed(), kp0.Address(), "SDF1", network.TestNetworkPassphrase, time.Duration(5*time.Minute))