-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LW-10784: add utility functions for import and exporting tx files (#1286
) * 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
1 parent
6a29b3c
commit ed12a8a
Showing
6 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/core/src/shared-wallets/docs/schema/shared-wallet-transaction-schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/core/src/shared-wallets/docs/schema/shared-wallet-transaction-type-autogenerated.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters