Skip to content

Commit b0e9010

Browse files
committed
go/callformat: extract encodeCallEncryptedX25519DeoxysII
1 parent 60ecfae commit b0e9010

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

client-sdk/go/callformat/callformat.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ type metaEncryptedX25519DeoxysII struct {
2727
pk *x25519.PublicKey
2828
}
2929

30+
func encodeCallEncryptedX25519DeoxysII(call *types.Call, pk *x25519.PublicKey, sk *x25519.PrivateKey, nonce [deoxysii.NonceSize]byte, cfg *EncodeConfig) (*types.Call, *metaEncryptedX25519DeoxysII) {
31+
// Seal serialized plain call.
32+
rawCall := cbor.Marshal(call)
33+
sealedCall := mraeDeoxysii.Box.Seal(nil, nonce[:], rawCall, nil, &cfg.PublicKey.PublicKey, sk)
34+
35+
encoded := &types.Call{
36+
Format: types.CallFormatEncryptedX25519DeoxysII,
37+
Method: "",
38+
Body: cbor.Marshal(&types.CallEnvelopeX25519DeoxysII{
39+
Pk: *pk,
40+
Nonce: nonce,
41+
Epoch: cfg.Epoch,
42+
Data: sealedCall,
43+
}),
44+
ReadOnly: call.ReadOnly,
45+
}
46+
meta := &metaEncryptedX25519DeoxysII{
47+
sk: sk,
48+
pk: &cfg.PublicKey.PublicKey,
49+
}
50+
return encoded, meta
51+
}
52+
3053
// EncodeCall encodes a call based on its configured call format.
3154
//
3255
// It returns the encoded call and any metadata needed to successfully decode the result.
@@ -52,25 +75,7 @@ func EncodeCall(call *types.Call, cf types.CallFormat, cfg *EncodeConfig) (*type
5275
return nil, nil, fmt.Errorf("callformat: failed to generate random nonce: %w", err)
5376
}
5477

55-
// Seal serialized plain call.
56-
rawCall := cbor.Marshal(call)
57-
sealedCall := mraeDeoxysii.Box.Seal(nil, nonce[:], rawCall, nil, &cfg.PublicKey.PublicKey, sk)
58-
59-
encoded := &types.Call{
60-
Format: types.CallFormatEncryptedX25519DeoxysII,
61-
Method: "",
62-
Body: cbor.Marshal(&types.CallEnvelopeX25519DeoxysII{
63-
Pk: *pk,
64-
Nonce: nonce,
65-
Epoch: cfg.Epoch,
66-
Data: sealedCall,
67-
}),
68-
ReadOnly: call.ReadOnly,
69-
}
70-
meta := &metaEncryptedX25519DeoxysII{
71-
sk: sk,
72-
pk: &cfg.PublicKey.PublicKey,
73-
}
78+
encoded, meta := encodeCallEncryptedX25519DeoxysII(call, pk, sk, nonce, cfg)
7479
return encoded, meta, nil
7580
default:
7681
return nil, nil, fmt.Errorf("callformat: unsupported call format: %s", cf)

0 commit comments

Comments
 (0)