SmartTokenLabs / attestation

Paper and implementation of blockchain attestations
MIT License
48 stars 10 forks source link

JS data object class (in src/main/javascript/) should support both parse and serialise #65

Open SmartLayer opened 3 years ago

SmartLayer commented 3 years ago
  1. data object can be instansiated by a dictionary object
  2. once data object is instansiated, you can call dataobj.serialize() to get a DER-encoded ArrayBuffer.
  3. the test cases be updated so that it instansiat a data object from a DEF file, then another data object is created from this one, then the result is serialized and produce an identical file. That is:
dataobj = new SignedDevconTciket(bufferFromFile)
bufferToFile = (new SignedDevconTicket(dataobj)).serialize()

The from and to files should end up identical.

darakhbharat commented 3 years ago

Hi Weiwu,

I have implemented the serialize method and updated the test cases in ParserSerializerTest.js. I am pushing my changes in branch - asn1js-parser-serializer

what I have tried is get Object from the existing der file and serialize it to new file. It creates exactly same DER data.

//get DER from object
let filePath="build/test-results/signed-devcon-ticket-new.der";
let finalDER;
finalDER = (new SignedDevconTicket(dataobj3)).serialize();
writeFileSync(filePath,  finalDER);
console.log("DER data is written to file"+filePath);

Now next goal is to instantiate dictionary object to be serialized.

// instantiate by a dictionary object and ENCODE it
let dataobj4;
dataobj4 = new SignedDevconTicket({
    ticket: {
        devconId:Uint8Array.from('6').buffer,
        ticketId: Uint8Array.from('48646').buffer,
        ticketClass:Uint8Array.from('0').buffer
    },

    commitment: new Uint8Array([4, 65, 4, 32, 100, 48, -54, -30, 2, -27, -6, -76, 28, -27, 75, 116, 114, 94, 69, 79, -8, 8, 114, 10, 3, -112, 55, -40, 64, 61, -107, -2, -3, -64, 53, 18, 44, -107, -65, -46, -82, 26, 72, -93, -12, 47, 11, -114, 120, -5, 121, 35, 81, -83, -30, 10, 34, 9, -93, 31, 78, -112, -36, 97, -92, -43, 15]).buffer,

    signatureValue: new Uint8Array([48, 68, 2, 32, 56, -37, 33, -74, -75, -73, -58, -110, -38, -83, -94, -74, 46, -69, -119, -27, -93, 110, 58, 60, -50, 102, 30, 56, 83, 43, -55, -84, -56, 92, 52, 27, 2, 32, 112, 70, 115, 33, -113, 119, -78, 71, -75, 81, -85, 60, 61, 116, -31, -17, -113, 79, 126, 58, -32, 64, 29, 83, 84, 38, 101, 58, -86, 94, -62, -94]).buffer

});
let finalDER2 = dataobj4.serialize();
writeFileSync('build/test-results/signed-devcon-ticket-new-2.der',  finalDER2);

I know value of the commitment and signatureValue is wrong here and needs to be calculated using PKI.js crypto stuff.

I am studying the Java code and found that computation of commitment and signatureValue is complex.

this.commitment = AttestationCrypto.makeCommitment(mail, AttestationType.EMAIL, secret);
this.signature = SignatureUtility.signDeterministic(asn1Tic.getEncoded(), keys.getPrivate());

My question is do we need to implement everything in JS same as what we are doing in Java, like shown above? i.e. Using the email and the secret compute the commitment and signatureValue ?

Thanks!

jot2re commented 3 years ago

Hi @darakhbharat, sorry if I am misunderstanding the specific problem/assignment, but @oleggrib already implemented the AttetsationCrypto in javascript, so you can use his code to compute these aspects and then your code to (de)serialise. I think what you are specifically looking for is this https://github.com/TokenScript/attestation/blob/javascript-crypto/src/main/javascript/crypto/src/libs/AttestationCrypto.ts

darakhbharat commented 3 years ago

Hi @darakhbharat, sorry if I am misunderstanding the specific problem/assignment, but @oleggrib already implemented the AttetsationCrypto in javascript, so you can use his code to compute these aspects and then your code to (de)serialise. I think what you are specifically looking for is this https://github.com/TokenScript/attestation/blob/javascript-crypto/src/main/javascript/crypto/src/libs/AttestationCrypto.ts

Great! Thanks! I will check this out and see how can I integrate it with my code.

darakhbharat commented 3 years ago

Hi Weiwu, Oleh,

The current status of the signedDevconTicket is almost near to completion except dictionary object initilization for commitment and signatureValue.

commitment:

I have tried port the Oleh's code and use it for the same but here are the issues I am facing for makeCommitment.

_alphawallet\attestation\src\main\javascript\cryptojs\lib\AttestationCrypto.js

makeSecret(bytes = 48) {
    var array = new Uint8Array(bytes);
    if(typeof(window) !== 'undefined')
        window.crypto.getRandomValues(array);
    else
        getRandomValues(array);
    let output = '0x';
...

TypeError: getRandomValues is not a function. I have imported this as constant and added "get-random-values": "^1.2.2"dependency in package.json.

As we are not testing it on browser we don't have a window object hence we will need a else part for the same as shown in above piece of code.

signatureValue: I have not worked on it yet.

Oleh may have a quick suggestion / solution for this.

Weiwu, If we need JS version of implemeting all these data module( not TypeScript ) then yes we have to convert TypeScript to JavaScript.

SmartLayer commented 3 years ago

Very good news and I'll check this morning

oleggrib commented 3 years ago

Hi, Bharat, can you chech last version of the code? https://github.com/TokenScript/attestation/blob/javascript-crypto/src/main/javascript/crypto/src/libs/AttestationCrypto.ts#L193-L213 it should work for you.