Skip to content

Commit

Permalink
fix: test cleanup and improve verbosity & API
Browse files Browse the repository at this point in the history
- Cleanup HTML test, move out of main class file.
- Improve verbosity of API & private members.
- Add hex & base64 format support.
  • Loading branch information
micwallace committed Aug 1, 2022
1 parent 6468eff commit e4e43ec
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 123 deletions.
87 changes: 86 additions & 1 deletion src/main/javascript/crypto/parser-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,93 @@
</head>
<body>
<script>
function test(){
let generatedSchema = new SchemaGenerator({
ticket: {
name: "Ticket",
items: {
devconId: {
type: "Utf8String",
optional: false
},
ticketIdNumber: {
type: "Integer",
optional: true
},
ticketIdString: {
type: "Utf8String",
optional: true
},
ticketClass: {
type: "Integer",
optional: false
},
linkedTicket: {
name: "Linked Ticket",
items: {
devconId: {
type: "Utf8String",
optional: false
},
ticketIdNumber: {
type: "Integer",
optional: true
},
ticketIdString: {
type: "Utf8String",
optional: true
},
ticketClass: {
type: "Integer",
optional: false
}
}
}
}
},
commitment: {
type: "OctetString",
optional: true
},
signatureValue: {
type: "BitString",
optional: false
}
});

let asnObject = generatedSchema.getSchemaObject();

asnObject.ticket.devconId = "6";
asnObject.ticket.ticketIdNumber = 10;
asnObject.ticket.ticketClass = 1;

asnObject.ticket.linkedTicket.devconId = "6";
asnObject.ticket.linkedTicket.ticketIdNumber = 10;
asnObject.ticket.linkedTicket.ticketClass = 1;

asnObject.signatureValue = new Uint8Array([
177, 53, 222, 215, 60, 2, 17, 132, 21, 143, 166,
234, 145, 239, 240, 169, 119, 83, 242, 113, 99, 243,
179, 90, 87, 211, 250, 197, 113, 70, 191, 10, 69,
121, 82, 36, 254, 251, 149, 237, 222, 125, 213, 90,
21, 84, 130, 155, 91, 226, 15, 62, 57, 177, 251,
39, 165, 43, 214, 57, 114, 209, 232, 156, 28
]);

console.log("Populated schema object: ");
console.log(asnObject);

let encoded = generatedSchema.serializeAndFormat(asnObject);

console.log("Encoded: ");
console.log(encoded);

let decoded = generatedSchema.parse(encoded);

console.log("Decoded: ");
console.log(decoded);
}
</script>
<button onclick="(new Meh()).meh();">Run Test</button>
<button onclick="test();">Run Test</button>
</body>
</html>
5 changes: 2 additions & 3 deletions src/main/javascript/crypto/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {Authenticator} from "./Authenticator";
import {Eip712AttestationRequest} from "./libs/Eip712AttestationRequest";
import {AttestationCrypto} from "./libs/AttestationCrypto";
import {IntegrationExample} from "./IntegrationExample";
import {Meh, SchemaGenerator} from "./util/SchemaGenerator";
import {SchemaGenerator} from "./util/SchemaGenerator";

(window as any).Authenticator = Authenticator;
(window as any).Attest = Eip712AttestationRequest;
(window as any).AttestationCrypto = AttestationCrypto;
(window as any).IntegrationExample = IntegrationExample;
(window as any).SchemaGenerator = SchemaGenerator;
(window as any).Meh = Meh;
(window as any).SchemaGenerator = SchemaGenerator;
116 changes: 85 additions & 31 deletions src/main/javascript/crypto/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,8 @@ import {Issuer} from "./libs/Issuer";
import { AttestedObject } from './libs/AttestedObject';
import { AttestableObject } from './libs/AttestableObject';
import { UseToken } from './asn1/shemas/UseToken';
import subtle from "./safe-connect/SubtleCryptoShim";
import {EthereumAddressAttestation} from "./safe-connect/EthereumAddressAttestation";
import {EthereumKeyLinkingAttestation} from "./safe-connect/EthereumKeyLinkingAttestation";
import {SchemaGenerator} from "./util/SchemaGenerator";
import {DevconTicket, SignedDevconTicket} from "./asn1/shemas/SignedDevconTicket";
import * as util from "util";
import * as asn1_schema_1 from "@peculiar/asn1-schema";
import {AsnParser, AsnPropTypes, AsnSerializer} from "@peculiar/asn1-schema";
import {utils} from "ethers";
const url = require('url');

