From b18b5e30e93d97b7588cb7bcbfb7fcb89c320f0e Mon Sep 17 00:00:00 2001 From: Howard Yeh Date: Thu, 21 Dec 2017 17:46:38 +0800 Subject: [PATCH] extend prefund to accept hex address --- b58addr/convert.go | 34 ++++++++++++++++++++++++++++++++++ b58addr/convert_test.go | 11 +++++++++++ deployer/qtum/qtumDeployer.go | 4 +++- prefund.go | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 b58addr/convert.go create mode 100644 b58addr/convert_test.go diff --git a/b58addr/convert.go b/b58addr/convert.go new file mode 100644 index 0000000..df6866c --- /dev/null +++ b/b58addr/convert.go @@ -0,0 +1,34 @@ +package b58addr + +import base58 "github.com/jbenet/go-base58" +import "encoding/hex" + +/* +An arbitrarily sized payload. + +* A set of 58 alphanumeric symbols consisting of easily distinguished uppercase and lowercase letters (0OIl are not used) +* One byte of version/application information. Bitcoin addresses use 0x00 for this byte (future ones may use 0x05). +* Four bytes (32 bits) of SHA256-based error checking code. This code can be used to automatically detect and possibly correct typographical errors. +* An extra step for preservation of leading zeroes in the data. + +data := version + payload +checksum := take4(sha256(sha256(data))) +addr := data + checksum +*/ + +// qcli gethexaddress qQGqkA16ZY6bCYy7Qjr77eU4BPsdadibCG +// 49a80104c0d27a9ba29678d07e87a57151107613 +func ToHexString(data string) string { + // reverse + buf := base58.Decode(data) + + // [version (1 byte)][address (20 bytes)][digest (4 bytes)] + hexstr := hex.EncodeToString(buf[1:21]) + return hexstr +} + +func reverse(numbers []byte) { + for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 { + numbers[i], numbers[j] = numbers[j], numbers[i] + } +} diff --git a/b58addr/convert_test.go b/b58addr/convert_test.go new file mode 100644 index 0000000..7b7c596 --- /dev/null +++ b/b58addr/convert_test.go @@ -0,0 +1,11 @@ +package b58addr + +import "testing" + +import "github.com/stretchr/testify/assert" + +func TestToHexAddressString(t *testing.T) { + is := assert.New(t) + hexstr := ToHexString("qQGqkA16ZY6bCYy7Qjr77eU4BPsdadibCG") + is.Equal(hexstr, "49a80104c0d27a9ba29678d07e87a57151107613") +} diff --git a/deployer/qtum/qtumDeployer.go b/deployer/qtum/qtumDeployer.go index 78c17da..0fe1356 100644 --- a/deployer/qtum/qtumDeployer.go +++ b/deployer/qtum/qtumDeployer.go @@ -4,6 +4,8 @@ import ( "net/url" "time" + "github.com/qtumproject/solar/b58addr" + "math/rand" "github.com/pkg/errors" @@ -82,7 +84,7 @@ func (d *Deployer) CreateContract(c *contract.CompiledContract, jsonParams []byt TransactionID: tx.TxID, Address: tx.Address, CreatedAt: time.Now(), - Sender: tx.Sender, + Sender: b58addr.ToHexString(tx.Sender), } if aslib { diff --git a/prefund.go b/prefund.go index d278e7e..457446e 100644 --- a/prefund.go +++ b/prefund.go @@ -1,9 +1,12 @@ package solar import ( + "encoding/hex" "encoding/json" "fmt" "strings" + + "github.com/pkg/errors" ) func init() { @@ -28,6 +31,18 @@ func init() { ownerAddr = *owner } + // if the address is hexadecimal, convert it to base58 address + _, err = hex.DecodeString(ownerAddr) + if err == nil { + var b58addr string + rpcErr := rpc.Call(&b58addr, "fromhexaddress", ownerAddr) + if rpcErr != nil { + return errors.Wrap(err, "convert hex address") + } + + ownerAddr = b58addr + } + // The JSON object is allowed to have duplicate keys for this call // { : , ... }