Class: Eth::Tx::Eip7702::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/tx/eip7702.rb

Overview

Provides an EIP-7702 authorization that store the address to code which the signer desires to execute in the context of their EOA.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ Authorization

Create a type-4 (EIP-7702) authorization object that can be prepared for type-4 transactions. Ref: eips.ethereum.org/EIPS/eip-7702

Parameters:

  • fields (Hash)

    all necessary transaction fields.

Options Hash (fields):

  • :chain_id (Integer)

    the chain ID.

  • :address (Eth::Address)

    the authority address.

  • :nonce (Integer)

    the transaction nonce.



57
58
59
60
61
62
63
64
# File 'lib/eth/tx/eip7702.rb', line 57

def initialize(fields)
  @chain_id = fields[:chain_id].to_i
  @address = fields[:address].to_s
  @nonce = fields[:nonce].to_i
  @signature_y_parity = fields[:recovery_id]
  @signature_r = fields[:r]
  @signature_s = fields[:s]
end

Instance Attribute Details

#addressObject (readonly)

The authority addess.



35
36
37
# File 'lib/eth/tx/eip7702.rb', line 35

def address
  @address
end

#chain_idObject (readonly)

The EIP-155 Chain ID. Ref: eips.ethereum.org/EIPS/eip-155



32
33
34
# File 'lib/eth/tx/eip7702.rb', line 32

def chain_id
  @chain_id
end

#nonceObject (readonly)

The transaction nonce.



38
39
40
# File 'lib/eth/tx/eip7702.rb', line 38

def nonce
  @nonce
end

#signature_rObject (readonly)

The signature r value.



44
45
46
# File 'lib/eth/tx/eip7702.rb', line 44

def signature_r
  @signature_r
end

#signature_sObject (readonly)

The signature s value.



47
48
49
# File 'lib/eth/tx/eip7702.rb', line 47

def signature_s
  @signature_s
end

#signature_y_parityObject (readonly)

The signature’s y-parity byte (not v).



41
42
43
# File 'lib/eth/tx/eip7702.rb', line 41

def signature_y_parity
  @signature_y_parity
end

Instance Method Details

#==(o) ⇒ Bool

Compares two authorization data objects.

Returns:

  • (Bool)

    true if objects are same and share same state.



130
131
132
# File 'lib/eth/tx/eip7702.rb', line 130

def ==(o)
  o.class == self.class && o.state == state
end

#rawBytes

Gets the raw serialized authorization data.

Returns:

  • (Bytes)

    raw serialized authorization data.



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/eth/tx/eip7702.rb', line 115

def raw
  authorization_data = []
  authorization_data.push Util.serialize_int_to_big_endian @chain_id
  authorization_data.push Util.hex_to_bin @address
  authorization_data.push Util.serialize_int_to_big_endian @nonce

  authorization_data.push Util.serialize_int_to_big_endian @signature_y_parity
  authorization_data.push Util.serialize_int_to_big_endian @signature_r
  authorization_data.push Util.serialize_int_to_big_endian @signature_s
  authorization_data
end

#sign(key) ⇒ String

Sign the authorization with a given key.

Parameters:

  • key (Eth::Key)

    the key-pair to use for signing.

Returns:

  • (String)

    a transaction hash.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/eth/tx/eip7702.rb', line 72

def sign(key)
  if Tx.signed? self
    raise Signature::SignatureError, "Authorization is already signed!"
  end

  # ensure the sender address matches the given key
  unless @address.nil? or @address.empty?
    signer_address = Tx.sanitize_address key.address.to_s
    from_address = Tx.sanitize_address @address
    raise Signature::SignatureError, "Signer does not match sender" unless signer_address == from_address
  end

  # sign a keccak hash of the unsigned, encoded transaction
  signature = key.sign(unsigned_hash, @chain_id)
  r, s, v = Signature.dissect signature
  recovery_id = Chain.to_recovery_id v.to_i(16), @chain_id
  @signature_y_parity = recovery_id
  @signature_r = r
  @signature_s = s
  return hash
end

#unsigned_encodedString

Encodes the unsigned authorization payload required for signing.

Returns:

  • (String)

    an RLP-encoded, unsigned, enveloped EIP-7702 transaction.



97
98
99
100
101
102
103
# File 'lib/eth/tx/eip7702.rb', line 97

def unsigned_encoded
  authorization_data = []
  authorization_data.push Util.serialize_int_to_big_endian @chain_id
  authorization_data.push Util.hex_to_bin @address
  authorization_data.push Util.serialize_int_to_big_endian @nonce
  Rlp.encode authorization_data
end

#unsigned_hashString

Gets the sign-hash required to sign.

Returns:

  • (String)

    a Keccak-256 hash.



108
109
110
# File 'lib/eth/tx/eip7702.rb', line 108

def unsigned_hash
  Util.keccak256 unsigned_encoded
end