Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Commit

Permalink
Add regaccount function
Browse files Browse the repository at this point in the history
  • Loading branch information
elmato committed Jul 28, 2018
1 parent b95fe96 commit aa1a1fa
Show file tree
Hide file tree
Showing 24 changed files with 5,123 additions and 13 deletions.
58 changes: 58 additions & 0 deletions eosio.unregd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
![Alt Text](https://i.imgur.com/6F5aHWH.png)

# Build

eosiocpp -o eosio.unregd.wast eosio.unregd.cpp

# Setup

```./setup.sh```

The setup script will install one contract (besides the defaults ones):

`eosio.unregd` (empty)

You need to have nodeos running.

# Dependecies

```pip install bitcoin --user```
```pip install requests --user```
```sudo apt-get install python-pysha3```

# Test

```./test.sh```

The test step will:

0. Generate a new ETH address with a random privkey.

1. Add the ETH address (`eosio.unregd::add`) and transfers a random amount of
EOS to the `eosio.unregd` contract.
This is to simulate a user that contributed to the ERC20 but
didn't register their EOS pubkey.

2. Call `eosio.unregd::regaccount` with:

* A signature for the message "$lib_block_num,$lib_block_prefix" generated with the ETH privkey
* The desired EOS account name (random in this case)
* An EOS pubkey (used to set the active/owner permission)

4. The function will :

* Verify that the destination account name is valid
* Verify that the account does not exists
* Extract the pubkey (compressed) from the signature using a message hash composed from the current TX block prefix/num
* Uncompress the pubkey
* Calculate ETH address based on the uncompressed publickey
* Verify that the ETH address exists in eosio.unregd contract
* Find and split the contribution into cpu/net/liquid
* Calculate the amount of EOS to purchase 8k of RAM
* Use the provided EOS key to build the owner/active authority to be used for the account
* Issue to eosio.unregd the necesary EOS to buy 8K of RAM
* Create the desired account
* Buy RAM for the account (8k)
* Delegate bandwith
* Transfer remaining if any (liquid EOS)
* Remove information for the ETH address from the eosio.unregd DB
105 changes: 105 additions & 0 deletions eosio.unregd/claim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import os
import sys
import json
import time
import requests as req
from datetime import datetime, timedelta
from hashlib import sha256
from bitcoin import ecdsa_raw_sign, encode_privkey
from tempfile import mktemp
from subprocess import Popen, PIPE

def url_for(url):
return 'http://127.0.0.1:8888{0}'.format(url)

def ref_block(block_id):
block_num = block_id[0:8]
block_prefix = block_id[16:16+8]

ref_block_num = int(block_num,16)
ref_block_prefix = int("".join(reversed([block_prefix[i:i+2] for i in range(0, len(block_prefix), 2)])),16)

return ref_block_num, ref_block_prefix

if len(sys.argv) < 3:
print "claim.py ETHPRIV EOSACCOUNT"
print " ETHPRIV : Ethereum private key. (can be in Wif or hex format)"
print " EOSACCOUNT: Desired EOS account name"
sys.exit(1)

block_id = req.get(url_for('/v1/chain/get_info')).json()['last_irreversible_block_id']

ref_block_num, ref_block_prefix = ref_block( block_id )

priv = sys.argv[1]
eos_account = sys.argv[2]
eos_pub = sys.argv[3]

msg = '%d,%d'%(ref_block_num, ref_block_prefix)

msghash = sha256(msg).digest()
v, r, s = ecdsa_raw_sign(msghash, encode_privkey(priv,'hex').decode('hex'))
signature = '00%x%x%x' % (v,r,s)

binargs = req.post(url_for('/v1/chain/abi_json_to_bin'),json.dumps({
"code" : "eosio.unregd",
"action" : "regaccount",
"args" : {
"signature" : signature,
"account" : eos_account,
"eos_pubkey" : eos_pub
}
})).json()['binargs']

tx_json = """
{
"expiration": "%s",
"ref_block_num": %d,
"ref_block_prefix": %d,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "eosio.unregd",
"name": "regaccount",
"authorization": [{
"actor": "%s",
"permission": "active"
}
],
"data": %s
}
],
"transaction_extensions": [],
"signatures": [],
"context_free_data": []
}
""" % (
(datetime.utcnow() + timedelta(minutes=3)).strftime("%Y-%m-%dT%T"),
ref_block_num,
ref_block_prefix,
"thisisatesta",
binargs
)

tmpf = mktemp()
with open(tmpf,"w") as f:
f.write(tx_json)

# print tmpf
# sys.exit(0)

with open(os.devnull, 'w') as devnull:
cmd = ["cleos","sign","-p","-k","5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3",tmpf]
p = Popen(cmd, stdout=PIPE, stderr=devnull)
output, err = p.communicate("")

if p.returncode:
sys.exit(1)

with open(tmpf,"w") as f:
f.write(output)

print tmpf
sys.exit(0)
Loading

0 comments on commit aa1a1fa

Please sign in to comment.