Skip to content

Commit

Permalink
feat: Change GenerateSecp256k1MultisigScript parameter (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjchen7 authored May 24, 2022
1 parent 9a3f743 commit 6023440
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
36 changes: 27 additions & 9 deletions address/multisig_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"github.com/nervosnetwork/ckb-sdk-go/types"
)

func GenerateSecp256k1MultisigScript(requireN, threshold int, publicKeys [][]byte) (*types.Script, []byte, error) {
// GenerateSecp256k1MultisigScript generate scep256k1 multisig script.
// It can accept public key (in compressed format, 33 bytes each) array or public key hash (20 bytes) array, and
// return error if giving none of them.
func GenerateSecp256k1MultisigScript(requireN, threshold int, publicKeysOrHashes [][]byte) (*types.Script, []byte, error) {
if requireN < 0 || requireN > 255 {
return nil, nil, errors.New("requireN must ranging from 0 to 255")
}
if threshold < 0 || threshold > 255 {
return nil, nil, errors.New("requireN must ranging from 0 to 255")
}
if len(publicKeys) > 255 {
if len(publicKeysOrHashes) > 255 {
return nil, nil, errors.New("public keys size must be less than 256")
}
if len(publicKeys) < requireN || len(publicKeys) < threshold {
if len(publicKeysOrHashes) < requireN || len(publicKeysOrHashes) < threshold {
return nil, nil, errors.New("public keys error")
}



var data []byte
data = append(data, 0)

Expand All @@ -35,15 +40,28 @@ func GenerateSecp256k1MultisigScript(requireN, threshold int, publicKeys [][]byt
data = append(data, b[:1]...)

b = make([]byte, 2)
binary.LittleEndian.PutUint16(b, uint16(len(publicKeys)))
binary.LittleEndian.PutUint16(b, uint16(len(publicKeysOrHashes)))
data = append(data, b[:1]...)

for _, pub := range publicKeys {
hash, err := blake2b.Blake160(pub)
if err != nil {
return nil, nil, err
isPublicKeyHash := len(publicKeysOrHashes[0]) == 20
if isPublicKeyHash {
for _, publicKeyHash := range publicKeysOrHashes {
if len(publicKeyHash) != 20 {
return nil, nil, errors.New("public key hash length must be 20 bytes")
}
data = append(data, publicKeyHash...)
}
} else {
for _, publicKey := range publicKeysOrHashes {
if len(publicKey) != 33 {
return nil, nil, errors.New("public key (compressed) length must be 33 bytes")
}
publicKeyHash, err := blake2b.Blake160(publicKey)
if err != nil {
return nil, nil, err
}
data = append(data, publicKeyHash...)
}
data = append(data, hash...)
}

args, err := blake2b.Blake160(data)
Expand Down
31 changes: 31 additions & 0 deletions address/multisig_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package address

import (
"encoding/hex"
"github.com/nervosnetwork/ckb-sdk-go/crypto/blake2b"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,3 +40,33 @@ func TestGenerateSecp256k1MultisigScript(t *testing.T) {
}
assert.Equal(t, "ckt1qpw9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sq0sfnkgf0ph76pkzwld9ujzex4pkeuwnlsdc5tqu", address)
}

func TestGenerateSecp256k1MultisigScriptByHash(t *testing.T) {
publicKeysHex := []string {
"032edb83018b57ddeb9bcc7287c5cc5da57e6e0289d31c9e98cb361e88678d6288",
"033aeb3fdbfaac72e9e34c55884a401ee87115302c146dd9e314677d826375dc8f",
"029a685b8206550ea1b600e347f18fd6115bffe582089d3567bec7eba57d04df01",
}
var publicKeysHash [][]byte
for _, publicKeyHex := range publicKeysHex {
key, err := hex.DecodeString(publicKeyHex)
if err != nil {
assert.Error(t, err)
}
hash, err := blake2b.Blake160(key)
if err != nil {
assert.Error(t, err)
}
publicKeysHash = append(publicKeysHash, hash)
}
script, _, err := GenerateSecp256k1MultisigScript(0, 2, publicKeysHash)
if err != nil {
assert.Error(t, err)
}

address, err := ConvertScriptToAddress(Testnet, script)
if err != nil {
assert.Error(t, err)
}
assert.Equal(t, "ckt1qpw9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sq0sfnkgf0ph76pkzwld9ujzex4pkeuwnlsdc5tqu", address)
}

0 comments on commit 6023440

Please sign in to comment.