Skip to content

Commit

Permalink
extend prefund to accept hex address
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed Dec 21, 2017
1 parent 9034590 commit b18b5e3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
34 changes: 34 additions & 0 deletions b58addr/convert.go
Original file line number Diff line number Diff line change
@@ -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]
}
}
11 changes: 11 additions & 0 deletions b58addr/convert_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 3 additions & 1 deletion deployer/qtum/qtumDeployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/url"
"time"

"github.com/qtumproject/solar/b58addr"

"math/rand"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions prefund.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package solar

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"

"github.com/pkg/errors"
)

func init() {
Expand All @@ -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
// { <addr>: <amount>, ... }

Expand Down

0 comments on commit b18b5e3

Please sign in to comment.