diff --git a/README.md b/README.md index 755c039..131ccb9 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,44 @@ npm i @tradetrust-tt/tradetrust-core ## Basic Usage +#### Wrapping and Signing of verifiable document + +This example provides how to sign tradetrust wrapped verifiable document, as well as a public/private key pair or an [Ethers.js Signer](https://docs.ethers.io/v5/api/signer/). +Replace `` and `` with your actual wallet address and private key. + +```ts +import { + wrapDocumentsV2, + signDocument, + isSignedWrappedV2Document, + SUPPORTED_SIGNING_ALGORITHM, +} from '@tradetrust-tt/tradetrust-core' + +const document = { + // raw v2 document with dns-did as identitify proof +} as any + +async function start() { + const wrappedDocuments = wrapDocumentsV2([document]) + const wrappedDocument = wrappedDocuments[0] + + const signedDocument = await signDocument( + wrappedDocument, + SUPPORTED_SIGNING_ALGORITHM.Secp256k1VerificationKey2018, + { + public: 'did:ethr:#controller', + private: '', + } + ) + // check if the document has already wrapped and signed + console.log(isSignedWrappedV2Document(signedDocument)) +} + +start() +``` + +#### Verifying + This example provides how to verify tradetrust document using your own provider configurations. ```ts @@ -57,6 +95,30 @@ tradetrust-core provides the following methods for document verification and val It generates receives provider options and returns the ethereum JSON RPC provider to be used for [verify](#verify) method. +#### `wrapDocumentsV2` + +It takes in array of Tradetrust v2 documents and returns the wrapped documents. + +#### `wrapDocumentsV3` + +It takes in array of Tradetrust v3 documents and returns the wrapped documents. + +#### `obfuscateDocument` + +It removes a key-value pair from the document's data section, without causing the file hash to change. This can be used to generate a new document containing a subset of the original data, yet allow the recipient to proof the provenance of the document. + +#### `getDataV2` + +It returns the original data stored in the Tradetrust v2 document, in a readable format. + +#### `diagnose` + +Tool to find out why a document is not a valid open attestation file (wrapped or signed document) + +#### `signDocument` + +It takes a wrapped document, a wallet (public and private key pair) or an Ethers.js Signer. The method will sign the merkle root from the wrapped document, append the signature to the document and return it. Currently, it supports `Secp256k1VerificationKey2018` sign algorithm. + #### `verify` It allows you to verify wrapped/ issued document programmatically. Upon successful verification, it will return fragments which would collectively prove the validity of the document. @@ -84,6 +146,28 @@ After verification, use `isValid` method to answer some questions: - Is the issuance state of the document valid ? - Is the document issuer identity valid ? (see [identity proof](https://docs.tradetrust.io/docs/topics/verifying-documents/issuer-identity)) +#### `verifySignature` + +It checks that the signature of the document corresponds to the actual content in the document. In addition, it checks that the target hash (hash of the document content), is part of the set of documents wrapped in the batch using the proofs. + +Note that this method does not check against the blockchain or any registry if this document has been published. The merkle root of this document need to be checked against a publicly accessible document store (can be a smart contract on the blockchain). + +#### `isWrappedV2Document` + +type guard for wrapped v2 document + +#### `isSignedWrappedV2Document` + +type guard for signed v2 document + +#### `isWrappedV3Document` + +type guard for wrapped v3 document + +#### `isSignedWrappedV3Document` + +type guard for signed v3 document + ## Contributing We welcome contributions to the TradeTrust core library. Please feel free to submit a pull request or open an issue. diff --git a/package-lock.json b/package-lock.json index f6740a4..0458718 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.6", "license": "ISC", "dependencies": { - "@tradetrust-tt/tradetrust": "^6.9.0", + "@tradetrust-tt/tradetrust": "^6.9.5", "@tradetrust-tt/tt-verify": "^8.9.2" }, "devDependencies": { @@ -3029,9 +3029,9 @@ } }, "node_modules/@tradetrust-tt/tradetrust": { - "version": "6.9.4", - "resolved": "https://registry.npmjs.org/@tradetrust-tt/tradetrust/-/tradetrust-6.9.4.tgz", - "integrity": "sha512-sGCg7c4nrAsb5vWL7OipfhbK0oysiSnoypn5Omu9+8iCQjrEn0d4RlfaIXqjMNrBiTP7BhfueCIkJnjsU2fxig==", + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/@tradetrust-tt/tradetrust/-/tradetrust-6.9.5.tgz", + "integrity": "sha512-odghk/04NR9DUyTGQKAPOyLJTlKnOYj3zi/Jmp9mDDRHn3KCbqDwYyaCzeOZOchiot1iiuykym4jGlgeJctphg==", "hasInstallScript": true, "dependencies": { "@govtechsg/jsonld": "^0.1.1", diff --git a/package.json b/package.json index edf355d..9d910bf 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,14 @@ "utils/provider": [ "./dist/types/utils/provider/index.d.ts" ], + "utils/tradetrust": [ + "./dist/types/utils/tradetrust/index.d.ts" + ], "verify": [ "./dist/types/verify/index.d.ts" + ], + "tradetrust": [ + "./dist/types/tradetrust/index.d.ts" ] } }, @@ -73,9 +79,17 @@ "require": "./dist/cjs/utils/provider/index.js", "import": "./dist/esm/utils/provider/index.js" }, + "./utils/tradetrust": { + "require": "./dist/cjs/utils/tradetrust/index.js", + "import": "./dist/esm/utils/tradetrust/index.js" + }, "./verify": { "require": "./dist/cjs/verify/index.js", "import": "./dist/esm/verify/index.js" + }, + "./tradetrust": { + "require": "./dist/cjs/tradetrust/index.js", + "import": "./dist/esm/tradetrust/index.js" } }, "author": "tradetrust", @@ -109,7 +123,7 @@ "url": "https://github.com/TradeTrust/tradetrust-core.git" }, "dependencies": { - "@tradetrust-tt/tradetrust": "^6.9.0", + "@tradetrust-tt/tradetrust": "^6.9.5", "@tradetrust-tt/tt-verify": "^8.9.2" }, "publishConfig": { diff --git a/src/index.ts b/src/index.ts index 1b35935..5c42bb3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './verify' export * from './utils' +export * from './tradetrust' diff --git a/src/tradetrust/index.ts b/src/tradetrust/index.ts new file mode 100644 index 0000000..e7f95d1 --- /dev/null +++ b/src/tradetrust/index.ts @@ -0,0 +1,36 @@ +// Import everything except utils from @tradetrust-tt/tradetrust +import { + type OpenAttestationDocument, + type WrappedDocument, + type SignedWrappedDocument, + v2, + v3, + SchemaId, + SUPPORTED_SIGNING_ALGORITHM, + validateSchema, + obfuscateDocument, + verifySignature, + signDocument, + getData as getDataV2, + isSchemaValidationError, + wrapDocuments as wrapDocumentsV2, + __unsafe__use__it__at__your__own__risks__wrapDocuments as wrapDocumentsV3, +} from '@tradetrust-tt/tradetrust' + +// Re-export everything +export type { WrappedDocument, SignedWrappedDocument } +export { + validateSchema, + OpenAttestationDocument, + obfuscateDocument, + verifySignature, + signDocument, + getDataV2, + isSchemaValidationError, + wrapDocumentsV2, + wrapDocumentsV3, + v2, + v3, + SchemaId, + SUPPORTED_SIGNING_ALGORITHM, +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 20b11fa..d46855e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,8 @@ +import * as CONSTANTS from './constants/VerificationErrorMessages' export * from './fragement' export * from './analytics' -import * as CONSTANTS from './constants/VerificationErrorMessages' export * from './constants/supportedChains' export * from './provider/provider' +export * from './tradetrust' export { CONSTANTS } diff --git a/src/utils/tradetrust/index.ts b/src/utils/tradetrust/index.ts new file mode 100644 index 0000000..2949841 --- /dev/null +++ b/src/utils/tradetrust/index.ts @@ -0,0 +1,34 @@ +import { utils } from '@tradetrust-tt/tradetrust' +import type { DiagnoseError } from '@tradetrust-tt/tradetrust/dist/types/shared/utils' + +const { + isTransferableAsset, + getAssetId, + isWrappedV2Document, + isSignedWrappedV2Document, + isWrappedV3Document, + isSignedWrappedV3Document, + isRawV2Document, + isRawV3Document, + isObfuscated, + getDocumentData, + getIssuerAddress, + diagnose, +} = utils + +export { + isTransferableAsset, + getAssetId, + isWrappedV2Document, + isSignedWrappedV2Document, + isWrappedV3Document, + isSignedWrappedV3Document, + isRawV2Document, + isRawV3Document, + isObfuscated, + getDocumentData, + getIssuerAddress, + diagnose, +} + +export type { DiagnoseError }