Skip to content

Commit

Permalink
Merge branch 'v2' into waas_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Jul 7, 2023
2 parents 2fa85de + 45614a2 commit 74cae41
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
5 changes: 4 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ type Signature[C WalletConfig] interface {
// Recover derives the wallet configuration that the signature applies to.
// Also returns the signature's weight.
// If chainID is not provided, provider must be provided.
// If provider is not provided, EIP-1271 signatures are assumed to be valid.
// If provider is not provided, EIP-1271 signatures are assumed to be NOT valid and ignored.
// If signerSignatures is provided, it will be populated with the valid signer signatures of this signature.
Recover(ctx context.Context, digest Digest, wallet common.Address, chainID *big.Int, provider *ethrpc.Provider, signerSignatures ...SignerSignatures) (C, *big.Int, error)

// Recover a signature but only using the subdigest
RecoverSubdigest(ctx context.Context, subdigest Subdigest, provider *ethrpc.Provider, signerSignatures ...SignerSignatures) (C, *big.Int, error)

// Reduce returns an equivalent optimized signature.
Reduce(subdigest Subdigest) Signature[C]

Expand Down
22 changes: 16 additions & 6 deletions core/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ func (s *signature) Checkpoint() uint32 {
}

func (s *signature) Recover(ctx context.Context, digest core.Digest, wallet common.Address, chainID *big.Int, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
if len(signerSignatures) == 0 {
signerSignatures = []core.SignerSignatures{nil}
}

if chainID == nil {
if provider == nil {
return nil, nil, fmt.Errorf("provider is required if chain ID is not specified")
Expand All @@ -157,12 +153,19 @@ func (s *signature) Recover(ctx context.Context, digest core.Digest, wallet comm
}

subdigest := digest.Subdigest(wallet, chainID)
return s.RecoverSubdigest(ctx, subdigest, provider, signerSignatures...)
}

func (s *signature) RecoverSubdigest(ctx context.Context, subDigest core.Subdigest, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
if len(signerSignatures) == 0 {
signerSignatures = []core.SignerSignatures{nil}
}

var signers []*WalletConfigSigner
var total big.Int

for i, leaf := range s.leaves {
signer, weight, err := leaf.recover(ctx, subdigest, provider, signerSignatures[0])
signer, weight, err := leaf.recover(ctx, subDigest, provider, signerSignatures[0])
if err != nil {
return nil, nil, fmt.Errorf("unable to recover signer for leaf %v: %w", i, err)
}
Expand Down Expand Up @@ -685,6 +688,8 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
}, new(big.Int).SetUint64(uint64(l.weight)), nil

case dynamicSignatureTypeEIP1271:
effectiveWeight := l.weight

if provider != nil {
contract := ethcontract.NewContractCaller(l.address, contracts.IERC1271.ABI, provider)

Expand All @@ -706,6 +711,11 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
if magicValue != isValidSignatureMagicValue {
return nil, nil, fmt.Errorf("isValidSignature returned %v, expected %v", hexutil.Encode(magicValue[:]), hexutil.Encode(isValidSignatureMagicValue[:]))
}
} else {
// Set the effective weight to 0
// we can still get the signer address (and its corresponding weight)
// but we should not count it towards the total weight
effectiveWeight = 0
}

signerSignatures.Insert(l.address, core.SignerSignature{
Expand All @@ -717,7 +727,7 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
return &WalletConfigSigner{
Weight: l.weight,
Address: l.address,
}, new(big.Int).SetUint64(uint64(l.weight)), nil
}, new(big.Int).SetUint64(uint64(effectiveWeight)), nil

default:
return nil, nil, fmt.Errorf("unknown dynamic signature type %v", l.type_)
Expand Down
33 changes: 26 additions & 7 deletions core/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ func (s *regularSignature) Checkpoint() uint32 {
}

func (s *regularSignature) Recover(ctx context.Context, digest core.Digest, wallet common.Address, chainID *big.Int, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
if len(signerSignatures) == 0 {
signerSignatures = []core.SignerSignatures{nil}
}

if chainID == nil {
if provider == nil {
return nil, nil, fmt.Errorf("provider is required if chain ID is not specified")
Expand All @@ -176,7 +172,15 @@ func (s *regularSignature) Recover(ctx context.Context, digest core.Digest, wall
}
}

tree, weight, err := s.tree.recover(ctx, digest.Subdigest(wallet, chainID), provider, signerSignatures[0])
return s.RecoverSubdigest(ctx, digest.Subdigest(wallet, chainID), provider, signerSignatures...)
}

func (s *regularSignature) RecoverSubdigest(ctx context.Context, subDigest core.Subdigest, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
if len(signerSignatures) == 0 {
signerSignatures = []core.SignerSignatures{nil}
}

tree, weight, err := s.tree.recover(ctx, subDigest, provider, signerSignatures[0])
if err != nil {
return nil, nil, fmt.Errorf("unable to recover wallet config: %w", err)
}
Expand Down Expand Up @@ -311,11 +315,15 @@ func (s *noChainIDSignature) Checkpoint() uint32 {
}

func (s *noChainIDSignature) Recover(ctx context.Context, digest core.Digest, wallet common.Address, chainID *big.Int, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
return s.RecoverSubdigest(ctx, digest.Subdigest(wallet), provider, signerSignatures...)
}

func (s *noChainIDSignature) RecoverSubdigest(ctx context.Context, subdigest core.Subdigest, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
if len(signerSignatures) == 0 {
signerSignatures = []core.SignerSignatures{nil}
}

tree, weight, err := s.tree.recover(ctx, digest.Subdigest(wallet), provider, signerSignatures[0])
tree, weight, err := s.tree.recover(ctx, subdigest, provider, signerSignatures[0])
if err != nil {
return nil, nil, fmt.Errorf("unable to recover wallet config: %w", err)
}
Expand Down Expand Up @@ -489,6 +497,10 @@ func (s chainedSignature) Recover(ctx context.Context, digest core.Digest, walle
return config, weight, nil
}

func (s chainedSignature) RecoverSubdigest(ctx context.Context, subdigest core.Subdigest, provider *ethrpc.Provider, signerSignatures ...core.SignerSignatures) (*WalletConfig, *big.Int, error) {
return nil, nil, fmt.Errorf("chained signatures do not support recovering subdigests")
}

func (s chainedSignature) Join(subdigest core.Subdigest, other core.Signature[*WalletConfig]) (core.Signature[*WalletConfig], error) {
//TODO implement me
panic("implement me")
Expand Down Expand Up @@ -1042,6 +1054,8 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
}, new(big.Int).SetUint64(uint64(l.weight)), nil

case dynamicSignatureTypeEIP1271:
effectiveWeight := l.weight

if provider != nil {
contract := ethcontract.NewContractCaller(l.address, contracts.IERC1271.ABI, provider)

Expand All @@ -1063,6 +1077,11 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
if magicValue != isValidSignatureMagicValue {
return nil, nil, fmt.Errorf("isValidSignature returned %v, expected %v", hexutil.Encode(magicValue[:]), hexutil.Encode(isValidSignatureMagicValue[:]))
}
} else {
// Set the effective weight to 0
// we can still get the signer address (and its corresponding weight)
// but we should not count it towards the total weight
effectiveWeight = 0
}

signerSignatures.Insert(l.address, core.SignerSignature{
Expand All @@ -1074,7 +1093,7 @@ func (l *signatureTreeDynamicSignatureLeaf) recover(ctx context.Context, subdige
return &WalletConfigTreeAddressLeaf{
Weight: l.weight,
Address: l.address,
}, new(big.Int).SetUint64(uint64(l.weight)), nil
}, new(big.Int).SetUint64(uint64(effectiveWeight)), nil

default:
return nil, nil, fmt.Errorf("unknown dynamic signature type %v", l.type_)
Expand Down

0 comments on commit 74cae41

Please sign in to comment.