This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HydroNitrogen's profile picture

Krist Address v2 generation algorythm?

Started by HydroNitrogen, 24 June 2016 - 01:15 PM
HydroNitrogen #1
Posted 24 June 2016 - 03:15 PM
Hello guys,
I heard of Krist last wednesday, and decided to check it out, and I really got interested in it.
While I'm not sure if its wasted time or power, I've been mining for 5 hours and already got a little capital of 10 000 Krist.
After looking into the API I created some testing programs and I really enjoyed doing that.

So I'm going to (I actually already am) programming my own wallet program, in C#. (For Windows only, outside Minecraft)
It will be the most advanced wallet of all wallets I've seen yet. I will try to implement every feature the web API gives us.
I am skilled with C#, and a little with lua, but not very much.

I looked into the reference wallet code to see all the algorithms, and I understand a lot, but the address generation algorithm is kinda fragmented so I couldn't exactly figure that out.
Could anybody tell me (prefferably in psuedo-code) how this is done?
Thank you!

Part of the address generation code:
Spoiler
function makev2address(key)
  local protein = {}
  local stick = sha256(sha256(key))
  local n = 0
  local link = 0
  local v2 = "k"
  repeat
	if n < 9 then protein[n] = string.sub(stick,0,2)
	stick = sha256(sha256(stick)) end
	n = n + 1
  until n == 9
  n = 0
  repeat
	link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
	if string.len(protein[link]) ~= 0 then
	  v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
	  protein[link] = ''
	  n = n + 1
	else
	  stick = sha256(stick)
	end
  until n == 9
  return v2
end

Full reference wallet: https://krist.ceriat.net/kristwallet


Fun facts I calculated:

The richest account has % 18.96 of all the money.
The 5 richest accounts have % 60.49 of all the money.
The 10 richest accounts have % 74.35 of all the money.



And since everybody seems to be posting this along with their post: donate your too much KST to
GoogleTech.kst
;)/>
:P/>
Edited on 24 June 2016 - 02:06 PM
Lignum #2
Posted 24 June 2016 - 03:25 PM
You can find a much cleaner version of the address generation algorithm in the official Krist node source here. Alternatively, there's a Java implementation in KWallet's source. I find the former to be very easy to understand, but if you're still having trouble, ask away.

EDIT: You can also use the /v2 call of the Krist API if you don't mind the HTTP overhead.
Edited on 24 June 2016 - 01:26 PM
HydroNitrogen #3
Posted 24 June 2016 - 03:58 PM
You can find a much cleaner version of the address generation algorithm in the official Krist node source here. Alternatively, there's a Java implementation in KWallet's source. I find the former to be very easy to understand, but if you're still having trouble, ask away.

EDIT: You can also use the /v2 call of the Krist API if you don't mind the HTTP overhead.

Thank you a lot, I haven't found that one earlier. Java is much closer to C#, And I understand a lot more now :)/>
I am able to implement that code in a program of my own now,
but I don't understand everything…

Spoiler<p>
<br>for (i = 0; i &amp;lt;= 8;) {<br>&amp;nbsp; var index = parseInt(hash.substring(2 * i, 2 + (2 * i)), 16) % 9;</p><p>&amp;nbsp; if (chars[index] === "") {<br>&amp;nbsp;&amp;nbsp; hash = utils.sha256(hash);<br>&amp;nbsp; } else {<br>&amp;nbsp;&amp;nbsp; prefix += utils.hexToBase36(parseInt(chars[index], 16));<br>&amp;nbsp;&amp;nbsp; chars[index] = "";<br>&amp;nbsp;&amp;nbsp; i++;<br>&amp;nbsp; }<br>}<br>
</p>

Thats the part of the code I can't understand a single thing in there, all weird stuff going on, formulaes and everything.
The thing I do understand there is the while loop, it loops for all 8 things (ints, chars, strings? no idea) inside "var".


(And I can't handle stuff I dont understand ;)/> :angry:/>)
Lignum #4
Posted 24 June 2016 - 06:58 PM
Here's an entire explanation of the entire algorithm:

Let's use the privatekey "123" as an example.
To start off, we double sha256 it:
A0FCBE9152B3FA32A352E0ECC2DAA5B1F8D28227E63348FFDF33C258C7B0E0ED

Now, we need to create the 9 proteins from this hash, the JavaScript code calls them chars. The first protein is the first two letters of the hash (A0). Following that, we double hash the hash, and get the first two characters from that hash, and proceed to do that for the rest of the proteins.

I've done this for our privatekey and got these proteins:
A0 C2 82 BB 2D 8E 7B 7F 9E

After that, we take the last hash we got from our protein computation:
9EC5862E5945432814E83A924D373CC72F21CF66D1E1D14A9CBA88543D2952FB

Now, this is the part you're confused about. You take 9 pairs of letters from the hash (9E C5 86 2E … in this case), convert each to decimal and modulo (%) them by 9 (makes sure they're between 0 and 9). These numbers are indices into the protein array. Index the array at this number, if it is an empty character, double hash the hash again, if not, convert the protein you got to base 36 (you must use this function) and add it to the address. Each time you append to the address, you must set proteins[index] to an empty character.

Once that's done, you should have k87ztrc3ux as a result.
Edited on 24 June 2016 - 05:00 PM
HydroNitrogen #5
Posted 24 June 2016 - 08:13 PM
Here's an entire explanation of the entire algorithm:

Let's use the privatekey "123" as an example.
To start off, we double sha256 it:
A0FCBE9152B3FA32A352E0ECC2DAA5B1F8D28227E63348FFDF33C258C7B0E0ED

Now, we need to create the 9 proteins from this hash, the JavaScript code calls them chars. The first protein is the first two letters of the hash (A0). Following that, we double hash the hash, and get the first two characters from that hash, and proceed to do that for the rest of the proteins.

I've done this for our privatekey and got these proteins:
A0 C2 82 BB 2D 8E 7B 7F 9E

After that, we take the last hash we got from our protein computation:
9EC5862E5945432814E83A924D373CC72F21CF66D1E1D14A9CBA88543D2952FB

Now, this is the part you're confused about. You take 9 pairs of letters from the hash (9E C5 86 2E … in this case), convert each to decimal and modulo (%) them by 9 (makes sure they're between 0 and 9). These numbers are indices into the protein array. Index the array at this number, if it is an empty character, double hash the hash again, if not, convert the protein you got to base 36 (you must use this function) and add it to the address. Each time you append to the address, you must set proteins[index] to an empty character.

Once that's done, you should have k87ztrc3ux as a result.

Thank you, thats the kind of explanation I was looking for :)/>

I have send you 150 KST in return for your help, is that OK?
Lignum #6
Posted 24 June 2016 - 10:22 PM
Thank you, thats the kind of explanation I was looking for :)/>

I have send you 150 KST in return for your help, is that OK?

You're welcome, and thanks for the donation! :)/>