Skip to content

Commit 9afaaff

Browse files
Use raw signing by default (#264)
1 parent 274b178 commit 9afaaff

File tree

2 files changed

+18
-129
lines changed

2 files changed

+18
-129
lines changed

pkg/cascadekit/signatures.go

Lines changed: 9 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77

88
"github.com/LumeraProtocol/supernode/v2/pkg/codec"
99
"github.com/LumeraProtocol/supernode/v2/pkg/errors"
10-
11-
actionkeeper "github.com/LumeraProtocol/lumera/x/action/v1/keeper"
12-
1310
keyringpkg "github.com/LumeraProtocol/supernode/v2/pkg/keyring"
1411

1512
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
@@ -42,28 +39,26 @@ func SignLayoutB64(layout codec.Layout, signer Signer) (layoutB64 string, layout
4239
}
4340

4441
// SignIndexB64 marshals the index to JSON, base64-encodes it, and signs the
45-
// JSON string (not the base64), returning both the index base64 and creator-signature base64.
42+
// base64 payload, returning both the index base64 and creator-signature base64.
4643
//
4744
// IMPORTANT:
48-
// - Message signed = index JSON string (same as JS signArbitrary(indexFileString))
49-
// - indexB64 is still base64(JSON(index)), used in metadata and RQID generation.
45+
// - Message signed = indexB64 string (chain-compatible)
46+
// - indexB64 is base64(JSON(index)), used in metadata and RQID generation.
5047
func SignIndexB64(idx IndexFile, signer Signer) (indexB64 string, creatorSigB64 string, err error) {
5148
raw, err := json.Marshal(idx)
5249
if err != nil {
5350
return "", "", errors.Errorf("marshal index file: %w", err)
5451
}
5552

56-
indexJSON := string(raw)
53+
indexB64 = base64.StdEncoding.EncodeToString(raw)
5754

58-
// Sign the JSON string (JS-style)
59-
sig, err := signer([]byte(indexJSON))
55+
// Sign the base64 payload (chain-compatible)
56+
sig, err := signer([]byte(indexB64))
6057
if err != nil {
6158
return "", "", errors.Errorf("sign index: %w", err)
6259
}
6360
creatorSigB64 = base64.StdEncoding.EncodeToString(sig)
6461

65-
// Base64(JSON(index)) used as the first segment of indexSignatureFormat
66-
indexB64 = base64.StdEncoding.EncodeToString(raw)
6762
return indexB64, creatorSigB64, nil
6863
}
6964

@@ -73,9 +68,7 @@ func SignIndexB64(idx IndexFile, signer Signer) (indexB64 string, creatorSigB64
7368
//
7469
// It validates the layout has exactly one block.
7570
//
76-
// The "signer" can be:
77-
// - raw: directly sign msg bytes (legacy Go path)
78-
// - ADR-36: wrap msg into an ADR-36 sign doc, then sign (JS-compatible path)
71+
// The signer directly signs the message bytes (raw signing).
7972
func CreateSignatures(layout codec.Layout, signer Signer, ic, max uint32) (indexSignatureFormat string, indexIDs []string, err error) {
8073
layoutB64, layoutSigB64, err := SignLayoutB64(layout, signer)
8174
if err != nil {
@@ -89,7 +82,7 @@ func CreateSignatures(layout codec.Layout, signer Signer, ic, max uint32) (index
8982
return "", nil, err
9083
}
9184

92-
// Build and sign the index file (JS-style: message = index JSON string)
85+
// Build and sign the index file (message = indexB64 string)
9386
idx := BuildIndex(layoutIDs, layoutSigB64)
9487
indexB64, creatorSigB64, err := SignIndexB64(idx, signer)
9588
if err != nil {
@@ -105,12 +98,10 @@ func CreateSignatures(layout codec.Layout, signer Signer, ic, max uint32) (index
10598
return indexSignatureFormat, indexIDs, nil
10699
}
107100

108-
// CreateSignaturesWithKeyring signs layout and index using a Cosmos keyring (legacy path).
101+
// CreateSignaturesWithKeyring signs layout and index using a Cosmos keyring.
109102
// Message signed = raw bytes passed by SignLayoutB64 / SignIndexB64:
110103
// - layout: layoutB64 string
111104
// - index: index JSON string
112-
//
113-
// The verification pipeline already handles both raw and ADR-36, so this remains valid.
114105
func CreateSignaturesWithKeyring(
115106
layout codec.Layout,
116107
kr sdkkeyring.Keyring,
@@ -122,97 +113,3 @@ func CreateSignaturesWithKeyring(
122113
}
123114
return CreateSignatures(layout, signer, ic, max)
124115
}
125-
126-
// adr36SignerForKeyring creates a signer that signs ADR-36 doc bytes
127-
// for the given signer address. The "msg" we pass in is the *message*
128-
// (layoutB64, index JSON, etc.), and this helper wraps it into ADR-36.
129-
func adr36SignerForKeyring(
130-
kr sdkkeyring.Keyring,
131-
keyName string,
132-
signerAddr string,
133-
) Signer {
134-
return func(msg []byte) ([]byte, error) {
135-
// msg is the cleartext message we want to sign (e.g., layoutB64 or index JSON string)
136-
dataB64 := base64.StdEncoding.EncodeToString(msg)
137-
138-
// Build ADR-36 sign bytes: signerAddr + base64(message)
139-
doc, err := actionkeeper.MakeADR36AminoSignBytes(signerAddr, dataB64)
140-
if err != nil {
141-
return nil, err
142-
}
143-
144-
// Now sign the ADR-36 doc bytes with the keyring (direct secp256k1)
145-
return keyringpkg.SignBytes(kr, keyName, doc)
146-
}
147-
}
148-
149-
// CreateSignaturesWithKeyringADR36WithSigner creates signatures in the SAME way as the JS SDK,
150-
// allowing an explicit bech32 signer address override for ADR-36 sign bytes.
151-
func CreateSignaturesWithKeyringADR36WithSigner(
152-
layout codec.Layout,
153-
kr sdkkeyring.Keyring,
154-
keyName string,
155-
signerAddr string,
156-
ic, max uint32,
157-
) (string, []string, error) {
158-
if signerAddr == "" {
159-
addr, err := keyringpkg.GetAddress(kr, keyName)
160-
if err != nil {
161-
return "", nil, fmt.Errorf("resolve signer address: %w", err)
162-
}
163-
signerAddr = addr.String()
164-
}
165-
166-
signer := adr36SignerForKeyring(kr, keyName, signerAddr)
167-
168-
return CreateSignatures(layout, signer, ic, max)
169-
}
170-
171-
// CreateSignaturesWithKeyringADR36 creates signatures in the SAME way as the JS SDK:
172-
//
173-
// - layout: Keplr-like ADR-36 signature over layoutB64 string
174-
// - index: Keplr-like ADR-36 signature over index JSON string
175-
//
176-
// The resulting indexSignatureFormat string will match what JS produces for the same
177-
// layout, signer, ic, and max.
178-
func CreateSignaturesWithKeyringADR36(
179-
layout codec.Layout,
180-
kr sdkkeyring.Keyring,
181-
keyName string,
182-
ic, max uint32,
183-
) (string, []string, error) {
184-
return CreateSignaturesWithKeyringADR36WithSigner(layout, kr, keyName, "", ic, max)
185-
}
186-
187-
// SignADR36String signs a message string using the ADR-36 scheme that Keplr uses.
188-
// "message" must be the same string you'd pass to Keplr's signArbitrary, e.g.:
189-
// - layoutB64
190-
// - index JSON
191-
// - dataHash (base64 blake3)
192-
func SignADR36String(
193-
kr sdkkeyring.Keyring,
194-
keyName string,
195-
signerAddr string,
196-
message string,
197-
) (string, error) {
198-
// 1) message -> []byte
199-
msgBytes := []byte(message)
200-
201-
// 2) base64(UTF-8(message))
202-
dataB64 := base64.StdEncoding.EncodeToString(msgBytes)
203-
204-
// 3) Build ADR-36 sign bytes (Keplr-accurate)
205-
docBytes, err := actionkeeper.MakeADR36AminoSignBytes(signerAddr, dataB64)
206-
if err != nil {
207-
return "", fmt.Errorf("build adr36 sign bytes: %w", err)
208-
}
209-
210-
// 4) Sign with Cosmos keyring
211-
sig, err := keyringpkg.SignBytes(kr, keyName, docBytes)
212-
if err != nil {
213-
return "", fmt.Errorf("sign adr36 doc: %w", err)
214-
}
215-
216-
// 5) Wire format: base64(rsSignature)
217-
return base64.StdEncoding.EncodeToString(sig), nil
218-
}

sdk/action/client.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Client interface {
4848
// and returns CascadeMetadata (with signatures) along with price and expiration time.
4949
// Internally derives ic (random in [1..100]), max (from chain params), price (GetActionFee),
5050
// and expiration (params duration + 1h buffer). signerAddr overrides the bech32 signer
51-
// used in ADR-36 sign bytes; pass empty string to use the keyring address.
51+
// used for key selection (e.g. ICA owner key); pass empty string to use the client signer.
5252
BuildCascadeMetadataFromFile(ctx context.Context, filePath string, public bool, signerAddr string) (actiontypes.CascadeMetadata, string, string, error)
5353
// GenerateStartCascadeSignatureFromFile computes blake3(file) and signs it with the configured key; returns base64 signature.
5454
GenerateStartCascadeSignatureFromFile(ctx context.Context, filePath string) (string, error)
@@ -292,7 +292,7 @@ func (c *ClientImpl) BuildCascadeMetadataFromFile(ctx context.Context, filePath
292292
rnd, _ := crand.Int(crand.Reader, big.NewInt(100))
293293
ic := uint32(rnd.Int64() + 1) // 1..100
294294

295-
// Create signatures from the layout struct using ADR-36 scheme (JS compatible).
295+
// Create signatures from the layout struct using raw signing.
296296
if signerAddr == "" {
297297
signerAddr = c.signerAddr
298298
}
@@ -309,11 +309,10 @@ func (c *ClientImpl) BuildCascadeMetadataFromFile(ctx context.Context, filePath
309309
event.KeyMessage: "metadata signed with ICA signer address",
310310
}))
311311
}
312-
indexSignatureFormat, _, err := cascadekit.CreateSignaturesWithKeyringADR36WithSigner(
312+
indexSignatureFormat, _, err := cascadekit.CreateSignaturesWithKeyring(
313313
layout,
314314
c.keyring,
315315
keyName,
316-
signerAddr,
317316
ic,
318317
max,
319318
)
@@ -370,15 +369,14 @@ func (c *ClientImpl) GenerateStartCascadeSignatureFromFileDeprecated(ctx context
370369
return base64.StdEncoding.EncodeToString(sig), nil
371370
}
372371

373-
// GenerateStartCascadeSignatureFromFile computes blake3(file) and signs it with the configured key
374-
// using the ADR-36 scheme, matching Keplr's signArbitrary(dataHash) behavior.
375-
// Returns base64-encoded signature suitable for StartCascade.
372+
// GenerateStartCascadeSignatureFromFile computes blake3(file) and signs the base64 hash string
373+
// using raw signing (pure Go internal format). Returns base64-encoded signature suitable for StartCascade.
376374
func (c *ClientImpl) GenerateStartCascadeSignatureFromFile(ctx context.Context, filePath string) (string, error) {
377375
return c.GenerateStartCascadeSignatureFromFileWithSigner(ctx, filePath, "")
378376
}
379377

380378
// GenerateStartCascadeSignatureFromFileWithSigner computes blake3(file) and signs it with the configured key,
381-
// using the provided bech32 signer address for ADR-36 sign bytes.
379+
// using signerAddr for key selection (e.g. ICA owner key).
382380
func (c *ClientImpl) GenerateStartCascadeSignatureFromFileWithSigner(ctx context.Context, filePath string, signerAddr string) (string, error) {
383381
// Compute blake3(file), encode as base64 string
384382
h, err := utils.Blake3HashFile(filePath)
@@ -387,7 +385,6 @@ func (c *ClientImpl) GenerateStartCascadeSignatureFromFileWithSigner(ctx context
387385
}
388386
dataHashB64 := base64.StdEncoding.EncodeToString(h)
389387

390-
// Sign the dataHashB64 string using ADR-36 (same as JS / Keplr).
391388
if signerAddr == "" {
392389
signerAddr = c.signerAddr
393390
}
@@ -400,17 +397,12 @@ func (c *ClientImpl) GenerateStartCascadeSignatureFromFileWithSigner(ctx context
400397
logtrace.Info(ctx, "auth: ICA start signature", logFields)
401398
c.logger.Info(ctx, "Signing cascade start with ICA signer", "signer", signerAddr, "key_name", keyName)
402399
}
403-
sigB64, err := cascadekit.SignADR36String(
404-
c.keyring,
405-
keyName,
406-
signerAddr, // bech32 address for ADR-36 sign bytes
407-
dataHashB64,
408-
)
400+
sig, err := snkeyring.SignBytes(c.keyring, keyName, []byte(dataHashB64))
409401
if err != nil {
410-
return "", fmt.Errorf("sign adr36 hash string: %w", err)
402+
return "", fmt.Errorf("sign hash string: %w", err)
411403
}
412404

413-
return sigB64, nil
405+
return base64.StdEncoding.EncodeToString(sig), nil
414406
}
415407

416408
func (c *ClientImpl) resolveSigningKeyName(signerAddr string) (string, bool) {

0 commit comments

Comments
 (0)