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

Create wallet API endpoint #1

Open
adriantpaez opened this issue Feb 22, 2019 · 0 comments
Open

Create wallet API endpoint #1

adriantpaez opened this issue Feb 22, 2019 · 0 comments

Comments

@adriantpaez
Copy link
Collaborator

adriantpaez commented Feb 22, 2019

Original issue: fibercrypto/skyxcommons#6 (comment)
Implement [POST] api/wallets

Should create a new wallet (address) in the blockchain.

Response:

{
 // Private key, which will be used to
 // sign transactions by the [POST] /api/sign
“privateKey”: ”string”,
 // Address which identifies the wallet in the blockchain
“publicAddress”: “string”,
 // Any non security sensitive data associated with
 // wallet. This context will be passed to
 // [POST] /api/transactions/*. 
 // Can be empty.
 “addressContext”: “string”
}

Python implementation

def create_wallet():
    """
    Create the wallet in blockchain
    """

    # generate CSRF token
    CSRF_token = app.session.get(form_url(app_config.SKYCOIN_NODE_URL, "/api/v1/csrf")).json()

    if not CSRF_token or "csrf_token" not in CSRF_token:
        return {"status": 500, "error": "Unknown server error"}

    # generate new seed
    new_seed = app.session.get(
        form_url(app_config.SKYCOIN_NODE_URL, "/api/v1/wallet/newSeed?entropy=128"),
        headers={'X-CSRF-Token': CSRF_token['csrf_token']}).json()

    if not new_seed or "seed" not in new_seed:
        return {"status": 500, "error": "Unknown server error"}

    # create the wallet from seed
    resp = app.session.post(form_url(app_config.SKYCOIN_NODE_URL, "/api/v1/wallet/create"),
                         {"seed": new_seed["seed"],
                             "label": app_config.WALLET_LABEL, "scan": "10"},
                         headers={'X-CSRF-Token': CSRF_token['csrf_token']})

    if not resp:
        return {"status": 500, "error": "Unknown server error"}

    if resp.status_code != 200:
        return {"status": 500, "error": "Unknown server error"}

    new_wallet = resp.json()

    if not new_wallet or "entries" not in new_wallet:
        return {"status": 500, "error": "Unknown server error"}

    seed = new_seed['seed']
    pubkey = skycoin.cipher_PubKey()
    seckey = skycoin.cipher_SecKey()
    error = skycoin.SKY_cipher_GenerateDeterministicKeyPair(
            seed.encode(), pubkey, seckey)
    if error != 0:
        return {"status": 500, "error": "Unknown server error"}

    return {
        "privateKey": binascii.hexlify(bytearray(seckey.toStr())).decode('ascii'),
        "publicAddress": new_wallet["entries"][0]["address"],
        "addressContext": new_wallet['meta']['filename']
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant