Skip to content

Instantly share code, notes, and snippets.

@miukki
Created April 23, 2018 16:24
Show Gist options
  • Save miukki/dc4e0d981294af313816bedd85be8045 to your computer and use it in GitHub Desktop.
Save miukki/dc4e0d981294af313816bedd85be8045 to your computer and use it in GitHub Desktop.
crypto-test-task
# cryptography-task
## Get Started
### Installation
```
npm install --save cryptography-task
```
### Example
https://runkit.com/miukki/cryptography-task
### Test
```bash
yarn test -- --watch -u
#-u update jest snapshots
```
### Task
```bash
requirements:
entry point : ByteStream.cs line 121 public void Write(Transaction)
the idea is to convert an JSON to -> byte array -> base64 String
example
JSON input :
{"Hash":"DWQnIVCxKZ+/XA2Gd5XFrlGJ2JMTuknP6236rl77MsQ=","Expire":636587692108677703,"Inputs":[{"Address":"qyl68tygnjx6qqwrsmynmejmc9wxlw7almv3397j","Currency":"BTC","Amount":10.0},{"Address":"qyaj20aksyvxlfmznyjdqzxrvvf0w7ca7mamwzll","Currency":"LTC","Amount":30.0}],"Outputs":[{"Address":"qyaj20aksyvxlfmznyjdqzxrvvf0w7ca7mamwzll","Currency":"BTC","Amount":10.0},{"Address":"qyl68tygnjx6qqwrsmynmejmc9wxlw7almv3397j","Currency":"LTC","Amount":30.0}],"Message":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000","Fees":null}
Base64 byte output :
byte array -> base64 (represent binary data in an ASCII string format by translating it into a radix-64 representation)
RwKGGCKd1QgAAAIB/o6yInI2gAcOGyT3mW8Fcb7vd/uUBADKmjsAAAAAAeyU/toEYb6dimSTQCMNjEvd7Hfb/B4AXtCyAAAAAAIB7JT+2gRhvp2KZJNAIw2MS93sd9uUBADKmjsAAAAAAf6OsiJyNoAHDhsk95lvBXG+73f7/B4AXtCyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
```
1. Entry point
public void Write(Transaction transaction) [line 121]
1. Prepare Buffer Array in nodejs
2. Write(transaction.Expire): Json -> Expire (Write(transaction.Expire)) [lin 123]-> use Buffer Array -> user method protected void Write(long longData) [line 46] > user stream.Write(BitConverter.GetBytes(longData), 0, 8) it is meaning 8 bytes put to Buffer Array
3. WriteNullable(transaction.Fees, Write) -> got to [line 33] -> private void WriteNullable<T>(T data, Action<T> write) -> then stream.Write(BitConverter.GetBytes(value), 0, 1) [line 43] -> BitConverter.GetBytes(value), 0, 1) take single byte value 00 OR 01 (true or false) -> write to Buffer Array . BTW in our case only {Fees: null} look to JSON
about bit converter source : https://msdn.microsoft.com/en-us/library/system.bitconverter(v=vs.110).aspx
4. I dont have Declarations in json, Write(transaction.Declarations.ToList(), Write); [line 125] -> [line 92] public void Write(TxDeclaration declaration) ? -> [line 379] private TxDeclaration ReadTxDeclaration() ? -> For declaration if the list is empty (null), just write 1 byte to 0. Idea is if something null or empt dont put it in the json.
5. Write(transaction.Inputs.ToList(), Write) -> [line 56] public void Write<T>(List<T> items, Action<T> write, int bytes = 1) -> put items.Count (same as array.length) put to Buffer Array N bite . You can specify N by default it is equal 1
actually we also write Array to buffer Bite look to
foreach (var item in items) [line 62] -> [line 85] public void Write(TxInputOutput input) :
- Write(input.Address.ToRawBytes()) : prop item Address.ToRawBytes() [line 87] -> byte array public byte[] ToRawBytes() -> Address32Format (Address32 fromat can be using the js sdk bech32.js)-> stream.Write(bytes, 0, bytes.Length); [line 194] write to buffer stream.Write(bytes, 0, bytes.Length)
- prop item Currency [line 88] Write(input.Currency); -> stream.Write(BitConverter.GetBytes(currency), 0, 2); Currency is a wrapper for short 2bytes
- prop iten Amount [line 89] -> Amount is wrapper for long 8bytes - stream.Write(BitConverter.GetBytes(amount), 0, 8);
6. Write(transaction.Outputs.ToList(), Write); same as Write(transaction.Inputs.ToList(), Write)
7. Message str 301 missing
return new TransactionMessage(stream.ReadBytes(TransactionMessage.SIZE)); > str 51 > 192
8. convert array buffer to string base64 > done
9. please agreed output is real and input it is real for debugging i have to know it.
Some types are in the same file then other classes. Look at payment.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment