Skip to content

Commit

Permalink
refactor: SystemScripts use functional option
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Dec 9, 2020
1 parent 6f70c91 commit 9ad024b
Showing 1 changed file with 94 additions and 30 deletions.
124 changes: 94 additions & 30 deletions utils/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
AnyoneCanPayCodeHashOnAggron = "0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356"
)

type Option func(*SystemScripts)
type SystemScriptCell struct {
CellHash types.Hash
OutPoint *types.OutPoint
Expand All @@ -21,48 +22,111 @@ type SystemScripts struct {
SecpSingleSigCell *SystemScriptCell
SecpMultiSigCell *SystemScriptCell
DaoCell *SystemScriptCell
ACPCell *SystemScriptCell
SUDTCell *SystemScriptCell
}

func NewSystemScripts(client rpc.Client) (*SystemScripts, error) {
genesis, err := client.GetBlockByNumber(context.Background(), 0)
if err != nil {
return nil, err
func secpSingleSigCell() *SystemScriptCell {
return &SystemScriptCell{
CellHash: types.HexToHash("0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8"),
OutPoint: &types.OutPoint{
TxHash: types.HexToHash("0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c"),
Index: 0,
},
}
}

secpHash, err := genesis.Transactions[0].Outputs[1].Type.Hash()
if err != nil {
return nil, err
}
multiSigHash, err := genesis.Transactions[0].Outputs[4].Type.Hash()
if err != nil {
return nil, err
func secpMultiSigCell() *SystemScriptCell {
return &SystemScriptCell{
CellHash: types.HexToHash("0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8"),
OutPoint: &types.OutPoint{
TxHash: types.HexToHash("0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c"),
Index: 1,
},
}
daoHash, err := genesis.Transactions[0].Outputs[2].Type.Hash()
if err != nil {
return nil, err
}

func daoCell() *SystemScriptCell {
return &SystemScriptCell{
CellHash: types.HexToHash("0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e"),
OutPoint: &types.OutPoint{
TxHash: types.HexToHash("0xe2fb199810d49a4d8beec56718ba2593b665db9d52299a0f9e6e75416d73ff5c"),
Index: 2,
},
}
}

return &SystemScripts{
SecpSingleSigCell: &SystemScriptCell{
CellHash: secpHash,
func acpCell(chain string) *SystemScriptCell {
if chain == "ckb" {
return &SystemScriptCell{
CellHash: types.HexToHash("0xd369597ff47f29fbc0d47d2e3775370d1250b85140c670e4718af712983a2354"),
OutPoint: &types.OutPoint{
TxHash: genesis.Transactions[1].Hash,
TxHash: types.HexToHash("0x4153a2014952d7cac45f285ce9a7c5c0c0e1b21f2d378b82ac1433cb11c25c4d"),
Index: 0,
},
},
SecpMultiSigCell: &SystemScriptCell{
CellHash: multiSigHash,
}
} else {
return &SystemScriptCell{
CellHash: types.HexToHash("0x3419a1c09eb2567f6552ee7a8ecffd64155cffe0f1796e6e61ec088d740c1356"),
OutPoint: &types.OutPoint{
TxHash: genesis.Transactions[1].Hash,
Index: 1,
TxHash: types.HexToHash("0xec26b0f85ed839ece5f11c4c4e837ec359f5adc4420410f6453b1f6b60fb96a6"),
Index: 0,
},
},
DaoCell: &SystemScriptCell{
CellHash: daoHash,
}
}
}

func sudtCell(chain string) *SystemScriptCell {
if chain == "ckb" {
return &SystemScriptCell{
CellHash: types.HexToHash("0x5e7a36a77e68eecc013dfa2fe6a23f3b6c344b04005808694ae6dd45eea4cfd5"),
OutPoint: &types.OutPoint{
TxHash: genesis.Transactions[0].Hash,
Index: 2,
TxHash: types.HexToHash("0xc7813f6a415144643970c2e88e0bb6ca6a8edc5dd7c1022746f628284a9936d5"),
Index: 0,
},
},
}, nil
}
} else {
return &SystemScriptCell{
CellHash: types.HexToHash("0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212"),
OutPoint: &types.OutPoint{
TxHash: types.HexToHash("0xc1b2ae129fad7465aaa9acc9785f842ba3e6e8b8051d899defa89f5508a77958"),
Index: 0,
},
}
}
}

// NewSystemScripts returns a SystemScripts object
func NewSystemScripts(client rpc.Client, options ...Option) (*SystemScripts, error) {
info, err := client.GetBlockchainInfo(context.Background())
if err != nil {
return nil, err
}
scripts := &SystemScripts{
SecpSingleSigCell: secpSingleSigCell(),
SecpMultiSigCell: secpMultiSigCell(),
DaoCell: daoCell(),
ACPCell: acpCell(info.Chain),
SUDTCell: sudtCell(info.Chain),
}

for _, option := range options {
option(scripts)
}

return scripts, nil
}

// ACPCell set a custom acp system script cell to SystemScripts
func ACPCell(acpCell *SystemScriptCell) Option {
return func(s *SystemScripts) {
s.ACPCell = acpCell
}
}

// SUDTCell set a custom sudt system script cell to SystemScripts
func SUDTCell(sudtCell *SystemScriptCell) Option {
return func(s *SystemScripts) {
s.SUDTCell = sudtCell
}
}

0 comments on commit 9ad024b

Please sign in to comment.