let EC = require("elliptic");
Expand Down Expand Up @@ -689,33 +682,94 @@ describe("Schema Generator", function(){

test("Serialize & parse ASN based on a dynamic schema", async function(){

let schemaGenerator = new SchemaGenerator();

let GeneratedSchema = schemaGenerator.getSchemaObject();

console.log(util.inspect(GeneratedSchema));

let currentSchema = new GeneratedSchema();

currentSchema.ticket.devconId = "6";
currentSchema.ticket.ticketIdNumber = 10;
currentSchema.ticket.ticketClass = 1;

currentSchema.ticket.linkedTicket.devconId = "6";
currentSchema.ticket.linkedTicket.ticketIdNumber = 10;
currentSchema.ticket.linkedTicket.ticketClass = 1;

currentSchema.signatureValue = new Uint8Array(hexStringToUint8("0xb135ded73c021184158fa6ea91eff0a97753f27163f3b35a57d3fac57146bf0a45795224fefb95edde7dd55a1554829b5be20f3e39b1fb27a52bd63972d1e89c1c"));

console.log(currentSchema);

let encoded = AsnSerializer.serialize(currentSchema);
let generatedSchema = new SchemaGenerator({
ticket: {
name: "Ticket",
items: {
devconId: {
type: "Utf8String",
optional: false
},
ticketIdNumber: {
type: "Integer",
optional: true
},
ticketIdString: {
type: "Utf8String",
optional: true
},
ticketClass: {
type: "Integer",
optional: false
},
linkedTicket: {
name: "Linked Ticket",
items: {
devconId: {
type: "Utf8String",
optional: false
},
ticketIdNumber: {
type: "Integer",
optional: true
},
ticketIdString: {
type: "Utf8String",
optional: true
},
ticketClass: {
type: "Integer",
optional: false
}
}
}
}
},
commitment: {
type: "OctetString",
optional: true
},
signatureValue: {
type: "BitString",
optional: false
}
});

let asnObject = generatedSchema.getSchemaObject();

asnObject.ticket.devconId = "6";
asnObject.ticket.ticketIdNumber = 5;
asnObject.ticket.ticketClass = 1;

asnObject.ticket.linkedTicket.devconId = "6";
asnObject.ticket.linkedTicket.ticketIdNumber = 10;
asnObject.ticket.linkedTicket.ticketClass = 2;

asnObject.signatureValue = new Uint8Array(hexStringToUint8("0xb135ded73c021184158fa6ea91eff0a97753f27163f3b35a57d3fac57146bf0a45795224fefb95edde7dd55a1554829b5be20f3e39b1fb27a52bd63972d1e89c1c"));

console.log(asnObject);

let encoded = generatedSchema.serializeAndFormat(asnObject);

console.log("Encoded: ");
console.log(encoded);

let decoded = generatedSchema.parse(encoded);

console.log("Decoded: ");
console.log(decoded);

console.log(uint8tohex(new Uint8Array(encoded)));
// Can't do a single match here because nested properties are set in the prototype
expect(asnObject.ticket.devconId).toBe("6");
expect(asnObject.ticket.ticketIdNumber).toBe(5);
expect(asnObject.ticket.ticketClass).toBe(1);

let decoded = AsnParser.parse(encoded, GeneratedSchema);
expect(asnObject.ticket.linkedTicket.devconId).toBe("6");
expect(asnObject.ticket.linkedTicket.ticketIdNumber).toBe(10);
expect(asnObject.ticket.linkedTicket.ticketClass).toBe(2);

console.log(decoded);
decoded.signatureValue = new Uint8Array(decoded.signatureValue);
expect(asnObject.signatureValue).toEqual(decoded.signatureValue);
});
});

Expand Down
Loading

0 comments on commit e4e43ec

Please sign in to comment.