function forward($decodeJson = false) { // Base64 the payload: $payload = base64_encode(file_get_contents('php://input')); // Base64 the protected header, which contains only the fwd field: $pHeader = 'eyJmd2QiOjF9'; // base64_encode('{"fwd":1}'); return sendToRoot($payload, $pHeader, $decodeJson); }
if ($forwardedFromRoot != 0) { // Some other root forwarded the request here. Just return my challenge: echo $myChallenge; exit; } // Forward this request to other root nodes, stating that this root node is leading it. // Custom forward call here because domain is almost always given over GET. // I.e. php://input isn't being set to what we want it to be. // pHeader (2nd parameter of sendToRoot) is the same string as seen inside the forward() function. // Base64 the payload: $payload = base64_encode('{"domain":"' . $domain . '"}'); // Forward now! $results = sendToRoot($payload, 'eyJmd2QiOjF9'); // See above about this base64 text // Results is now a set of successful responses. Each contains the signature // of the challenge for a given endpoint and the public key. // Verify each signature (except our own - we pass that in instead) to test if we have a majority. // If we do, forward the whole set to complete the creation of a new entity! $fullSet = testMajority($results, $hexPublicKey, $mySignature); // Majority formed! - send it to the root so everyone can verify all the signatures too. // Add the domain to it: $fullSet = '{"domain":"' . $domain . '","challenges":' . $fullSet . '}'; // For info on the base64 string in the middle, see the forward() function. sendToRoot(base64_encode($fullSet), 'eyJmd2QiOjF9', false, 'entity/challenge/success'); // Remove from pending: $dz->query('delete from `Root.Entities.Pending` where `Endpoint`="' . $domain . '"'); // Transfer the entity to being an official entity: $dz->query('insert into `Root.Entities` (`Key`,`Endpoint`,`Type`,`Group`,`Name`,`Country`) values (unhex("' . $hexPublicKey . '"),"' . $domain . '",' . $row['Type'] . ',' . $thisEntity['Group'] . ',"' . $row['Name'] . '",' . $row['Country'] . ')'); // Create the change event too: changed('entity', array('key' => $hexPublicKey, 'type' => $row['Type'], 'endpoint' => $domain, 'group' => $thisEntity['Group'], 'name' => $row['Name'], 'country' => $row['Country'])); echo '{"entity":"' . $domain . '"}';
// Validate the signature: $signed = bin2hex($publicKey) . '.' . $balance; if (!verify($signature, $signed, $address)) { // Invalid signature: error('signature/invalid'); } // Valid claim! // Sign it to say this node agrees, using some additional random data: $challenge = randomString(45); $myChallenge = '{"challenge":"' . $challenge . '","signature":"' . base64_encode(sign($challenge . $signed)) . '"}'; // Was this request forwarded? if ($forwardedFromRoot != 0) { // Yes - just output my challenge. We'll wait for a majority. echo $myChallenge; exit; } // Forward: $responses = forward(); // Did it obtain a majority? $fullSet = testMajority($responses, $signed, $myChallenge); // Yes - majority obtained! Forward the key set to the root now: // Add the address: $fullSet = '{"address":"' . $address . '","challenges":' . $fullSet . '}'; // For info on the base64 string in the middle, see the forward() function. $encodedFullSet = base64_encode($fullSet); // Send now: sendToRoot($encodedFullSet, 'eyJmd2QiOjF9', false, 'balance/claim/success'); // Update the balance: $dz->query('update `Root.Balances` set `Entity`=' . $verifiedEntity . ' where `Key`=unhex("' . $address . '")'); // Create a clm record (occurs in balance/claim/success as well): changed('clm', array('entity' => $verifiedEntityEndpoint, 'address' => $address, 'signature' => $signature, 'balance' => $balance));
// We do it by calling the same function but with an empty results set, which is set like so: $fullSet = '{}'; } else { $majority = true; // Majority obtained! Forward all the signatures (fullSet) to everyone else. } // Add the amount and address: $fullSet = '{"amount":"' . $amount . '","commodity":"' . $tag . '","from":{"address":"' . $fromAddress . '","group":' . $fromGroup . ',"balance":' . $fromBalance . '},"to":{"address":"' . $toAddress . '","group":' . $toGroup . '},"signature":"' . $signature . '","challenges":' . $fullSet . '}'; // For info on the base64 string in the middle, see the forward() function. $encodedFullSet = base64_encode($fullSet); sendToRoot($encodedFullSet, 'eyJmd2QiOjF9', false, 'transfer/create/success'); if (!$majority) { // Now produce the error: error('majority/notformed'); } // Update the amounts now! $dz->query('update `Root.Balances` set LockedBalance=LockedBalance-' . $amount . ' where `Key`=UNHEX("' . $fromAddress . '")'); if (!$outbound) { // It's going to this root - update the to address as well: $dz->query('update `Root.Balances` set Balance=Balance+' . $amount . ',LockedBalance=LockedBalance-' . $amount . ' where `Key`=UNHEX("' . $toAddress . '")'); } // Create a tx record (occurs in transfer/create/success as well): changed('tx', array('amount' => $amount, 'to' => array('address' => $toAddress, 'group' => $toGroup, 'balance' => $fromBalance), 'from' => array('address' => $fromAddress, 'group' => $fromGroup), 'signature' => $signature)); // Is it going out of this root? // Note: Only the 'leading' root node needs to do this. if ($outbound) { // Yes - forwarding the to balance to another group. // These should be grouped together and sent in bulk for significant link latencies. // For info on the base64 string in the middle, see the forward() function. sendToRoot($encodedFullSet, 'eyJmd2QiOjF9', false, 'transfer/outroot', $toRoot); }
// We're now going to receive that amount locked as we'll need a majority here too. // Lock the given amount into the given receiving address. This happens first because it fails // if the commodities do not match: receiveLocked($toAddress, $amount, $commodity); // Ok! We've locked $amount. This half-transaction is now in progress. // Generate some random challenge data: $challenge = randomString(45); // Sign the challenge data along with the data that was signed earlier: $rootSignature = sign($challenge . $signed); // Build my signature JSON: $myPair = '{"challenge":"' . $challenge . '","signature":"' . base64_encode($rootSignature) . '"}'; // Is this already forwarded? If so, just return my signature. if ($forwardedFromRoot != 0) { // Some other root forwarded the request here. Just output the signature and stop: echo $myPair; exit; } // Forward to the group: $results = forward(); // Next, verify all the signatures. If we have a valid majority, forward it to the group. // At which point, a successful issue has occured. $fullSet = testMajority($results, $signed, $myPair); // Majority obtained! Forward all the signatures to everyone else. // Add the amount and address: $fullSet = '{"amount":"' . $amount . '","commodity":"' . $commodity . '","from":{"address":"' . $fromAddress . '","group":' . $fromGroup . ',"balance":' . $fromBalance . '},"to":{"address":"' . $toAddress . '","group":' . $toGroup . '},"signature":"' . $signature . '","challenges":' . $fullSet . '}'; // For info on the base64 string in the middle, see the forward() function. sendToRoot(base64_encode($fullSet), 'eyJmd2QiOjF9', false, 'transfer/outroot/success'); // Update the amounts now! $dz->query('update `Root.Balances` set Balance=Balance+' . $amount . ',LockedBalance=LockedBalance-' . $amount . ' where `Key`=UNHEX("' . $toAddress . '")'); // Create a tx record (occurs in transfer/outroot/success as well): changed('tx', array('amount' => $amount, 'to' => array('address' => $toAddress, 'group' => $toGroup, 'balance' => $fromBalance), 'from' => array('address' => $fromAddress, 'group' => $fromGroup), 'signature' => $signature));
} // Get the address to issue into: $hexAddress = safe('address', VALID_HEX); // Lock the given amount into the given receiving address: receiveLocked($hexAddress, $amount, $tag, $issuerID); // Generate some random challenge data: $challenge = randomString(45); // Sign the challenge data along with the hex public key and amount: $signature = sign($challenge . $hexAddress . $amount); // Build my signature JSON: $myPair = '{"challenge":"' . $challenge . '","signature":"' . base64_encode($signature) . '"}'; // Is this already forwarded? If so, just return my signature. if ($forwardedFromRoot != 0) { // Some other root forwarded the request here. Just output the signature and stop: echo $myPair; exit; } // Forward to the group: $results = forward(); // Next, verify all the signatures. If we have a valid majority, forward it to the group. // At which point, a successful issue has occured. $fullSet = testMajority($results, $hexAddress . $amount, $myPair); // Majority obtained! Forward all the signatures to everyone else. // Add the amount and address: $fullSet = '{"amount":"' . $amount . '","address":"' . $hexAddress . '","challenges":' . $fullSet . '}'; // For info on the base64 string in the middle, see the forward() function. sendToRoot(base64_encode($fullSet), 'eyJmd2QiOjF9', false, 'commodity/issue/success'); // Update the amount now! $dz->query('update `Root.Balances` set Balance=Balance+' . $amount . ',LockedBalance=LockedBalance-' . $amount . ' where `Key`=UNHEX("' . $hexAddress . '")'); // Create an issue record (occurs in issue/success as well): changed('issue', array('amount' => $amount, 'to' => $hexAddress, 'tag' => $tag));