@@ -22,49 +22,12 @@ import (
2222 "math/big"
2323 "os"
2424 "reflect"
25- "sync"
2625 "time"
2726
2827 "github.com/blinklabs-io/gouroboros/cbor"
2928 "github.com/blinklabs-io/gouroboros/ledger/common"
3029)
3130
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-
6831type ShelleyGenesis struct {
6932 cbor.StructAsArray
7033 SystemStart time.Time `json:"systemStart"`
@@ -269,6 +232,17 @@ func (g *ShelleyGenesis) GenesisUtxos() ([]common.Utxo, error) {
269232 return ret , nil
270233}
271234
235+ func (g * ShelleyGenesis ) getNetworkId () (uint8 , error ) {
236+ switch g .NetworkId {
237+ case "Mainnet" :
238+ return common .AddressNetworkMainnet , nil
239+ case "Testnet" :
240+ return common .AddressNetworkTestnet , nil
241+ default :
242+ return 0 , errors .New ("unknown network ID" )
243+ }
244+ }
245+
272246func (g * ShelleyGenesis ) InitialPools () (map [string ]common.PoolRegistrationCertificate , map [string ][]common.Address , error ) {
273247 pools := make (map [string ]common.PoolRegistrationCertificate )
274248 poolStake := make (map [string ][]common.Address )
@@ -277,38 +251,36 @@ func (g *ShelleyGenesis) InitialPools() (map[string]common.PoolRegistrationCerti
277251 return pools , poolStake , nil
278252 }
279253
280- headerByte , err := getStakeAddressHeader ( g . NetworkId , true )
254+ networkId , err := g . getNetworkId ( )
281255 if err != nil {
282- return nil , nil , errors . New ( "failed to get stake address header" )
256+ return nil , nil , err
283257 }
284258
259+ // Process all stake addresses
285260 for stakeAddr , poolId := range g .Staking .Stake {
286- if len (stakeAddr ) != 56 {
287- return nil , nil , errors .New ("invalid stake address length" )
288- }
289-
290- stakeKeyBytes , err := hex .DecodeString (stakeAddr )
261+ stakeKey , err := hex .DecodeString (stakeAddr )
291262 if err != nil {
292263 return nil , nil , errors .New ("failed to decode stake key" )
293264 }
294265
295- stakeAddrBytes := append ([]byte {headerByte }, stakeKeyBytes ... )
296- addr , err := common .NewAddressFromBytes (stakeAddrBytes )
266+ addr , err := common .NewAddressFromParts (
267+ common .AddressTypeNoneScript , // Script stake address
268+ networkId ,
269+ nil ,
270+ stakeKey ,
271+ )
297272 if err != nil {
298- return nil , nil , errors .New ("failed to create stake address" )
273+ return nil , nil , errors .New ("failed to create address" )
299274 }
300275
301276 poolStake [poolId ] = append (poolStake [poolId ], addr )
302277 }
303278
279+ // Process all stake pools
304280 for poolId , pool := range g .Staking .Pools {
305- if len (poolId ) != 56 {
306- return nil , nil , errors .New ("invalid pool ID length" )
307- }
308-
309281 operatorBytes , err := hex .DecodeString (poolId )
310282 if err != nil {
311- return nil , nil , errors .New ("failed to decode pool operator key " )
283+ return nil , nil , errors .New ("failed to decode pool ID " )
312284 }
313285
314286 pools [poolId ] = common.PoolRegistrationCertificate {
@@ -334,30 +306,30 @@ func (g *ShelleyGenesis) PoolById(poolId string) (*common.PoolRegistrationCertif
334306
335307 pool , exists := g .Staking .Pools [poolId ]
336308 if ! exists {
337- return nil , nil , errors .New ("pool not found" )
309+ return nil , nil , errors .New ("pool not found" )
338310 }
339311
340- headerByte , err := getStakeAddressHeader ( g . NetworkId , true )
312+ networkId , err := g . getNetworkId ( )
341313 if err != nil {
342- return nil , nil , errors . New ( "failed to get stake address header" )
314+ return nil , nil , err
343315 }
344316
345317 var delegators []common.Address
346318 for stakeAddr , pId := range g .Staking .Stake {
347319 if pId == poolId {
348- if len (stakeAddr ) != 56 {
349- return nil , nil , errors .New ("invalid stake address length" )
350- }
351-
352- stakeKeyBytes , err := hex .DecodeString (stakeAddr )
320+ stakeKey , err := hex .DecodeString (stakeAddr )
353321 if err != nil {
354322 return nil , nil , errors .New ("failed to decode stake key" )
355323 }
356324
357- stakeAddrBytes := append ([]byte {headerByte }, stakeKeyBytes ... )
358- addr , err := common .NewAddressFromBytes (stakeAddrBytes )
325+ addr , err := common .NewAddressFromParts (
326+ common .AddressTypeNoneScript ,
327+ networkId ,
328+ nil ,
329+ stakeKey ,
330+ )
359331 if err != nil {
360- return nil , nil , errors .New ("failed to create stake address" )
332+ return nil , nil , errors .New ("failed to create address" )
361333 }
362334
363335 delegators = append (delegators , addr )
0 commit comments