Example #1
0
function issue($commodity, $amount)
{
    if (!is_numeric($amount) || $amount <= 0) {
        // Not a suitable number.
        error('field/invalid', 'amount');
    }
    // Make and store the address pair:
    $keypair = generateKeyPair();
    $publicKey = storeKeyPair($keypair);
    // Build the API message. It requires the tag, amount and an address.
    $payload = '{"tag":"' . $commodity . '","amount":' . $amount . ',"address":"' . $publicKey . '"}';
    // Call the issue API:
    $error;
    $result = callRoot('commodity/issue', $payload, $error);
    if ($error) {
        // Failed.
        return false;
    }
    // Ok!
    return true;
}
Example #2
0
    // Get the policy name:
    $policyName = 'closed';
    if ($policy == 1) {
        // Public policy.
        $policyName = 'public';
    } else {
        if ($policy == 2) {
            // Reviewed.
            $policyName = 'reviewed';
        }
    }
    // Now we call the root commodity/create API:
    $rootCreate = '{"tag":"' . $tag . '","policy":"' . $policyName . '","description":' . json_encode($description) . ',"name":' . json_encode($name) . ',"divisor":' . $divisor . ',"issuer":"' . $futureIssuer . '"}';
    // Send the request to root:
    $error;
    $response = callRoot('commodity/create', $rootCreate, $error);
    if ($error) {
        // Failed to request root.
        error('root/failed', $error);
    }
    // It was successful! futureIssuer is now the issuer of $tag.
    // Let's tell them about it using the status API again:
    issuerStatus($futureIssuer, $tag, 'success');
    // -----------------------------
    // Let the requester know that the request got here:
    echo '{"status":"OK"}';
} else {
    // Reviewed request (2). Note that it's not anything else (i.e. closed)
    // because we do a check for 1 or 2 further up.
    // Add it to a pending table instead,
    // review it however you wish, then call the above with success/reject.
Example #3
0
    // Device already has an account assigned to it.
    error('device/assigned');
}
// Username available?
$row = $dz->get_row('select Username from `Root.Usernames` where `Username`="' . $user . '"');
if ($row) {
    // Username used.
    error('username/exists');
}
// Generate a keypair which is used to sign for this user:
$signPair = generateKeyPair();
// Get the public key as hex:
$pubSignKey = bin2hex($signPair['public']);
// 'Claim' the username by calling the root API:
$error;
$result = callRoot('username/create', '{"username":"******","public_key":"' . $pubSignKey . '"}', $error);
if ($error) {
    // Error claiming the username.
    // This mainly indicates that one or more people tried to obtain it at the same time.
    error('username/unclaimed');
}
// Hex the private key too:
$privSignKey = bin2hex($signPair['private']);
// Create the account now:
$dz->query('insert into `Bank.Accounts`(`Username`,`FullName`,`Registered`,`Country`,`SignKey`) values ("' . $user . '","' . $fullName . '",' . time() . ',0,unhex("' . $privSignKey . '"))');
// Get the account row ID:
$accountID = $dz->insert_id();
// Apply account settings next.
$dz->query('insert into `Bank.Account.Settings`(`Setting`,`Value`,`Account`) values ' . '("commodity.pref","' . $bankCurrency . '",' . $accountID . '), ' . '("email","' . $email . '",' . $accountID . ')');
// Associate the device with the account:
$dz->query('update `Bank.Devices` set `Account`=' . $accountID . ' where ID=' . $verifiedDevice);
Example #4
0
function globalTransfer($fromAddress, $toAddress, $toGroup, $amount, $fromBalance, $private)
{
    // Request the root now:
    global $thisEntity;
    // Get the from group:
    $fromGroup = $thisEntity['Group'];
    // Build the signed data:
    $signed = $fromGroup . '/' . $fromAddress . '-' . $toGroup . '/' . $toAddress . '-' . $amount . '-' . $fromBalance;
    // Sign it:
    $signature = base64_encode(sign($signed, $private));
    // Call the root API:
    $error;
    $response = callRoot('transfer/create', '{"from":{"address":"' . $fromAddress . '","group":' . $fromGroup . ',"balance":' . $fromBalance . '},"to":{"address":"' . $toAddress . '","group":' . $toGroup . '},"amount":' . $amount . ',"signature":"' . $signature . '"}', $error);
    if ($error) {
        // Remote server generated an error:
        error('remote/error', $error);
    }
    // Success!
    return true;
}