|
| 1 | +// Copyright 2026 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package types |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + |
| 25 | + "github.com/ethereum/go-ethereum/common" |
| 26 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 27 | + "github.com/ethereum/go-ethereum/rlp" |
| 28 | +) |
| 29 | + |
| 30 | +// EIP-8130 account_changes entry type bytes. On the wire each entry is encoded |
| 31 | +// as type_byte || rlp([body fields...]). |
| 32 | +const ( |
| 33 | + accountChangeTypeCreate = 0x00 |
| 34 | + accountChangeTypeConfig = 0x01 |
| 35 | + accountChangeTypeDelegation = 0x02 |
| 36 | +) |
| 37 | + |
| 38 | +// ActorChangeType is the operation performed by an ActorChange. The value is the |
| 39 | +// on-wire op byte; in RLP it encodes as a bare uint, in JSON as a string. |
| 40 | +type ActorChangeType uint8 |
| 41 | + |
| 42 | +const ( |
| 43 | + ActorChangeAuthorize ActorChangeType = 0x01 |
| 44 | + ActorChangeRevoke ActorChangeType = 0x02 |
| 45 | +) |
| 46 | + |
| 47 | +func (t ActorChangeType) MarshalJSON() ([]byte, error) { |
| 48 | + switch t { |
| 49 | + case ActorChangeAuthorize: |
| 50 | + return []byte(`"Authorize"`), nil |
| 51 | + case ActorChangeRevoke: |
| 52 | + return []byte(`"Revoke"`), nil |
| 53 | + default: |
| 54 | + return nil, fmt.Errorf("eip8130: invalid actor change type %d", uint8(t)) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (t *ActorChangeType) UnmarshalJSON(input []byte) error { |
| 59 | + switch string(input) { |
| 60 | + case `"Authorize"`: |
| 61 | + *t = ActorChangeAuthorize |
| 62 | + case `"Revoke"`: |
| 63 | + *t = ActorChangeRevoke |
| 64 | + default: |
| 65 | + return fmt.Errorf("eip8130: invalid actor change type %s", input) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// InitialActor is an actor installed on a newly-created account. Wire form is |
| 71 | +// rlp([actorId, authenticator]). |
| 72 | +type InitialActor struct { |
| 73 | + ActorID common.Hash `json:"actorId"` |
| 74 | + Authenticator common.Address `json:"authenticator"` |
| 75 | +} |
| 76 | + |
| 77 | +// ActorChange is a single actor authorize/revoke operation inside a ConfigChange. |
| 78 | +// Wire form is rlp([changeType, actorId, data]); data is opaque at this layer. |
| 79 | +type ActorChange struct { |
| 80 | + ChangeType ActorChangeType `json:"changeType"` |
| 81 | + ActorID common.Hash `json:"actorId"` |
| 82 | + Data hexutil.Bytes `json:"data"` |
| 83 | +} |
| 84 | + |
| 85 | +// CreateEntry is the body of an AccountChange create entry. Wire form is |
| 86 | +// rlp([userSalt, code, [InitialActor, ...]]). |
| 87 | +type CreateEntry struct { |
| 88 | + UserSalt common.Hash `json:"userSalt"` |
| 89 | + Code hexutil.Bytes `json:"code"` |
| 90 | + InitialActors []InitialActor `json:"initialActors"` |
| 91 | +} |
| 92 | + |
| 93 | +// ConfigChange is the body of an AccountChange config-change entry. Wire form is |
| 94 | +// rlp([chainId, sequence, [ActorChange, ...], auth]). |
| 95 | +type ConfigChange struct { |
| 96 | + ChainID uint64 `json:"chainId"` |
| 97 | + Sequence uint64 `json:"sequence"` |
| 98 | + ActorChanges []ActorChange `json:"actorChanges"` |
| 99 | + Auth hexutil.Bytes `json:"auth"` |
| 100 | +} |
| 101 | + |
| 102 | +// Delegation is the body of an AccountChange delegation entry. Wire form is |
| 103 | +// rlp([target]); a zero target clears the existing delegation. |
| 104 | +type Delegation struct { |
| 105 | + Target common.Address `json:"target"` |
| 106 | +} |
| 107 | + |
| 108 | +// AccountChange is a tagged-union entry inside Eip8130Tx.AccountChanges. Exactly |
| 109 | +// one of the body pointers is set. On the wire each entry is |
| 110 | +// type_byte || rlp([body fields...]); in JSON it is the body object with an added |
| 111 | +// "type" discriminator ("create" / "configChange" / "delegation"). |
| 112 | +type AccountChange struct { |
| 113 | + Create *CreateEntry |
| 114 | + ConfigChange *ConfigChange |
| 115 | + Delegation *Delegation |
| 116 | +} |
| 117 | + |
| 118 | +// EncodeRLP writes the entry as type_byte || rlp(body). |
| 119 | +func (a AccountChange) EncodeRLP(w io.Writer) error { |
| 120 | + var ( |
| 121 | + typeByte byte |
| 122 | + body interface{} |
| 123 | + ) |
| 124 | + switch { |
| 125 | + case a.Create != nil: |
| 126 | + typeByte, body = accountChangeTypeCreate, a.Create |
| 127 | + case a.ConfigChange != nil: |
| 128 | + typeByte, body = accountChangeTypeConfig, a.ConfigChange |
| 129 | + case a.Delegation != nil: |
| 130 | + typeByte, body = accountChangeTypeDelegation, a.Delegation |
| 131 | + default: |
| 132 | + return errors.New("eip8130: empty account change") |
| 133 | + } |
| 134 | + if _, err := w.Write([]byte{typeByte}); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + return rlp.Encode(w, body) |
| 138 | +} |
| 139 | + |
| 140 | +// DecodeRLP reads the type byte and decodes the matching body. Returns rlp.EOL |
| 141 | +// at the end of the enclosing list so it composes with slice decoding. |
| 142 | +func (a *AccountChange) DecodeRLP(s *rlp.Stream) error { |
| 143 | + typeByte, err := s.Bytes() |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + if len(typeByte) != 1 { |
| 148 | + return errors.New("eip8130: invalid account change type byte") |
| 149 | + } |
| 150 | + switch typeByte[0] { |
| 151 | + case accountChangeTypeCreate: |
| 152 | + a.Create = new(CreateEntry) |
| 153 | + return s.Decode(a.Create) |
| 154 | + case accountChangeTypeConfig: |
| 155 | + a.ConfigChange = new(ConfigChange) |
| 156 | + return s.Decode(a.ConfigChange) |
| 157 | + case accountChangeTypeDelegation: |
| 158 | + a.Delegation = new(Delegation) |
| 159 | + return s.Decode(a.Delegation) |
| 160 | + default: |
| 161 | + return fmt.Errorf("eip8130: invalid account change type byte 0x%x", typeByte[0]) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// MarshalJSON encodes the entry as its body object plus a "type" discriminator. |
| 166 | +func (a AccountChange) MarshalJSON() ([]byte, error) { |
| 167 | + var ( |
| 168 | + typ string |
| 169 | + body interface{} |
| 170 | + ) |
| 171 | + switch { |
| 172 | + case a.Create != nil: |
| 173 | + typ, body = "create", a.Create |
| 174 | + case a.ConfigChange != nil: |
| 175 | + typ, body = "configChange", a.ConfigChange |
| 176 | + case a.Delegation != nil: |
| 177 | + typ, body = "delegation", a.Delegation |
| 178 | + default: |
| 179 | + return nil, errors.New("eip8130: empty account change") |
| 180 | + } |
| 181 | + raw, err := json.Marshal(body) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + fields := map[string]json.RawMessage{} |
| 186 | + if err := json.Unmarshal(raw, &fields); err != nil { |
| 187 | + return nil, err |
| 188 | + } |
| 189 | + fields["type"], _ = json.Marshal(typ) |
| 190 | + return json.Marshal(fields) |
| 191 | +} |
| 192 | + |
| 193 | +// UnmarshalJSON dispatches on the "type" discriminator. |
| 194 | +func (a *AccountChange) UnmarshalJSON(input []byte) error { |
| 195 | + var tag struct { |
| 196 | + Type string `json:"type"` |
| 197 | + } |
| 198 | + if err := json.Unmarshal(input, &tag); err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + switch tag.Type { |
| 202 | + case "create": |
| 203 | + a.Create = new(CreateEntry) |
| 204 | + return json.Unmarshal(input, a.Create) |
| 205 | + case "configChange": |
| 206 | + a.ConfigChange = new(ConfigChange) |
| 207 | + return json.Unmarshal(input, a.ConfigChange) |
| 208 | + case "delegation": |
| 209 | + a.Delegation = new(Delegation) |
| 210 | + return json.Unmarshal(input, a.Delegation) |
| 211 | + default: |
| 212 | + return fmt.Errorf("eip8130: unknown account change type %q", tag.Type) |
| 213 | + } |
| 214 | +} |
0 commit comments