Skip to content

Commit a5b038d

Browse files
authored
Preserve timed refund preimage (#352)
1 parent f485629 commit a5b038d

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

intent_config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ func TestCreateIntentConfigurationWithTimedRefundSapient(t *testing.T) {
359359
sapientLeaf := findSapientSignerLeaf(config.Tree, timedRefundSigner)
360360
require.NotNil(t, sapientLeaf)
361361
require.Equal(t, expectedSapientImageHash, sapientLeaf.ImageHash_.Hash)
362+
preimage, ok := sapientLeaf.ImageHash_.Preimage.(*sequence.TimedRefundSapientImageHashPreimage)
363+
require.True(t, ok)
364+
require.Equal(t, destination, preimage.Destination)
365+
require.Equal(t, uint64(1_750_000_000), preimage.UnlockTimestamp)
366+
require.Equal(t, expectedSapientImageHash, preimage.ImageHash().Hash)
362367

363368
plainConfig, err := sequence.CreateIntentConfiguration(mainSigner, []*v3.CallsPayload{&payload}, 0, nil)
364369
require.NoError(t, err)
@@ -406,6 +411,20 @@ func TestCreateIntentConfigurationWithTimedRefundSapient_ZeroWeight(t *testing.T
406411
require.EqualError(t, err, "timed refund sapient signer weight is zero")
407412
}
408413

414+
func TestTimedRefundSapientImageHash(t *testing.T) {
415+
destination := common.HexToAddress("0x4444444444444444444444444444444444444444")
416+
417+
imageHash, err := sequence.TimedRefundSapientImageHash(destination, 1_750_000_000)
418+
require.NoError(t, err)
419+
require.Equal(t, common.HexToHash("0x577e11f2280512fff4541fc08cc7eb98357bdcff482db5634db7327e3c97ba58"), imageHash.Hash)
420+
421+
preimage, ok := imageHash.Preimage.(*sequence.TimedRefundSapientImageHashPreimage)
422+
require.True(t, ok)
423+
require.Equal(t, destination, preimage.Destination)
424+
require.Equal(t, uint64(1_750_000_000), preimage.UnlockTimestamp)
425+
require.Equal(t, imageHash.Hash, preimage.ImageHash().Hash)
426+
}
427+
409428
func TestGetIntentConfigurationSignature(t *testing.T) {
410429
// Create test wallets
411430
eoa1, err := ethwallet.NewWalletFromRandomEntropy()

intent_config_timed_refund.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ type TimedRefundIntentConfigurationSigner struct {
2222
Weight uint8
2323
}
2424

25+
// TimedRefundSapientImageHashPreimage is the typed preimage for a timed-refund sapient signer.
26+
// It preserves the refund destination and unlock timestamp alongside the irreversible hash.
27+
type TimedRefundSapientImageHashPreimage struct {
28+
Destination common.Address
29+
UnlockTimestamp uint64
30+
}
31+
32+
func (p *TimedRefundSapientImageHashPreimage) ImageHash() core.ImageHash {
33+
if p == nil {
34+
return core.ImageHash{}
35+
}
36+
37+
imageHash, err := TimedRefundSapientImageHash(p.Destination, p.UnlockTimestamp)
38+
if err != nil {
39+
panic(fmt.Errorf("timed refund sapient image hash preimage: %w", err))
40+
}
41+
42+
return imageHash
43+
}
44+
2545
// CreateIntentConfigurationWithTimedRefundSapient creates an intent configuration that includes
2646
// a timed-refund sapient signer leaf in addition to the default any-address subdigests.
2747
func CreateIntentConfigurationWithTimedRefundSapient(
@@ -51,7 +71,7 @@ func createTimedRefundSapientSignerLeaf(signer TimedRefundIntentConfigurationSig
5171
return nil, fmt.Errorf("timed refund sapient signer weight is zero")
5272
}
5373

54-
imageHash, err := timedRefundSapientImageHash(signer.Destination, signer.UnlockTimestamp)
74+
imageHash, err := TimedRefundSapientImageHash(signer.Destination, signer.UnlockTimestamp)
5575
if err != nil {
5676
return nil, err
5777
}
@@ -86,7 +106,9 @@ func mustTimedRefundSapientImageHashArguments() abi.Arguments {
86106
}
87107
}
88108

89-
func timedRefundSapientImageHash(destination common.Address, unlockTimestamp uint64) (core.ImageHash, error) {
109+
// TimedRefundSapientImageHash computes the image hash for a timed-refund sapient signer and
110+
// includes the recoverable typed preimage in the returned core.ImageHash.
111+
func TimedRefundSapientImageHash(destination common.Address, unlockTimestamp uint64) (core.ImageHash, error) {
90112
encoded, err := timedRefundSapientImageHashArguments.Pack(
91113
"timed-refund",
92114
destination,
@@ -96,5 +118,13 @@ func timedRefundSapientImageHash(destination common.Address, unlockTimestamp uin
96118
return core.ImageHash{}, fmt.Errorf("failed to ABI pack timed refund sapient image hash: %w", err)
97119
}
98120

99-
return core.ImageHash{Hash: crypto.Keccak256Hash(encoded)}, nil
121+
preimage := &TimedRefundSapientImageHashPreimage{
122+
Destination: destination,
123+
UnlockTimestamp: unlockTimestamp,
124+
}
125+
126+
return core.ImageHash{
127+
Hash: crypto.Keccak256Hash(encoded),
128+
Preimage: preimage,
129+
}, nil
100130
}

0 commit comments

Comments
 (0)