@aioha/tx-digest 1.0.0

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.
package/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export const transactionDigest: (
2
+ transaction: any,
3
+ chainId?: Uint8Array
4
+ ) => Promise<{
5
+ digest: Uint8Array
6
+ txId: string
7
+ }>
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import { ByteBuffer } from './helpers/ByteBuffer.js'
2
+ import { Serializer } from './helpers/serializer.js'
3
+ import { hexToUint8Array, uint8ArrayToHex } from './helpers/uint8Array.js'
4
+ // import { sha256 } from '@noble/hashes/sha256'
5
+
6
+ export const sha256 = async (message) => {
7
+ const hashBuffer = await window.crypto.subtle.digest('SHA-256', message)
8
+ return new Uint8Array(hashBuffer)
9
+ }
10
+
11
+ const CHAIN_ID = hexToUint8Array('beeab0de00000000000000000000000000000000000000000000000000000000')
12
+
13
+ export const transactionDigest = async (transaction, chainId = CHAIN_ID) => {
14
+ const buffer = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
15
+ const temp = { ...transaction }
16
+ try {
17
+ Serializer.Transaction(buffer, temp)
18
+ } catch (cause) {
19
+ throw new Error('Unable to serialize transaction: ' + cause)
20
+ }
21
+ buffer.flip()
22
+ const transactionData = new Uint8Array(buffer.toBuffer())
23
+ const txId = uint8ArrayToHex(await sha256(transactionData)).slice(0, 40)
24
+ const digest = await sha256(new Uint8Array([...chainId, ...transactionData]))
25
+ return { digest, txId }
26
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@aioha/tx-digest",
3
+ "version": "1.0.0",
4
+ "description": "Hive transaction serializer and digest",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [],
12
+ "author": "techcoderx",
13
+ "license": "MIT",
14
+ "repository": {
15
+ "url": "https://github.com/aioha-hive/tx-digest"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/aioha-hive/tx-digest/issues"
19
+ },
20
+ "dependencies": {
21
+ "bs58": "^6.0.0"
22
+ }
23
+ }