Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix identity create #1283

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
25 changes: 3 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,13 @@ all-cross: ontology-cross tools-cross abi
format:
$(GOFMT) -w main.go

docker/payload: docker/build/bin/ontology docker/Dockerfile
@echo "Building ontology payload"
@mkdir -p $@
@cp docker/Dockerfile $@
@cp docker/build/bin/ontology $@
@touch $@

docker/build/bin/%: Makefile
@echo "Building ontology in docker"
@mkdir -p docker/build/bin docker/build/pkg
@$(DRUN) --rm \
-v $(abspath docker/build/bin):/go/bin \
-v $(abspath docker/build/pkg):/go/pkg \
-v $(GOPATH)/src:/go/src \
-w /go/src/github.com/ontio/ontology \
golang:1.9.5-stretch \
$(GC) $(BUILD_NODE_PAR) -o docker/build/bin/ontology main.go
@touch $@

docker: Makefile docker/payload docker/Dockerfile
docker: Makefile
@echo "Building ontology docker"
@$(DBUILD) -t $(DOCKER_NS)/ontology docker/payload
@docker tag $(DOCKER_NS)/ontology $(DOCKER_NS)/ontology:$(DOCKER_TAG)
@$(DBUILD) --no-cache -t $(DOCKER_NS)/ontology:$(DOCKER_TAG) - < docker/Dockerfile
@touch $@

clean:
rm -rf *.8 *.o *.out *.6 *exe coverage
rm -rf ontology ontology-* tools docker/payload docker/build
rm -rf ontology ontology-* tools

24 changes: 5 additions & 19 deletions account/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ import (

"github.com/itchyny/base58-go"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/types"
"golang.org/x/crypto/ripemd160"
)

const (
SCHEME = "did"
METHOD = "ont"
VER = 0x41
)

