trezoa-sas-lib 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/src/generated/accounts/attestation.d.ts +40 -0
  2. package/dist/src/generated/accounts/attestation.js +69 -0
  3. package/dist/src/generated/accounts/credential.d.ts +27 -0
  4. package/dist/src/generated/accounts/credential.js +61 -0
  5. package/dist/src/generated/accounts/index.d.ts +10 -0
  6. package/dist/src/generated/accounts/index.js +26 -0
  7. package/dist/src/generated/accounts/schema.d.ts +31 -0
  8. package/dist/src/generated/accounts/schema.js +69 -0
  9. package/dist/src/generated/index.d.ts +11 -0
  10. package/dist/src/generated/index.js +27 -0
  11. package/dist/src/generated/instructions/changeAuthorizedSigners.d.ts +51 -0
  12. package/dist/src/generated/instructions/changeAuthorizedSigners.js +93 -0
  13. package/dist/src/generated/instructions/changeSchemaDescription.d.ts +56 -0
  14. package/dist/src/generated/instructions/changeSchemaDescription.js +96 -0
  15. package/dist/src/generated/instructions/changeSchemaStatus.d.ts +50 -0
  16. package/dist/src/generated/instructions/changeSchemaStatus.js +82 -0
  17. package/dist/src/generated/instructions/changeSchemaVersion.d.ts +60 -0
  18. package/dist/src/generated/instructions/changeSchemaVersion.js +107 -0
  19. package/dist/src/generated/instructions/closeAttestation.d.ts +56 -0
  20. package/dist/src/generated/instructions/closeAttestation.js +102 -0
  21. package/dist/src/generated/instructions/closeTokenizedAttestation.d.ts +74 -0
  22. package/dist/src/generated/instructions/closeTokenizedAttestation.js +124 -0
  23. package/dist/src/generated/instructions/createAttestation.d.ts +67 -0
  24. package/dist/src/generated/instructions/createAttestation.js +100 -0
  25. package/dist/src/generated/instructions/createCredential.d.ts +52 -0
  26. package/dist/src/generated/instructions/createCredential.js +92 -0
  27. package/dist/src/generated/instructions/createSchema.d.ts +63 -0
  28. package/dist/src/generated/instructions/createSchema.js +105 -0
  29. package/dist/src/generated/instructions/createTokenizedAttestation.d.ts +110 -0
  30. package/dist/src/generated/instructions/createTokenizedAttestation.js +146 -0
  31. package/dist/src/generated/instructions/emitEvent.d.ts +36 -0
  32. package/dist/src/generated/instructions/emitEvent.js +69 -0
  33. package/dist/src/generated/instructions/index.d.ts +19 -0
  34. package/dist/src/generated/instructions/index.js +35 -0
  35. package/dist/src/generated/instructions/tokenizeSchema.d.ts +67 -0
  36. package/dist/src/generated/instructions/tokenizeSchema.js +106 -0
  37. package/dist/src/generated/programs/index.d.ts +8 -0
  38. package/dist/src/generated/programs/index.js +24 -0
  39. package/dist/src/generated/programs/trezoaAttestationService.d.ts +57 -0
  40. package/dist/src/generated/programs/trezoaAttestationService.js +74 -0
  41. package/dist/src/generated/shared/index.d.ts +49 -0
  42. package/dist/src/generated/shared/index.js +94 -0
  43. package/dist/src/generated/types/closeAttestationEvent.d.ts +17 -0
  44. package/dist/src/generated/types/closeAttestationEvent.js +36 -0
  45. package/dist/src/generated/types/index.d.ts +8 -0
  46. package/dist/src/generated/types/index.js +24 -0
  47. package/dist/src/index.d.ts +3 -0
  48. package/dist/src/index.js +19 -0
  49. package/dist/src/pdas.d.ts +69 -0
  50. package/dist/src/pdas.js +96 -0
  51. package/dist/src/utils.d.ts +21 -0
  52. package/dist/src/utils.js +98 -0
  53. package/dist/test/pdas.d.ts +1 -0
  54. package/dist/test/pdas.js +61 -0
  55. package/dist/test/utils.d.ts +1 -0
  56. package/dist/test/utils.js +29 -0
  57. package/package.json +29 -0
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deserializeAttestationData = exports.serializeAttestationData = exports.convertSasSchemaToBorshSchema = void 0;
4
+ const borsher_1 = require("borsher");
5
+ // A char type does not exist on BorshSchema, so we (de)serialize
6
+ // using the 4 byte representation.
7
+ const CHAR_SCHEMA = borsher_1.BorshSchema.Array(borsher_1.BorshSchema.u8, 4);
8
+ /**
9
+ * Maps the SAS compact byte layout to the equivalent data type.
10
+ */
11
+ const compactLayoutMapping = {
12
+ 0: borsher_1.BorshSchema.u8,
13
+ 1: borsher_1.BorshSchema.u16,
14
+ 2: borsher_1.BorshSchema.u32,
15
+ 3: borsher_1.BorshSchema.u64,
16
+ 4: borsher_1.BorshSchema.u128,
17
+ 5: borsher_1.BorshSchema.i8,
18
+ 6: borsher_1.BorshSchema.i16,
19
+ 7: borsher_1.BorshSchema.i32,
20
+ 8: borsher_1.BorshSchema.i64,
21
+ 9: borsher_1.BorshSchema.i128,
22
+ 10: borsher_1.BorshSchema.bool,
23
+ 11: CHAR_SCHEMA,
24
+ 12: borsher_1.BorshSchema.String,
25
+ 13: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.u8),
26
+ 14: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.u16),
27
+ 15: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.u32),
28
+ 16: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.u64),
29
+ 17: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.u128),
30
+ 18: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.i8),
31
+ 19: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.i16),
32
+ 20: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.i32),
33
+ 21: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.i64),
34
+ 22: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.i128),
35
+ 23: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.bool),
36
+ 24: borsher_1.BorshSchema.Vec(borsher_1.BorshSchema.String),
37
+ 25: CHAR_SCHEMA,
38
+ };
39
+ const MAX_LAYOUT_VALUE = 25;
40
+ /**
41
+ * Given the onchain representation of a Schema, we generate a Borsh schema
42
+ * for (de)serialization.
43
+ * @param schema
44
+ * @returns
45
+ */
46
+ const convertSasSchemaToBorshSchema = (schema) => {
47
+ const textDecoder = new TextDecoder();
48
+ const fields = splitJoinedVecs(Uint8Array.from(schema.fieldNames)).map((f) => textDecoder.decode(Uint8Array.from(f)));
49
+ if (fields.length !== schema.layout.length) {
50
+ throw new Error("Schema field names and layout do not match");
51
+ }
52
+ return borsher_1.BorshSchema.Struct(fields.reduce((acc, field, index) => {
53
+ const layoutByte = schema.layout[index];
54
+ if (layoutByte > MAX_LAYOUT_VALUE) {
55
+ throw new Error("Invalid Schema layout value");
56
+ }
57
+ acc[field] = compactLayoutMapping[layoutByte];
58
+ return acc;
59
+ }, {}));
60
+ };
61
+ exports.convertSasSchemaToBorshSchema = convertSasSchemaToBorshSchema;
62
+ /**
63
+ * Given a SAS Schema and an object that represents the Attestation data,
64
+ * serialize the Attestation data to valid byte array.
65
+ * @param schema
66
+ */
67
+ const serializeAttestationData = (schema, data) => {
68
+ const borshSchema = (0, exports.convertSasSchemaToBorshSchema)(schema);
69
+ return borshSchema.serialize(data);
70
+ };
71
+ exports.serializeAttestationData = serializeAttestationData;
72
+ /**
73
+ * Given a SAS Schema and a byte array of Attestation data,
74
+ * deserialize the Attestation data to an object.
75
+ * @param schema
76
+ */
77
+ const deserializeAttestationData = (schema, data) => {
78
+ const borshSchema = (0, exports.convertSasSchemaToBorshSchema)(schema);
79
+ return borshSchema.deserialize(data);
80
+ };
81
+ exports.deserializeAttestationData = deserializeAttestationData;
82
+ const splitJoinedVecs = (bytes) => {
83
+ let offset = 0;
84
+ const ret = [];
85
+ while (offset < bytes.length) {
86
+ const len = u32FromLeBytes(bytes.slice(offset, offset + 4));
87
+ offset += 4;
88
+ ret.push(bytes.slice(offset, offset + len));
89
+ offset += len;
90
+ }
91
+ return ret;
92
+ };
93
+ const u32FromLeBytes = (bytes) => {
94
+ if (bytes.length !== 4) {
95
+ throw new Error("Input must be a 4-byte array");
96
+ }
97
+ return ((bytes[0] << 0) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
98
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const chai_1 = require("chai");
4
+ const kit_1 = require("@trezoa/kit");
5
+ const src_1 = require("../src");
6
+ describe("PDAs", () => {
7
+ describe("PDA derivation", () => {
8
+ it("should derive a credential PDA", async () => {
9
+ const issuer = (0, kit_1.address)('E15NNEiGt7BQvuyXpVeCsbS3uQY7jd8QtrUrWc1fH425');
10
+ const credentialName = 'test';
11
+ const expectedPda = ['98ay9m4KXXbQktfg5w1DSgbcyyjyHecvNtqcMn2BwL5M', 254];
12
+ const testPda = await (0, src_1.deriveCredentialPda)({ authority: issuer, name: credentialName });
13
+ chai_1.assert.deepEqual(testPda, expectedPda);
14
+ });
15
+ it("should derive a schema PDA", async () => {
16
+ const credential = (0, kit_1.address)('A1ghCNKvMCPKqzjdvnjYk3YFR13RiP8dh9HxBetr9kon');
17
+ const schemaName = 'test';
18
+ const version = 1;
19
+ const expectedPda = ['DKc1A7uuVyKsY3jCmQAKzdcAp16g5pUKJshAszdRQFbw', 254];
20
+ const testPda = await (0, src_1.deriveSchemaPda)({
21
+ credential,
22
+ version,
23
+ name: schemaName
24
+ });
25
+ chai_1.assert.deepEqual(testPda, expectedPda);
26
+ });
27
+ it("should derive an attestation PDA", async () => {
28
+ const credential = (0, kit_1.address)('FMdNo2Rfg6DUCdHCwZCC3wLkYn53JQCoMgzfXfqTH5Uv');
29
+ const schema = (0, kit_1.address)('BdaSyg8ZJRbbMpx24i2ugFcYaPagQBtpHUqBfcZaF9LJ');
30
+ const nonce = (0, kit_1.address)('3axwvYuBmgFPc1pexoccctra5LkUxZGXEjyakAMMtm5m');
31
+ const expectedPda = ['3e3K3DfNNNfgMTKeWuRA3VPKkpTEHVHbfpJz3BJHpmKB', 255];
32
+ const testPda = await (0, src_1.deriveAttestationPda)({
33
+ credential,
34
+ schema,
35
+ nonce
36
+ });
37
+ chai_1.assert.deepEqual(testPda, expectedPda);
38
+ });
39
+ it("should derive a schema mint PDA", async () => {
40
+ const schema = (0, kit_1.address)('7Wks2GbXrr22HkVzgLbfJ9z6pacZGHYk7p5B9amoCSDW');
41
+ const expectedPda = ['8UKqFzWzgrakFaJijkabRbkKCom2KbJGJjbu3SX7t1Qq', 254];
42
+ const testPda = await (0, src_1.deriveSchemaMintPda)({
43
+ schema,
44
+ });
45
+ chai_1.assert.deepEqual(testPda, expectedPda);
46
+ });
47
+ it("should derive an attestation mint PDA", async () => {
48
+ const attestation = (0, kit_1.address)('99Ui29EecR3uVZ9oLJzX4XdWDeKvoYUpK1dMupc3eAgn');
49
+ const expectedPda = ['B5TxRjVzoyLoRNq83BRg7g63dUAKrD3XCCoMxBxsfZUk', 253];
50
+ const testPda = await (0, src_1.deriveAttestationMintPda)({
51
+ attestation,
52
+ });
53
+ chai_1.assert.deepEqual(testPda, expectedPda);
54
+ });
55
+ it("should derive event authority pda PDA", async () => {
56
+ const expectedPda = '9qmfijJ9MX4XVGdgWB64UuGUb19guVXrpn1HxweCybVb';
57
+ const testPda = await (0, src_1.deriveEventAuthorityAddress)();
58
+ chai_1.assert.deepEqual(testPda, expectedPda);
59
+ });
60
+ });
61
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const chai_1 = require("chai");
4
+ const generated_1 = require("../src/generated");
5
+ const utils_1 = require("../src/utils");
6
+ describe("Utils", () => {
7
+ const schemaAccountBytes = Uint8Array.from([
8
+ 1, 147, 244, 210, 208, 208, 76, 164, 106, 193, 96, 129, 24, 152, 59, 215,
9
+ 13, 112, 136, 111, 235, 117, 29, 128, 253, 99, 200, 171, 204, 126, 178, 74,
10
+ 175, 9, 0, 0, 0, 116, 101, 115, 116, 95, 100, 97, 116, 97, 20, 0, 0, 0, 115,
11
+ 99, 104, 101, 109, 97, 32, 102, 111, 114, 32, 116, 101, 115, 116, 32, 100,
12
+ 97, 116, 97, 2, 0, 0, 0, 12, 0, 20, 0, 0, 0, 4, 0, 0, 0, 110, 97, 109, 101,
13
+ 8, 0, 0, 0, 108, 111, 99, 97, 116, 105, 111, 110, 0, 1,
14
+ ]);
15
+ describe("convertSasSchemaToBorshSchema", () => {
16
+ it("should convert SAS Schema to a proper BorshSchema", () => {
17
+ const decoder = (0, generated_1.getSchemaDecoder)();
18
+ const schema = decoder.decode(schemaAccountBytes);
19
+ const borshSchema = (0, utils_1.convertSasSchemaToBorshSchema)(schema);
20
+ const testData = {
21
+ name: "hello",
22
+ location: 10,
23
+ };
24
+ const serialized = borshSchema.serialize(testData);
25
+ const deserialized = borshSchema.deserialize(serialized);
26
+ chai_1.assert.deepEqual(testData, deserialized);
27
+ });
28
+ });
29
+ });
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "trezoa-sas-lib",
3
+ "version": "1.0.11",
4
+ "repository": "git@github.com:trzledgerfoundation/trezoa-attestation-service.git",
5
+ "main": "dist/src/index.js",
6
+ "types": "dist/src/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "devDependencies": {
11
+ "@types/chai": "^5.2.2",
12
+ "@types/mocha": "^10.0.10",
13
+ "@types/node": "^20.11.24",
14
+ "chai": "^4.4.1",
15
+ "mocha": "^10.8.2",
16
+ "ts-node": "^10.9.2",
17
+ "typescript": "^5.0.0"
18
+ },
19
+ "dependencies": {
20
+ "@trezoa/addresses": "^5.1.1",
21
+ "@trezoa/kit": "^5.1.0",
22
+ "borsh": "^2.0.0",
23
+ "borsher": "^4.0.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "test": "mocha --require ts-node/register 'test/**/*'"
28
+ }
29
+ }