Skip to content

Commit

Permalink
LW-10784: add utility functions for import and exporting tx files (#1286
Browse files Browse the repository at this point in the history
)

* feat: add utility functions for import and exporting tx files

* refactor: rename function

* refactor: rename files and change types

* refactor: validate against schema

* refactor: cast to txcbor
  • Loading branch information
shawnbusuttil authored Jul 15, 2024
1 parent 6a29b3c commit ed12a8a
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/cardano/src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export {
SortField,
EraSummary,
HandleResolution,
TxSubmissionError
TxSubmissionError,
TxCBOR
} from '@cardano-sdk/core';

export { testnetEraSummaries } from '@cardano-sdk/util-dev';
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@lace/cardano": "0.1.0",
"@lace/common": "0.1.0",
"@lace/translation": "0.1.0",
"ajv": "^8.16.0",
"antd": "^4.24.10",
"axios": "0.28.0",
"axios-cache-adapter": "2.7.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"description": "The transaction structure for shared wallet",
"properties": {
"version": {
"type": "string",
"description": "The version of the schema you are using",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"metadata": {
"type": "object",
"properties": {
"createdAt": {
"type": "string",
"format": "date-time",
"description": "The date and time the transaction was created"
},
"note": {
"type": "string",
"description": "A note about the transaction"
},
"createdBy": {
"type": "string",
"description": "The public key of the creator of the transaction"
},
"chainId": {
"type": "string",
"description": "The CIP34 formatted chainId the transaction should be submitted to",
"pattern": "^cip34:[01]-\\w+$"
}
},
"required": ["createdAt", "createdBy", "chainId"]
},
"transaction": {
"type": "object",
"properties": {
"cborHex": {
"type": "string",
"description": "This contains the CBOR for the transaction"
}
},
"required": ["cborHex"]
}
},
"required": ["version", "metadata", "transaction"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

/**
* The transaction structure for shared wallet
*/
export interface SharedWalletTransactionSchema {
metadata: {
[k: string]: unknown;
/**
* The CIP34 formatted chainId the transaction should be submitted to
*/
chainId: string;
/**
* The date and time the transaction was created
*/
createdAt: string;
/**
* The public key of the creator of the transaction
*/
createdBy: string;
/**
* A note about the transaction
*/
note?: string;
};
transaction: {
[k: string]: unknown;
/**
* This contains the CBOR for the transaction
*/
cborHex: string;
};
/**
* The version of the schema you are using
*/
version: string;
}
95 changes: 95 additions & 0 deletions packages/core/src/shared-wallets/transactionFileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Wallet } from '@lace/cardano';
import { Ajv } from 'ajv';
import schema from '../shared-wallets/docs/schema/shared-wallet-transaction-schema.json';
import { SharedWalletTransactionSchema } from './docs/schema/shared-wallet-transaction-type-autogenerated';

type MultisigTxData = {
metadata: {
chainId: `cip34:${number}-${number}`;
createdAt: Date;
createdBy: string;
note?: string;
};
transaction: {
cborHex: string;
};
version: string;
};

const ajv = new Ajv({ allErrors: true });
const validateWithSchema = ajv.compile(schema);

export const importMultiSigTransaction = async (file: File): Promise<Wallet.Cardano.Tx<Wallet.Cardano.TxBody>> => {
const reader = new FileReader();

return new Promise((resolve, reject) => {
reader.addEventListener('load', async (e) => {
try {
const data = JSON.parse(<string>e.target.result) as SharedWalletTransactionSchema;
const isValid = validateWithSchema(data);

if (!isValid) {
console.error(validateWithSchema.errors);
reject(validateWithSchema.errors);
}

const {
transaction: { cborHex },
} = data;

const coSignedTx = Wallet.TxCBOR.deserialize(Wallet.TxCBOR(cborHex));

resolve(coSignedTx);
} catch (error) {
reject(new Error(`Error parsing JSON: ${error.message}`));
}
});

reader.addEventListener('error', () => {
reject(new Error('Error reading file'));
});

reader.readAsText(file);
});
};

export const exportMultisigTransaction = async (
signedTx: Wallet.KeyManagement.WitnessedTx,
publicKey: Wallet.Crypto.Bip32PublicKeyHex,
chainId: Wallet.Cardano.ChainId,
note?: string,
) => {
const multisigTxData: MultisigTxData = {
metadata: {
chainId: `cip34:${chainId.networkId}-${chainId.networkMagic}`,
createdAt: new Date(),
createdBy: publicKey,
note,
},
transaction: {
cborHex: signedTx.cbor,
},
version: '1.0.0',
};

const isValid = validateWithSchema(multisigTxData);

if (!isValid) {
console.error(validateWithSchema.errors);
return;
}

// eslint-disable-next-line no-magic-numbers
const jsonStr = JSON.stringify(multisigTxData, null, 2);
const blob = new Blob([jsonStr], { type: 'application/json' });

const link = document.createElement('a');

link.download = `Wallet_sign_transaction+${Date.now()}`;
link.href = window.URL.createObjectURL(blob);

document.body.append(link);

link.click();
link.remove();
};
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11437,6 +11437,7 @@ __metadata:
"@types/babel__preset-env": ^7
"@types/debounce-promise": ^3.1.6
"@types/uuid": ^8
ajv: ^8.16.0
antd: ^4.24.10
axios: 0.28.0
axios-cache-adapter: 2.7.3
Expand Down Expand Up @@ -23066,6 +23067,18 @@ __metadata:
languageName: node
linkType: hard

"ajv@npm:^8.16.0":
version: 8.16.0
resolution: "ajv@npm:8.16.0"
dependencies:
fast-deep-equal: ^3.1.3
json-schema-traverse: ^1.0.0
require-from-string: ^2.0.2
uri-js: ^4.4.1
checksum: bdf3d4c9f1d11e220850051ef4cd89346e951cfb933d6d41be36d45053c1092af1523ee6c62525cce567355caf0a4f4c19a08a93851649c1fa32b4a39b7c4858
languageName: node
linkType: hard

"allure-commandline@npm:2.29.0":
version: 2.29.0
resolution: "allure-commandline@npm:2.29.0"
Expand Down Expand Up @@ -52898,7 +52911,7 @@ __metadata:
languageName: node
linkType: hard

"uri-js@npm:^4.2.2":
"uri-js@npm:^4.2.2, uri-js@npm:^4.4.1":
version: 4.4.1
resolution: "uri-js@npm:4.4.1"
dependencies:
Expand Down

0 comments on commit ed12a8a

Please sign in to comment.