func GenerateID() (string, error) {
Expand All @@ -44,25 +43,12 @@ func GenerateID() (string, error) {
if err != nil {
return "", fmt.Errorf("generate ID error, %s", err)
}
return CreateID(buf[:])
return CreateID(buf[:]), nil
}

func CreateID(nonce []byte) (string, error) {
hasher := ripemd160.New()
_, err := hasher.Write(nonce)
if err != nil {
return "", fmt.Errorf("create ID error, %s", err)
}
data := hasher.Sum([]byte{VER})
data = append(data, checksum(data)...)

bi := new(big.Int).SetBytes(data).String()
idstring, err := base58.BitcoinEncoding.Encode([]byte(bi))
if err != nil {
return "", fmt.Errorf("create ID error, %s", err)
}

return SCHEME + ":" + METHOD + ":" + string(idstring), nil
func CreateID(nonce []byte) string {
addr := common.AddressFromVmCode(nonce)
return SCHEME + ":" + METHOD + ":" + addr.ToBase58()
}

func VerifyID(id string) bool {
Expand Down
26 changes: 9 additions & 17 deletions account/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ package account
import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
)

var id = "did:ont:TSS6S4Xhzt5wtvRBTm4y3QCTRqB4BnU7vT"
var id = "did:ont:AQr5dncx7L7MjgnE91tgsJheBCLMGRwjDt"

func TestCreate(t *testing.T) {
nonce, _ := hex.DecodeString("4c6b58adc6b8c6774eee0eb07dac4e198df87aae28f8932db3982edf3ff026e4")
id1, err := CreateID(nonce)
if err != nil {
t.Fatal(err)
}
t.Log("result ID:", id1)
if id != id1 {
t.Fatal("expected ID:", id)
}
id1 := CreateID(nonce)
assert.Equal(t, id1, id)
id2 := CreateID([]byte{1})
assert.NotEqual(t, id1, id2)
}

func TestVerify(t *testing.T) {
t.Log("verify", id)
if !VerifyID(id) {
t.Error("error: failed")
}
assert.True(t, VerifyID(id))

invalid := []string{
"did:ont:",
Expand All @@ -52,9 +47,6 @@ func TestVerify(t *testing.T) {
}

for _, v := range invalid {
t.Log("verify", v)
if VerifyID(v) {
t.Error("error: passed")
}
assert.False(t, VerifyID(v))
}
}
54 changes: 24 additions & 30 deletions cmd/abi/native_abi_script/governance.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,29 @@
],
"returnType":"Bool"
},
{
"name":"setFeePercentage",
"parameters":
[
{
"name":"PeerPubkey",
"type":"String"
},
{
"name":"Address",
"type":"Address"
},
{
"name":"PeerCost",
"type":"Int"
},
{
"name":"StakeCost",
"type":"Int"
}
],
"returnType":"Bool"
},
{
"name":"withdrawFee",
"parameters":
Expand Down Expand Up @@ -556,38 +579,9 @@
}
],
"returnType":"Bool"
},
{
"name":"getPeerPool",
"parameters":
[
],
"returnType":"ByteArray"
},
{
"name":"getPeerInfo",
"parameters":
[
{
"name":"PeerPublicKey",
"type":"String"
}
],
"returnType":"ByteArray"
},
{
"name":"getPeerPoolByAddress",
"parameters":
[
{
"name":"Address",
"type":"Address"
}
],
"returnType":"ByteArray"
}
],
"events":
[
]
}
}
12 changes: 3 additions & 9 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var schemeMap = map[string]schemeInfo{
// wait for user to choose options
func chooseKeyType(reader *bufio.Reader) string {
common.PrintNotice("key type")
for true {
for {
tmp, _ := reader.ReadString('\n')
tmp = strings.TrimSpace(tmp)
_, ok := keyTypeMap[tmp]
Expand All @@ -123,7 +123,7 @@ func chooseKeyType(reader *bufio.Reader) string {
}
func chooseScheme(reader *bufio.Reader) string {
common.PrintNotice("signature-scheme")
for true {
for {
tmp, _ := reader.ReadString('\n')
tmp = strings.TrimSpace(tmp)

Expand All @@ -139,7 +139,7 @@ func chooseScheme(reader *bufio.Reader) string {
}
func chooseCurve(reader *bufio.Reader) string {
common.PrintNotice("curve")
for true {
for {
tmp, _ := reader.ReadString('\n')
tmp = strings.TrimSpace(tmp)
_, ok := curveMap[tmp]
Expand Down Expand Up @@ -217,15 +217,12 @@ func checkCurve(ctx *cli.Context, reader *bufio.Reader, t *string) string {
} else {
c = chooseCurve(reader)
}
break
case "sm2":
fmt.Println("Use curve sm2p256v1 with key length of 256 bits.")
c = "SM2P256V1"
break
case "ed25519":
fmt.Println("Use curve 25519 with key length of 256 bits.")
c = "ED25519"
break
default:
return ""
}
Expand All @@ -247,15 +244,12 @@ func checkScheme(ctx *cli.Context, reader *bufio.Reader, t *string) string {
} else {
sch = chooseScheme(reader)
}
break
case "sm2":
fmt.Println("Use SM3withSM2 as the signature scheme.")
sch = "SM3withSM2"
break
case "ed25519":
fmt.Println("Use SHA512withEdDSA as the signature scheme.")
sch = "SHA512withEdDSA"
break
default:
return ""
}
Expand Down
3 changes: 0 additions & 3 deletions cmd/common/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Select a signature algorithm from the following:
3 Ed25519

[default is 1]: `)
break

case "curve":
fmt.Printf(`
Expand All @@ -46,7 +45,6 @@ Select a curve from the following:
4 | P-521 | 521

This determines the length of the private key [default is 2]: `)
break

case "signature-scheme":
fmt.Printf(`
Expand All @@ -63,7 +61,6 @@ Select a signature scheme from the following:
9 RIPEMD160withECDSA

This can be changed later [default is 2]: `)
break

default:
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/contract_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func deployContract(ctx *cli.Context) error {
gasPrice = 0
}

cversion := fmt.Sprintf("%s", version)
cversion := version

if ctx.IsSet(utils.GetFlagName(utils.ContractPrepareDeployFlag)) {
preResult, err := utils.PrepareDeployContract(vmtype, code, name, cversion, author, email, desc)
Expand Down
6 changes: 6 additions & 0 deletions cmd/sig_tx_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func genMultiSigAddress(ctx *cli.Context) error {
continue
}
data, err := hex.DecodeString(pk)
if err != nil {
return err
}
pubKey, err := keypair.DeserializePublicKey(data)
if err != nil {
return fmt.Errorf("invalid pub key:%s", pk)
Expand Down Expand Up @@ -141,6 +144,9 @@ func multiSigToTx(ctx *cli.Context) error {
continue
}
data, err := hex.DecodeString(pk)
if err != nil {
return err
}
pubKey, err := keypair.DeserializePublicKey(data)
if err != nil {
return fmt.Errorf("invalid pub key:%s", pk)
Expand Down
15 changes: 7 additions & 8 deletions cmd/tx_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ func transferTx(ctx *cli.Context) error {
switch strings.ToLower(asset) {
case "ont":
amount = utils.ParseOnt(amountStr)
amountStr = utils.FormatOnt(amount)
// amountStr = utils.FormatOnt(amount)
case "ong":
amount = utils.ParseOng(amountStr)
amountStr = utils.FormatOng(amount)
// amountStr = utils.FormatOng(amount)
default:
return fmt.Errorf("unsupport asset:%s", asset)
}
Expand Down Expand Up @@ -277,10 +277,10 @@ func approveTx(ctx *cli.Context) error {
switch strings.ToLower(asset) {
case "ont":
amount = utils.ParseOnt(amountStr)
amountStr = utils.FormatOnt(amount)
// amountStr = utils.FormatOnt(amount)
case "ong":
amount = utils.ParseOng(amountStr)
amountStr = utils.FormatOng(amount)
// amountStr = utils.FormatOng(amount)
default:
return fmt.Errorf("unsupport asset:%s", asset)
}
Expand Down Expand Up @@ -363,10 +363,9 @@ func transferFromTx(ctx *cli.Context) error {
switch strings.ToLower(asset) {
case "ont":
amount = utils.ParseOnt(amountStr)
amountStr = utils.FormatOnt(amount)
case "ong":
amount = utils.ParseOng(amountStr)
amountStr = utils.FormatOng(amount)
// amountStr = utils.FormatOng(amount)
default:
return fmt.Errorf("unsupport asset:%s", asset)
}
Expand Down Expand Up @@ -425,13 +424,13 @@ func withdrawONGTx(ctx *cli.Context) error {
if amount <= 0 {
return fmt.Errorf("haven't unbound ong")
}
amountStr = utils.FormatOng(amount)
// amountStr = utils.FormatOng(amount)
} else {
amount = utils.ParseOng(amountStr)
if amount <= 0 {
return fmt.Errorf("haven't unbound ong")
}
amountStr = utils.FormatOng(amount)
// amountStr = utils.FormatOng(amount)
}

var payer common.Address
Expand Down
Loading