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 . '"}';
$exists = $dz->get_row('select ID from `Root.Entities` where Endpoint="' . $issuer . '"'); if (!$exists) { // Didn't find the entity that wants to issue it. error('entity/notfound'); } // Update issuer with the (local) ID: $issuerID = $exists['ID']; // $verifiedEntity must match the parent commodity. // Find the parent commodity and then check to see if its issuer signed the request. // For example, if we're requesting 'currency.usd', then the issuer of 'currency' // must have been the one signing this request. $parentIssuer = findParent($tag); if (!$parentIssuer) { // No suitable parent commodity was not found. error('commodity/notfound'); } // Was that parent issuer the same one who signed this request? if ($verifiedEntity != $parentIssuer['ID']) { // Nope! Only the parent can sign the request. error('entity/notparent'); } // Create the commodity now: $dz->query('insert into `Root.Commodities` (`Tag`,`Description_en`,`Name_en`,`Divisor`,`Issuer`,`Policy`) values ("' . $tag . '","' . $description_en . '","' . $name_en . '",' . $divisor . ',' . $issuerID . ',' . $policy . ')'); changed('com', array('tag' => $tag, 'description' => array('en' => $description_en), 'name' => array('en' => $name_en), 'divisor' => $divisor, 'issuer' => $issuer, 'policy' => $policy)); // Is this already forwarded? If so, stop there. if ($forwardedFromRoot != 0) { // Some other root forwarded the request here. Just quit: exit; } // Forward this request to other root nodes. forward();
$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));
// This allows them to independently form a proof of majority. // In this case, when the majority is successful, an issue occurs. // Always JSON posted here: postedTo(); if ($forwardedFromRoot == 0) { // Must have been forwarded by a root node. error('entity/notroot'); } // Get the challenges: // We want it as-is (it's an array) $challenges = safe('challenges', VALID_ARRAY); // Amount must be a positive non-zero number: $amount = safe('amount', VALID_NUMBER); $amount = (int) $amount; if ($amount == 0) { // They specified 0 - this isn't valid: error('field/invalid', 'amount'); } // Get the address to issue into: $hexAddress = safe('address', VALID_HEX); // Test for a majority. This time we check all signatures including our own // because the whole set came from somewhere else (we're not leading the majority process here). testMajority($challenges, $hexAddress . $amount); // Majority formed! // Note: From the previous test a few ms ago, we know the balance exists and is of the correct commodity. // Because of the locking process, we know that the balance can't have been emptied and deleted. // 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));
// 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); }