Class: Eth::Tx::Eip7702::Authorization
- Inherits:
-
Object
- Object
- Eth::Tx::Eip7702::Authorization
- 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
-
#address ⇒ Object
readonly
The authority addess.
-
#chain_id ⇒ Object
readonly
The EIP-155 Chain ID.
-
#nonce ⇒ Object
readonly
The transaction nonce.
-
#signature_r ⇒ Object
readonly
The signature
r
value. -
#signature_s ⇒ Object
readonly
The signature
s
value. -
#signature_y_parity ⇒ Object
readonly
The signature’s y-parity byte (not v).
Instance Method Summary collapse
-
#==(o) ⇒ Bool
Compares two authorization data objects.
-
#initialize(fields) ⇒ Authorization
constructor
Create a type-4 (EIP-7702) authorization object that can be prepared for type-4 transactions.
-
#raw ⇒ Bytes
Gets the raw serialized authorization data.
-
#sign(key) ⇒ String
Sign the authorization with a given key.
-
#unsigned_encoded ⇒ String
Encodes the unsigned authorization payload required for signing.
-
#unsigned_hash ⇒ String
Gets the sign-hash required to sign.
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
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
#address ⇒ Object (readonly)
The authority addess.
35 36 37 |
# File 'lib/eth/tx/eip7702.rb', line 35 def address @address end |
#chain_id ⇒ Object (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 |
#nonce ⇒ Object (readonly)
The transaction nonce.
38 39 40 |
# File 'lib/eth/tx/eip7702.rb', line 38 def nonce @nonce end |
#signature_r ⇒ Object (readonly)
The signature r
value.
44 45 46 |
# File 'lib/eth/tx/eip7702.rb', line 44 def signature_r @signature_r end |
#signature_s ⇒ Object (readonly)
The signature s
value.
47 48 49 |
# File 'lib/eth/tx/eip7702.rb', line 47 def signature_s @signature_s end |
#signature_y_parity ⇒ Object (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.
130 131 132 |
# File 'lib/eth/tx/eip7702.rb', line 130 def ==(o) o.class == self.class && o.state == state end |
#raw ⇒ Bytes
Gets the 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 = [] .push Util.serialize_int_to_big_endian @chain_id .push Util.hex_to_bin @address .push Util.serialize_int_to_big_endian @nonce .push Util.serialize_int_to_big_endian @signature_y_parity .push Util.serialize_int_to_big_endian @signature_r .push Util.serialize_int_to_big_endian @signature_s end |
#sign(key) ⇒ String
Sign the authorization with a given key.
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_encoded ⇒ String
Encodes the unsigned authorization payload required for signing.
97 98 99 100 101 102 103 |
# File 'lib/eth/tx/eip7702.rb', line 97 def unsigned_encoded = [] .push Util.serialize_int_to_big_endian @chain_id .push Util.hex_to_bin @address .push Util.serialize_int_to_big_endian @nonce Rlp.encode end |