-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xdr, keypair: Add helpers to create CAP-40 decorated signatures (#4302)
- Loading branch information
Showing
8 changed files
with
122 additions
and
13 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
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
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,38 @@ | ||
package xdr | ||
|
||
// NewDecoratedSignature constructs a decorated signature structure directly | ||
// from the given signature and hint. Note that the hint should | ||
// correspond to the signer that created the signature, but this helper cannot | ||
// ensure that. | ||
func NewDecoratedSignature(sig []byte, hint [4]byte) DecoratedSignature { | ||
return DecoratedSignature{ | ||
Hint: SignatureHint(hint), | ||
Signature: Signature(sig), | ||
} | ||
} | ||
|
||
// NewDecoratedSignatureForPayload creates a decorated signature with a hint | ||
// that uses the key hint, the last four bytes of signature, and the last four | ||
// bytes of the input that got signed. Note that the signature should be the | ||
// signature of the payload via the key being hinted, but this construction | ||
// method cannot ensure that. | ||
func NewDecoratedSignatureForPayload( | ||
sig []byte, keyHint [4]byte, payload []byte, | ||
) DecoratedSignature { | ||
hint := [4]byte{} | ||
// copy the last four bytes of the payload into the hint | ||
if len(payload) >= len(hint) { | ||
copy(hint[:], payload[len(payload)-len(hint):]) | ||
} else { | ||
copy(hint[:], payload[:]) | ||
} | ||
|
||
for i := 0; i < len(keyHint); i++ { | ||
hint[i] ^= keyHint[i] | ||
} | ||
|
||
return DecoratedSignature{ | ||
Hint: SignatureHint(hint), | ||
Signature: Signature(sig), | ||
} | ||
} |
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,51 @@ | ||
package xdr_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stellar/go/xdr" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
signature = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8} | ||
hint = [4]byte{9, 10, 11, 12} | ||
) | ||
|
||
func TestDecoratedSignature(t *testing.T) { | ||
decoSig := xdr.NewDecoratedSignature(signature, hint) | ||
assert.EqualValues(t, hint, decoSig.Hint) | ||
assert.EqualValues(t, signature, decoSig.Signature) | ||
} | ||
|
||
func TestDecoratedSignatureForPayload(t *testing.T) { | ||
testCases := []struct { | ||
payload []byte | ||
expectedHint [4]byte | ||
}{ | ||
{ | ||
[]byte{13, 14, 15, 16, 17, 18, 19, 20, 21}, | ||
[4]byte{27, 25, 31, 25}, | ||
}, | ||
{ | ||
[]byte{18, 19, 20}, | ||
[4]byte{27, 25, 31, 12}, | ||
}, | ||
{ | ||
[]byte{}, | ||
hint, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run( | ||
fmt.Sprintf("%d-byte payload", len(testCase.payload)), | ||
func(t *testing.T) { | ||
decoSig := xdr.NewDecoratedSignatureForPayload( | ||
signature, hint, testCase.payload) | ||
assert.EqualValues(t, testCase.expectedHint, decoSig.Hint) | ||
assert.EqualValues(t, signature, decoSig.Signature) | ||
}) | ||
} | ||
} |
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