Skip to content

Commit

Permalink
Merge pull request #147 from commerceblock/chore/cleanup
Browse files Browse the repository at this point in the history
chore: cleanup the chaincodes and scripts
  • Loading branch information
tomt1664 authored Feb 5, 2024
2 parents 0943f54 + 453e0a4 commit 0c1cc62
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 74 deletions.
2 changes: 1 addition & 1 deletion attestation/attestclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func newNonMultisigAttestClient(config *confpkg.Config, isSigner bool, wif *btcu
if err != nil {
log.Errorf("Invalid public key %v", err)
}
chaincode, _ := hex.DecodeString(config.InitChaincodes()[0])
chaincode, _ := hex.DecodeString(config.InitChaincode())
return &AttestClient{
MainClient: config.MainClient(),
MainChainCfg: config.MainChainCfg(),
Expand Down
6 changes: 3 additions & 3 deletions conf_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"staychain":
{
"initTx": "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1",
"initScript": "51210381324c14a482646e9ad7cf82372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae",
"initChaincodes": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa,0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"initPK": "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLz",
"initChaincode": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"topupAddress": "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB",
"topupScript": "512103e52cf15e0a5cf6612314f077bb65cf9a6596b76c0fcb34b682f673a8314c7b332102f3a78a7bd6cf01c56312e7e828bef74134dfb109e59afd088526212d96518e7552ae",
"topupPK": "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLa",
"regtest": "1"
},
"main":
Expand Down
12 changes: 3 additions & 9 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
{
"staychain": {
"initTx": "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1",
"initScript": "51210381324c14a482646e9ad7cf82372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae",
"initChaincodes": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa,0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"initChaincode": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"topupAddress": "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB",
"topupScript": "51210381324c14a482646e9ad7cf92372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae",
"regtest": "1"
},
"main": {
Expand Down Expand Up @@ -66,10 +64,8 @@ The `staychain` category is compulsory and can be set from either .conf file or

- `staychain` : configuration options for staychain parameters
- `initTx` : initial transaction sets the state for the staychain
- `initScript` : initial script used to derive subsequent staychain addresses
- `initChaincodes`: chaincodes of init script pubkeys used to derive subsequent staychain addresses
- `initChaincode`: chaincode of init script pubkeys used to derive staychain address
- `topupAddress` : address to topup the mainstay service
- `topupScript` : script that requires signing for the topup


Several other subcategories become compulsory only if the base category exists in the `.conf` file.
Expand Down Expand Up @@ -114,10 +110,8 @@ Currently only parameters in the `staychain` category can be parsed through comm

These command line arguments are:
- `tx` : argument for initTx as above
- `script` : argument for initScript as above
- `chaincodes`: argument for initChaincodes as above
- `chaincode`: argument for initChaincode as above
- `addrTopup` : argument for topupAddress as above
- `scriptTopup` : argument for topupScript as above

### Env Variables

Expand Down
6 changes: 3 additions & 3 deletions config/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"staychain":
{
"initTx": "MAINSTAY_INIT_TX",
"initScript": "MAINSTAY_INIT_SCRIPT",
"initChaincodes": "MAINSTAY_INIT_CHAINCODES",
"initPK": "MAINSTAY_INIT_PK",
"initChaincode": "MAINSTAY_INIT_CHAINCODE",
"topupAddress": "MAINSTAY_TOPUP_ADDRESS",
"topupScript": "MAINSTAY_TOPUP_SCRIPT"
"topupPK": "MAINSTAY_TOPUP_PK"
},
"main":
{
Expand Down
43 changes: 9 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ const (
StaychainName = "staychain"
StaychainRegtestName = "regtest"
StaychainInitTxName = "initTx"
StaychainInitScriptName = "initScript"
StaychainInitPkName = "initPK"
StaychainInitChaincodesName = "initChaincodes"
StaychainInitChaincodeName = "initChaincode"
StaychainInitPublicKeyName = "initPublicKey"
StaychainTopupAddressName = "topupAddress"
StaychainTopupScriptName = "topupScript"
StaychainTopupPkName = "topupPK"
StayChainTopupChaincodesName = "topupChaincodes"
)

// Config struct
Expand All @@ -45,13 +42,10 @@ type Config struct {
regtest bool
initTX string
initPK string
initScript string
initPublicKey string
initChaincodes []string
initChaincode string
topupAddress string
topupScript string
topupPK string
topupChaincodes []string

// additional parameter categories
signerConfig SignerConfig
Expand Down Expand Up @@ -136,13 +130,13 @@ func (c *Config) SetInitPK(pk string) {
}

// Get Init Chaincodes
func (c *Config) InitChaincodes() []string {
return c.initChaincodes
func (c *Config) InitChaincode() string {
return c.initChaincode
}

// Set Init Chaincodes
func (c *Config) SetInitChaincodes(chaincodes []string) {
c.initChaincodes = chaincodes
func (c *Config) SetInitChaincode(chaincode string) {
c.initChaincode = chaincode
}

// Get init Public key
Expand All @@ -155,16 +149,6 @@ func (c *Config) SetInitPublicKey(publickey string) {
c.initPublicKey = publickey
}

// Get Topup Chaincodes
func (c *Config) TopupChaincodes() []string {
return c.topupChaincodes
}

// Set Topup Chaincodes
func (c *Config) SetTopupChaincodes(chaincodes []string) {
c.topupChaincodes = chaincodes
}

// Get topup PK
func (c *Config) TopupPK() string {
return c.topupPK
Expand Down Expand Up @@ -223,16 +207,8 @@ func NewConfig(customConf ...[]byte) (*Config, error) {
topupPKStr := TryGetParamFromConf(StaychainName, StaychainTopupPkName, conf)
initPublicKeyStr := TryGetParamFromConf(StaychainName, StaychainInitPublicKeyName, conf)

initChaincodesStr := TryGetParamFromConf(StaychainName, StaychainInitChaincodesName, conf)
initChaincodes := strings.Split(initChaincodesStr, ",") // string to string slice
for i := range initChaincodes { // trim whitespace
initChaincodes[i] = strings.TrimSpace(initChaincodes[i])
}
topupChaincodesStr := TryGetParamFromConf(StaychainName, StayChainTopupChaincodesName, conf)
topupChaincodes := strings.Split(topupChaincodesStr, ",") // string to string slice
for i := range topupChaincodes { // trim whitespace
topupChaincodes[i] = strings.TrimSpace(topupChaincodes[i])
}
initChaincodeStr := TryGetParamFromConf(StaychainName, StaychainInitChaincodeName, conf)
initChaincode := strings.TrimSpace(initChaincodeStr) // trim whitespace

return &Config{
mainClient: mainClient,
Expand All @@ -241,10 +217,9 @@ func NewConfig(customConf ...[]byte) (*Config, error) {
initTX: initTxStr,
initPK: initPKStr,
initPublicKey: initPublicKeyStr,
initChaincodes: initChaincodes,
initChaincode: initChaincode,
topupAddress: topupAddrStr,
topupPK: topupPKStr,
topupChaincodes: topupChaincodes,
signerConfig: signerConfig,
dbConfig: dbConnectivity,
feesConfig: feesConfig,
Expand Down
18 changes: 6 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,9 @@ func TestConfigStaychain(t *testing.T) {
},
"staychain": {
"initTx": "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1",
"initScript": "51210381324c14a482646e9ad7cf82372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae",
"initChaincodes": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa ,0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa ",
"initChaincode": "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"initPK": "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLz",
"topupAddress": "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB",
"topupScript": "51210381324c14a482646e9ad7cf92372021e5ecb9a7e1b67ee168dddf1e97dafe40af210376c091faaeb6bb3b74e0568db5dd499746d99437758a5cb1e60ab38f02e279c352ae",
"topupChaincodes": " 0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa, 0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"topupPK": "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLa",
"regtest": "1"
}
Expand All @@ -254,12 +251,9 @@ func TestConfigStaychain(t *testing.T) {

assert.Equal(t, "87e56bda501ba6a022f12e178e9f1ac03fb2c07f04e1dfa62ac9e1d83cd840e1", config.InitTx())
assert.Equal(t, "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLz", config.InitPK())
assert.Equal(t, []string{"0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa"}, config.InitChaincodes())
assert.Equal(t, "0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa", config.InitChaincode())
assert.Equal(t, "2MxBi6eodnuoVCw8McGrf1nuoVhastqoBXB", config.TopupAddress())
assert.Equal(t, "cQca2KvrBnJJUCYa2tD4RXhiQshWLNMSK2A96ZKWo1SZkHhh3YLa", config.TopupPK())
assert.Equal(t, []string{"0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa",
"0a090f710e47968aee906804f211cf10cde9a11e14908ca0f78cc55dd190ceaa"}, config.TopupChaincodes())
assert.Equal(t, true, config.Regtest())

config.SetRegtest(false)
Expand All @@ -271,17 +265,17 @@ func TestConfigStaychain(t *testing.T) {
config.SetInitPK("PKPKPK")
assert.Equal(t, "PKPKPK", config.InitPK())

config.SetInitChaincodes([]string{"chaincode1", "chaincode2"})
assert.Equal(t, []string{"chaincode1", "chaincode2"}, config.InitChaincodes())
config.SetInitChaincode("chaincode1")
assert.Equal(t, "chaincode1", config.InitChaincode())

config.SetTopupAddress("cc")
assert.Equal(t, "cc", config.TopupAddress())

config.SetTopupPK("TOPUPPKPK")
assert.Equal(t, "TOPUPPKPK", config.TopupPK())

config.SetInitChaincodes([]string{"chaincode3", "chaincode6"})
assert.Equal(t, []string{"chaincode3", "chaincode6"}, config.InitChaincodes())
config.SetInitChaincode("chaincode3")
assert.Equal(t, "chaincode3", config.InitChaincode())

testConf = []byte(`
{
Expand Down
15 changes: 6 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var (
tx0 string
chaincodes string
chaincode string
addrTopup string
isRegtest bool
mainConfig *config.Config
Expand All @@ -31,7 +31,7 @@ var (
func parseFlags() {
flag.BoolVar(&isRegtest, "regtest", false, "Use regtest wallet configuration instead of user wallet")
flag.StringVar(&tx0, "tx", "", "Tx id for genesis attestation transaction")
flag.StringVar(&chaincodes, "chaincodes", "", "Chaincodes for multisig pubkeys")
flag.StringVar(&chaincode, "chaincode", "", "Chaincode for sig pubkey")
flag.StringVar(&addrTopup, "addrTopup", "", "Address for topup transaction")
flag.Parse()
}
Expand All @@ -51,20 +51,17 @@ func init() {
}

// if either tx or script not set throw error
if tx0 == "" || chaincodes == "" {
if mainConfig.InitTx() == "" || len(mainConfig.InitChaincodes()) == 0 {
if tx0 == "" || chaincode == "" {
if mainConfig.InitTx() == "" || mainConfig.InitChaincode() == "" {
flag.PrintDefaults()
log.Error(`Need to provide all -tx, -script and -chaincode arguments.
To use test configuration set the -regtest flag.`)
}
} else {
mainConfig.SetInitTx(tx0)

chaincodesList := strings.Split(chaincodes, ",") // string to string slice
for i := range chaincodesList { // trim whitespace
chaincodesList[i] = strings.TrimSpace(chaincodesList[i])
}
mainConfig.SetInitChaincodes(chaincodesList)
chaincodeStr := strings.TrimSpace(chaincode) // trim whitespace
mainConfig.SetInitChaincode(chaincodeStr)
}
if addrTopup != "" {
mainConfig.SetTopupAddress(addrTopup)
Expand Down
5 changes: 2 additions & 3 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"os"
"os/exec"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -45,7 +44,7 @@ var testConf = []byte(`

// test parameters for a 1-2 multisig redeemScript and P2SH address
const Address = "bcrt1q7h6ue5w39ramd4ux6gtxh6swnrefpcfgt7vl64"
const InitChaincodes = "14df7ece79e83f0f479a37832d770294014edc6884b0c8bfa2e0aaf51fb00229,14df7ece79e83f0f479a37832d770294014edc6884b0c8bfa2e0aaf51fb00229"
const InitChaincode = "14df7ece79e83f0f479a37832d770294014edc6884b0c8bfa2e0aaf51fb00229"

const PrivMain = "cRb1ZU6gsHeifDnBRyRMfbMayWpnNtpKcNs7Z9XzqE87ZwW6Vqx8"

Expand Down Expand Up @@ -100,7 +99,7 @@ func NewTest(logOutput bool, isRegtest bool) *Test {

config.SetInitTx(txid)
config.SetInitPK(PrivMain)
config.SetInitChaincodes(strings.Split(InitChaincodes, ","))
config.SetInitChaincode(InitChaincode)
config.SetInitPublicKey(PublicKey)

// custom config for top up process
Expand Down

0 comments on commit 0c1cc62

Please sign in to comment.