Skip to content

Commit

Permalink
fix to make serialization and deserializations of zenroom work
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Jun 22, 2022
1 parent f1dea55 commit 3c1e90e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/schemas/fulfillment.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Ed25519Sha256Fulfillment = asn.define('Ed25519Sha256Fulfillment', f
)
})

export const ZenroomSha256Fulfillment = asn.define('Ed25519Sha256Fulfillment', function () {
export const ZenroomSha256Fulfillment = asn.define('ZenroomSha256Fulfillment', function () {
this.seq().obj(
this.key('script').implicit(0).octstr(),
this.key('data').implicit(1).octstr(),
Expand All @@ -51,6 +51,7 @@ export const Fulfillment = asn.define('Fulfillment', function () {
prefixSha256Fulfillment: this.implicit(1).use(PrefixFulfillment),
thresholdSha256Fulfillment: this.implicit(2).use(ThresholdFulfillment),
rsaSha256Fulfillment: this.implicit(3).use(RsaSha256Fulfillment),
ed25519Sha256Fulfillment: this.implicit(4).use(Ed25519Sha256Fulfillment)
ed25519Sha256Fulfillment: this.implicit(4).use(Ed25519Sha256Fulfillment),
zenroomSha256Fulfillment: this.implicit(5).use(ZenroomSha256Fulfillment)
})
})
2 changes: 1 addition & 1 deletion src/types/base-sha256.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class BaseSha256 extends Fulfillment {
}
}

export default BaseSha256;
export default BaseSha256;
39 changes: 18 additions & 21 deletions src/types/zenroom-sha256.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ class ZenroomSha256 extends BaseSha256 {
this.keys = keys
}

getScript () {
return this.script
}

getData () {
return this.data
}

getKeys () {
return this.keys
}

parseJson (json) {
console.log(json)
this.setData(json.data)
this.setScript(json.script.toString('utf-8'))
this.setData(JSON.parse(json.data.toString('utf-8')))
this.setKeys(JSON.parse(json.keys.toString('utf-8')))
}

/**
Expand All @@ -71,8 +84,8 @@ class ZenroomSha256 extends BaseSha256 {
getAsn1JsonPayload () {
return {
script: this.script,
data: this.data,
keys: this.keys
data: JSON.stringify(this.data),
keys: JSON.stringify(this.keys)
}
}

Expand All @@ -98,23 +111,7 @@ class ZenroomSha256 extends BaseSha256 {
* @return {Boolean} Whether this fulfillment is valid.
*/
validate (message) {
if (!Buffer.isBuffer(message)) {
throw new TypeError('Message must be a Buffer')
}

// Use native library if available (~60x faster)
let result
if (ed25519) {
result = ed25519.Verify(message, this.signature, this.publicKey)
} else {
result = sign.detached.verify(bufferToUint8Array(message), bufferToUint8Array(this.signature), bufferToUint8Array(this.publicKey))
}

if (result !== true) {
throw new ValidationError('Invalid ed25519 signature')
}

return true
throw new Error('Not implemented yet')
}
}

Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import PrefixSha256 from './types/prefix-sha256';
import ThresholdSha256 from './types/threshold-sha256';
import RsaSha256 from './types/rsa-sha256';
import Ed25519Sha256 from './types/ed25519-sha256';
import ZenroomSha256 from './types/ed25519-sha256';
import ZenroomSha256 from './types/zenroom-sha256';
import base64url from './util/base64url';

export { base64url, Condition, Fulfillment, TypeRegistry };
Expand Down
8 changes: 8 additions & 0 deletions types/types/zenroom-sha256.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export default class ZenroomSha256 extends BaseSha256 {

constructor();

setScript(script: string): void;
setData(data: Record<string, any>): void;
setKeys(keys: Record<string, any>): void;

getScript(): string;
getData(): Record<string, any>;
getKeys(): Record<string, any>;

parseJson(json: ZenroomSha256Json): void;

private getFingerprintContents(): Buffer;
Expand Down

0 comments on commit 3c1e90e

Please sign in to comment.