@xyo-network/xl1-protocol-sdk 1.12.0 → 1.12.2
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/dist/neutral/config/Api.d.ts +3 -3
- package/dist/neutral/config/Api.d.ts.map +1 -1
- package/dist/neutral/config/App.d.ts +3 -3
- package/dist/neutral/config/App.d.ts.map +1 -1
- package/dist/neutral/config/Chain.d.ts +2 -2
- package/dist/neutral/config/Chain.d.ts.map +1 -1
- package/dist/neutral/config/Config.d.ts +19 -19
- package/dist/neutral/config/Config.d.ts.map +1 -1
- package/dist/neutral/config/Evm.d.ts +6 -6
- package/dist/neutral/config/Evm.d.ts.map +1 -1
- package/dist/neutral/config/Log.d.ts +2 -2
- package/dist/neutral/config/Log.d.ts.map +1 -1
- package/dist/neutral/config/Producer.d.ts +5 -5
- package/dist/neutral/config/Producer.d.ts.map +1 -1
- package/dist/neutral/config/Telemetry.d.ts +4 -4
- package/dist/neutral/config/Telemetry.d.ts.map +1 -1
- package/dist/neutral/config/UsageMeta.d.ts +4 -4
- package/dist/neutral/config/UsageMeta.d.ts.map +1 -1
- package/dist/neutral/config/storage/Storage.d.ts +3 -3
- package/dist/neutral/config/storage/Storage.d.ts.map +1 -1
- package/dist/neutral/config/storage/driver/Mongo.d.ts +2 -2
- package/dist/neutral/config/storage/driver/Mongo.d.ts.map +1 -1
- package/dist/neutral/index.mjs +296 -157
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/transaction/signTransaction.d.ts +8 -1
- package/dist/neutral/transaction/signTransaction.d.ts.map +1 -1
- package/dist/neutral/transaction/spec/signTransaction.spec.d.ts +2 -0
- package/dist/neutral/transaction/spec/signTransaction.spec.d.ts.map +1 -0
- package/dist/neutral/validation/schema/Mnemonic.d.ts +1 -1
- package/dist/neutral/validation/schema/Mnemonic.d.ts.map +1 -1
- package/package.json +24 -22
- package/src/config/Api.ts +2 -1
- package/src/config/App.ts +2 -1
- package/src/config/Chain.ts +2 -1
- package/src/config/Config.ts +1 -1
- package/src/config/Evm.ts +2 -1
- package/src/config/Log.ts +2 -1
- package/src/config/Producer.ts +2 -1
- package/src/config/Telemetry.ts +2 -1
- package/src/config/UsageMeta.ts +1 -1
- package/src/config/storage/Storage.ts +2 -1
- package/src/config/storage/driver/Mongo.ts +2 -1
- package/src/transaction/signTransaction.ts +14 -2
- package/src/transaction/spec/signTransaction.spec.ts +46 -0
- package/src/validation/schema/Mnemonic.ts +1 -1
|
@@ -2,17 +2,29 @@ import { toArrayBuffer } from '@xylabs/arraybuffer'
|
|
|
2
2
|
import { assertEx } from '@xylabs/assert'
|
|
3
3
|
import { hexFromArrayBuffer } from '@xylabs/hex'
|
|
4
4
|
import type { AccountInstance } from '@xyo-network/account-model'
|
|
5
|
+
import type { Unsigned } from '@xyo-network/boundwitness-model'
|
|
5
6
|
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
6
7
|
import type { TransactionBoundWitness } from '@xyo-network/xl1-protocol'
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Signs an unsigned transaction with the provided account.
|
|
11
|
+
* @param tx The transaction to sign
|
|
12
|
+
* @param account The account to sign the transaction with
|
|
13
|
+
* @returns The signed transaction
|
|
14
|
+
*/
|
|
15
|
+
export async function signTransaction(tx: Unsigned<TransactionBoundWitness>, account: AccountInstance) {
|
|
9
16
|
assertEx(tx.from === account.address, () => 'Signer address does not match transaction from address')
|
|
17
|
+
// Clone tx to prevent modifying original
|
|
10
18
|
const signedTx = structuredClone(tx)
|
|
19
|
+
// Update dynamic fields based on account
|
|
11
20
|
signedTx.addresses = [account.address]
|
|
12
21
|
signedTx.previous_hashes = [account.previousHash ?? null]
|
|
13
|
-
|
|
22
|
+
// Calculate tx hash and sign it
|
|
23
|
+
const hash = await PayloadBuilder.dataHash(signedTx)
|
|
14
24
|
const hashBytes = toArrayBuffer(hash)
|
|
15
25
|
const [signature] = await account.sign(hashBytes)
|
|
26
|
+
// Append the signatures to the transaction
|
|
16
27
|
signedTx.$signatures = [hexFromArrayBuffer(signature)]
|
|
28
|
+
// Return the signed transaction
|
|
17
29
|
return signedTx
|
|
18
30
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { Account } from '@xyo-network/account'
|
|
3
|
+
import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator'
|
|
4
|
+
import type { TransactionBoundWitness } from '@xyo-network/xl1-protocol'
|
|
5
|
+
import {
|
|
6
|
+
describe, expect, it,
|
|
7
|
+
} from 'vitest'
|
|
8
|
+
|
|
9
|
+
import { signTransaction } from '../signTransaction.ts'
|
|
10
|
+
|
|
11
|
+
describe('signTransaction', () => {
|
|
12
|
+
it('successfully signs transaction', async () => {
|
|
13
|
+
const bw = {
|
|
14
|
+
$signatures: [],
|
|
15
|
+
addresses: [],
|
|
16
|
+
chain: '0000000000000000000000000000000000000000',
|
|
17
|
+
exp: 1001,
|
|
18
|
+
fees: {
|
|
19
|
+
base: 'e8d4a51000',
|
|
20
|
+
gasLimit: '038d7ea4c68000',
|
|
21
|
+
gasPrice: '02540be400',
|
|
22
|
+
priority: '00',
|
|
23
|
+
},
|
|
24
|
+
nbf: 1,
|
|
25
|
+
payload_hashes: [
|
|
26
|
+
'34e5eadb2ccfc7005e224bfa6c3d10c32087d63238005efeaef9c67a85ed6fa2',
|
|
27
|
+
'eb28b0213854d3b5f255806c2261e568fabcef8afa8b7d7f504bee35d9bce917',
|
|
28
|
+
],
|
|
29
|
+
payload_schemas: [
|
|
30
|
+
'network.xyo.hash',
|
|
31
|
+
'network.xyo.id',
|
|
32
|
+
],
|
|
33
|
+
previous_hashes: [],
|
|
34
|
+
schema: 'network.xyo.boundwitness',
|
|
35
|
+
script: [
|
|
36
|
+
'elevate|34e5eadb2ccfc7005e224bfa6c3d10c32087d63238005efeaef9c67a85ed6fa2',
|
|
37
|
+
],
|
|
38
|
+
} as any as TransactionBoundWitness
|
|
39
|
+
const account = await Account.random()
|
|
40
|
+
bw.from = account.address
|
|
41
|
+
const signed = await signTransaction(bw, account)
|
|
42
|
+
expect(signed).toBeDefined()
|
|
43
|
+
const validationErrors = await new BoundWitnessValidator(signed).validate()
|
|
44
|
+
expect(validationErrors.length).toBe(0)
|
|
45
|
+
})
|
|
46
|
+
})
|