11package sdk
22
33import (
4- "context"
5- "encoding/hex"
6- "fmt"
4+ "context"
5+ "encoding/hex"
6+ "fmt"
77
8- servicetypes "github.com/pokt-network/poktroll/x/service/types"
9- "github.com/pokt-network/ring-go"
8+ servicetypes "github.com/pokt-network/poktroll/x/service/types"
9+ "github.com/pokt-network/ring-go"
1010)
1111
1212// Signer holds the application or gateway's private key used to sign Relay Requests.
@@ -16,10 +16,10 @@ import (
1616// - With "ethereum_secp256k1" tag: Uses Ethereum's libsecp256k1 (fastest, requires CGO)
1717// - Without tag: Uses Decred's implementation (excellent performance, pure Go)
1818type Signer struct {
19- // PrivateKeyHex is retained for compatibility and debugging.
20- PrivateKeyHex string
21- // privateKeyBytes caches the 32-byte private key to avoid repeated hex decoding.
22- privateKeyBytes []byte
19+ // PrivateKeyHex is retained for compatibility and debugging.
20+ PrivateKeyHex string
21+ // privateKeyBytes caches the 32-byte private key to avoid repeated hex decoding.
22+ privateKeyBytes []byte
2323}
2424
2525// NewSignerFromHex creates a new Signer instance from a hex-encoded private key.
@@ -32,17 +32,17 @@ type Signer struct {
3232// log.Fatal(err)
3333// }
3434func NewSignerFromHex (privateKeyHex string ) (* Signer , error ) {
35- keyBytes , err := hex .DecodeString (privateKeyHex )
36- if err != nil {
37- return nil , fmt .Errorf ("invalid hex private key: %w" , err )
38- }
39- if len (keyBytes ) != 32 {
40- return nil , fmt .Errorf ("invalid private key length: expected 32 bytes, got %d" , len (keyBytes ))
41- }
42- return & Signer {
43- PrivateKeyHex : privateKeyHex ,
44- privateKeyBytes : keyBytes ,
45- }, nil
35+ keyBytes , err := hex .DecodeString (privateKeyHex )
36+ if err != nil {
37+ return nil , fmt .Errorf ("invalid hex private key: %w" , err )
38+ }
39+ if len (keyBytes ) != 32 {
40+ return nil , fmt .Errorf ("invalid private key length: expected 32 bytes, got %d" , len (keyBytes ))
41+ }
42+ return & Signer {
43+ PrivateKeyHex : privateKeyHex ,
44+ privateKeyBytes : keyBytes ,
45+ }, nil
4646}
4747
4848// Sign signs the given relay request using the signer's private key and the application's ring.
@@ -52,59 +52,55 @@ func NewSignerFromHex(privateKeyHex string) (*Signer, error) {
5252//
5353// Returns a pointer instead of directly setting the signature on the input relay request to avoid implicit output.
5454func (s * Signer ) Sign (
55- ctx context.Context ,
56- relayRequest * servicetypes.RelayRequest ,
57- appRing * ApplicationRing ,
55+ ctx context.Context ,
56+ relayRequest * servicetypes.RelayRequest ,
57+ appRing * ApplicationRing ,
5858) (* servicetypes.RelayRequest , error ) {
59- // Get the session ring for the application's session end block height
60- sessionRing , err := appRing .GetRing (ctx , uint64 (relayRequest .Meta .SessionHeader .SessionEndBlockHeight ))
61- if err != nil {
62- return nil , fmt .Errorf (
63- "Sign: error getting a ring for application address %s: %w" ,
64- appRing .Address ,
65- err ,
66- )
67- }
59+ // Get the session ring for the application's session end block height
60+ sessionRing , err := appRing .GetRing (ctx , uint64 (relayRequest .Meta .SessionHeader .SessionEndBlockHeight ))
61+ if err != nil {
62+ return nil , fmt .Errorf (
63+ "Sign: error getting a ring for application address %s: %w" ,
64+ appRing .Address ,
65+ err ,
66+ )
67+ }
6868
69- // Get the signable bytes hash from the relay request
70- signableBz , err := relayRequest .GetSignableBytesHash ()
71- if err != nil {
72- return nil , fmt .Errorf ("Sign: error getting signable bytes hash from the relay request: %w" , err )
73- }
69+ // Get the signable bytes hash from the relay request
70+ signableBz , err := relayRequest .GetSignableBytesHash ()
71+ if err != nil {
72+ return nil , fmt .Errorf ("Sign: error getting signable bytes hash from the relay request: %w" , err )
73+ }
7474
75- // Sign the request using the session ring and signer's private key
76- // TODO_OPTIMIZE: Pre-cache the decoded scalar to avoid per-call DecodeToScalar
77- // once ring-go exposes a stable public scalar type for long-lived reuse.
78- // Decode private key bytes to scalar (fast, but still cheaper than hex decoding each call)
79- signerPrivKey , err := ring .Secp256k1 ().DecodeToScalar (s .privateKeyBytes )
80- if err != nil {
81- return nil , fmt .Errorf ("Sign: error decoding private key to scalar: %w" , err )
82- }
75+ // Sign the request using the session ring and signer's private key
76+ // TODO_OPTIMIZE: Pre-cache the decoded scalar to avoid per-call DecodeToScalar
77+ // once ring-go exposes a stable public scalar type for long-lived reuse.
78+ // Decode private key bytes to scalar (fast, but still cheaper than hex decoding each call)
79+ signerPrivKey , err := ring .Secp256k1 ().DecodeToScalar (s .privateKeyBytes )
80+ if err != nil {
81+ return nil , fmt .Errorf ("Sign: error decoding private key to scalar: %w" , err )
82+ }
8383
84- ringSig , err := sessionRing .Sign (signableBz , signerPrivKey )
85- if err != nil {
86- return nil , fmt .Errorf (
87- "Sign: error signing using the ring of application with address %s: %w" ,
88- appRing .Address ,
89- err ,
90- )
91- }
84+ ringSig , err := sessionRing .Sign (signableBz , signerPrivKey )
85+ if err != nil {
86+ return nil , fmt .Errorf (
87+ "Sign: error signing using the ring of application with address %s: %w" ,
88+ appRing .Address ,
89+ err ,
90+ )
91+ }
9292
93- // Serialize the signature
94- signature , err := ringSig .Serialize ()
95- if err != nil {
96- return nil , fmt .Errorf (
97- "Sign: error serializing the signature of application with address %s: %w" ,
98- appRing .Address ,
99- err ,
100- )
101- }
93+ // Serialize the signature
94+ signature , err := ringSig .Serialize ()
95+ if err != nil {
96+ return nil , fmt .Errorf (
97+ "Sign: error serializing the signature of application with address %s: %w" ,
98+ appRing .Address ,
99+ err ,
100+ )
101+ }
102102
103- // Set the signature on the relay request
104- relayRequest .Meta .Signature = signature
105- return relayRequest , nil
103+ // Set the signature on the relay request
104+ relayRequest .Meta .Signature = signature
105+ return relayRequest , nil
106106}
107-
108- // GetCryptoSigner returns the underlying crypto signer for advanced use cases.
109- // This allows access to backend-specific functionality if needed.
110- // Backward-compatibility helpers removed: crypto backends are now selected within ring-go.
0 commit comments