@@ -22,12 +22,49 @@ import (
22
22
"math/big"
23
23
"os"
24
24
"reflect"
25
+ "sync"
25
26
"time"
26
27
27
28
"github.com/blinklabs-io/gouroboros/cbor"
28
29
"github.com/blinklabs-io/gouroboros/ledger/common"
29
30
)
30
31
32
+ // Network stake address headers
33
+ var (
34
+ stakeHeaderRegistry = map [string ]struct {
35
+ Base , Script byte
36
+ }{
37
+ "Mainnet" : {0xE0 , 0xE1 },
38
+ "Testnet" : {0xF0 , 0xF1 },
39
+ }
40
+ stakeHeaderMutex sync.RWMutex
41
+ )
42
+
43
+ // RegisterStakeHeaders allows runtime registration of network address headers
44
+ func RegisterStakeHeaders (networkId string , baseHeader , scriptHeader byte ) {
45
+ stakeHeaderMutex .Lock ()
46
+ defer stakeHeaderMutex .Unlock ()
47
+ stakeHeaderRegistry [networkId ] = struct { Base , Script byte }{
48
+ Base : baseHeader ,
49
+ Script : scriptHeader ,
50
+ }
51
+ }
52
+
53
+ func getStakeAddressHeader (networkId string , isScript bool ) (byte , error ) {
54
+ stakeHeaderMutex .RLock ()
55
+ defer stakeHeaderMutex .RUnlock ()
56
+
57
+ headers , exists := stakeHeaderRegistry [networkId ]
58
+ if ! exists {
59
+ return 0 , errors .New ("network not registered in stake header registry" )
60
+ }
61
+
62
+ if isScript {
63
+ return headers .Script , nil
64
+ }
65
+ return headers .Base , nil
66
+ }
67
+
31
68
type ShelleyGenesis struct {
32
69
cbor.StructAsArray
33
70
SystemStart time.Time `json:"systemStart"`
@@ -232,7 +269,6 @@ func (g *ShelleyGenesis) GenesisUtxos() ([]common.Utxo, error) {
232
269
return ret , nil
233
270
}
234
271
235
- // InitialPools returns all pools and their delegators from the genesis data
236
272
func (g * ShelleyGenesis ) InitialPools () (map [string ]common.PoolRegistrationCertificate , map [string ][]common.Address , error ) {
237
273
pools := make (map [string ]common.PoolRegistrationCertificate )
238
274
poolStake := make (map [string ][]common.Address )
@@ -241,6 +277,11 @@ func (g *ShelleyGenesis) InitialPools() (map[string]common.PoolRegistrationCerti
241
277
return pools , poolStake , nil
242
278
}
243
279
280
+ headerByte , err := getStakeAddressHeader (g .NetworkId , true )
281
+ if err != nil {
282
+ return nil , nil , errors .New ("failed to get stake address header" )
283
+ }
284
+
244
285
for stakeAddr , poolId := range g .Staking .Stake {
245
286
if len (stakeAddr ) != 56 {
246
287
return nil , nil , errors .New ("invalid stake address length" )
@@ -251,7 +292,7 @@ func (g *ShelleyGenesis) InitialPools() (map[string]common.PoolRegistrationCerti
251
292
return nil , nil , errors .New ("failed to decode stake key" )
252
293
}
253
294
254
- stakeAddrBytes := append ([]byte {0xE1 }, stakeKeyBytes ... )
295
+ stakeAddrBytes := append ([]byte {headerByte }, stakeKeyBytes ... )
255
296
addr , err := common .NewAddressFromBytes (stakeAddrBytes )
256
297
if err != nil {
257
298
return nil , nil , errors .New ("failed to create stake address" )
@@ -286,7 +327,6 @@ func (g *ShelleyGenesis) InitialPools() (map[string]common.PoolRegistrationCerti
286
327
return pools , poolStake , nil
287
328
}
288
329
289
- // PoolById returns a specific pool by its ID along with its delegators
290
330
func (g * ShelleyGenesis ) PoolById (poolId string ) (* common.PoolRegistrationCertificate , []common.Address , error ) {
291
331
if len (poolId ) != 56 {
292
332
return nil , nil , errors .New ("invalid pool ID length" )
@@ -297,9 +337,9 @@ func (g *ShelleyGenesis) PoolById(poolId string) (*common.PoolRegistrationCertif
297
337
return nil , nil , errors .New ("pool not found" )
298
338
}
299
339
300
- operatorBytes , err := hex . DecodeString ( poolId )
340
+ headerByte , err := getStakeAddressHeader ( g . NetworkId , true )
301
341
if err != nil {
302
- return nil , nil , errors .New ("failed to decode pool operator key " )
342
+ return nil , nil , errors .New ("failed to get stake address header " )
303
343
}
304
344
305
345
var delegators []common.Address
@@ -314,7 +354,7 @@ func (g *ShelleyGenesis) PoolById(poolId string) (*common.PoolRegistrationCertif
314
354
return nil , nil , errors .New ("failed to decode stake key" )
315
355
}
316
356
317
- stakeAddrBytes := append ([]byte {0xE1 }, stakeKeyBytes ... )
357
+ stakeAddrBytes := append ([]byte {headerByte }, stakeKeyBytes ... )
318
358
addr , err := common .NewAddressFromBytes (stakeAddrBytes )
319
359
if err != nil {
320
360
return nil , nil , errors .New ("failed to create stake address" )
@@ -324,6 +364,11 @@ func (g *ShelleyGenesis) PoolById(poolId string) (*common.PoolRegistrationCertif
324
364
}
325
365
}
326
366
367
+ operatorBytes , err := hex .DecodeString (poolId )
368
+ if err != nil {
369
+ return nil , nil , errors .New ("failed to decode pool operator key" )
370
+ }
371
+
327
372
return & common.PoolRegistrationCertificate {
328
373
Operator : common .Blake2b224 (operatorBytes ),
329
374
VrfKeyHash : pool .VrfKeyHash ,
0 commit comments