@bsv/sdk 1.0.1 → 1.0.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.
Files changed (112) hide show
  1. package/dist/cjs/package.json +8 -4
  2. package/dist/cjs/src/transaction/broadcasters/ARC.js +13 -2
  3. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -1
  4. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  5. package/dist/esm/src/transaction/broadcasters/ARC.js +13 -2
  6. package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -1
  7. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  8. package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -1
  9. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  10. package/package.json +2 -1
  11. package/src/compat/BSM.ts +51 -0
  12. package/src/compat/ECIES.ts +557 -0
  13. package/src/compat/HD.ts +348 -0
  14. package/src/compat/Mnemonic.ts +295 -0
  15. package/src/compat/__tests/BSM.test.ts +38 -0
  16. package/src/compat/__tests/ECIES.test.ts +90 -0
  17. package/src/compat/__tests/HD.test.ts +405 -0
  18. package/src/compat/__tests/Mnemonic.test.ts +177 -0
  19. package/src/compat/__tests/Mnemonic.vectors.ts +172 -0
  20. package/src/compat/bip-39-wordlist-en.ts +2053 -0
  21. package/src/compat/index.ts +4 -0
  22. package/src/messages/EncryptedMessage.ts +70 -0
  23. package/src/messages/SignedMessage.ts +87 -0
  24. package/src/messages/__tests/EncryptedMessage.test.ts +36 -0
  25. package/src/messages/__tests/SignedMessage.test.ts +53 -0
  26. package/src/messages/index.ts +2 -0
  27. package/src/primitives/AESGCM.ts +479 -0
  28. package/src/primitives/BasePoint.ts +21 -0
  29. package/src/primitives/BigNumber.ts +4619 -0
  30. package/src/primitives/Curve.ts +1163 -0
  31. package/src/primitives/DRBG.ts +102 -0
  32. package/src/primitives/ECDSA.ts +164 -0
  33. package/src/primitives/Hash.ts +1420 -0
  34. package/src/primitives/JacobianPoint.ts +410 -0
  35. package/src/primitives/K256.ts +116 -0
  36. package/src/primitives/Mersenne.ts +123 -0
  37. package/src/primitives/MontgomoryMethod.ts +160 -0
  38. package/src/primitives/Point.ts +852 -0
  39. package/src/primitives/PrivateKey.ts +195 -0
  40. package/src/primitives/PublicKey.ts +154 -0
  41. package/src/primitives/Random.ts +55 -0
  42. package/src/primitives/ReductionContext.ts +528 -0
  43. package/src/primitives/Signature.ts +235 -0
  44. package/src/primitives/SymmetricKey.ts +75 -0
  45. package/src/primitives/TransactionSignature.ts +189 -0
  46. package/src/primitives/__tests/AESGCM.test.ts +338 -0
  47. package/src/primitives/__tests/BRC42.private.vectors.ts +33 -0
  48. package/src/primitives/__tests/BRC42.public.vectors.ts +33 -0
  49. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +572 -0
  50. package/src/primitives/__tests/BigNumber.binary.test.ts +203 -0
  51. package/src/primitives/__tests/BigNumber.constructor.test.ts +176 -0
  52. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +18 -0
  53. package/src/primitives/__tests/BigNumber.fixtures.ts +264 -0
  54. package/src/primitives/__tests/BigNumber.serializers.test.ts +157 -0
  55. package/src/primitives/__tests/BigNumber.utils.test.ts +347 -0
  56. package/src/primitives/__tests/Curve.unit.test.ts +192 -0
  57. package/src/primitives/__tests/DRBG.test.ts +18 -0
  58. package/src/primitives/__tests/DRBG.vectors.ts +167 -0
  59. package/src/primitives/__tests/ECDH.test.ts +31 -0
  60. package/src/primitives/__tests/ECDSA.test.ts +58 -0
  61. package/src/primitives/__tests/HMAC.test.ts +59 -0
  62. package/src/primitives/__tests/Hash.test.ts +121 -0
  63. package/src/primitives/__tests/PBKDF2.vectors.ts +119 -0
  64. package/src/primitives/__tests/PrivateKey.test.ts +17 -0
  65. package/src/primitives/__tests/PublicKey.test.ts +66 -0
  66. package/src/primitives/__tests/Random.test.ts +14 -0
  67. package/src/primitives/__tests/Reader.test.ts +296 -0
  68. package/src/primitives/__tests/ReductionContext.test.ts +279 -0
  69. package/src/primitives/__tests/SymmetricKey.test.ts +58 -0
  70. package/src/primitives/__tests/SymmetricKey.vectors.ts +40 -0
  71. package/src/primitives/__tests/Writer.test.ts +198 -0
  72. package/src/primitives/__tests/sighash.vectors.ts +3503 -0
  73. package/src/primitives/__tests/utils.test.ts +108 -0
  74. package/src/primitives/index.ts +8 -0
  75. package/src/primitives/utils.ts +665 -0
  76. package/src/script/LockingScript.ts +30 -0
  77. package/src/script/OP.ts +219 -0
  78. package/src/script/Script.ts +426 -0
  79. package/src/script/ScriptChunk.ts +7 -0
  80. package/src/script/ScriptTemplate.ts +36 -0
  81. package/src/script/Spend.ts +1379 -0
  82. package/src/script/UnlockingScript.ts +30 -0
  83. package/src/script/__tests/Script.test.ts +369 -0
  84. package/src/script/__tests/Spend.test.ts +248 -0
  85. package/src/script/__tests/script.invalid.vectors.ts +925 -0
  86. package/src/script/__tests/script.valid.vectors.ts +1120 -0
  87. package/src/script/__tests/scriptFromVector.ts +42 -0
  88. package/src/script/__tests/spend.valid.vectors.ts +2288 -0
  89. package/src/script/index.ts +7 -0
  90. package/src/script/templates/P2PKH.ts +109 -0
  91. package/src/script/templates/RPuzzle.ts +140 -0
  92. package/src/script/templates/index.ts +2 -0
  93. package/src/transaction/Broadcaster.ts +42 -0
  94. package/src/transaction/ChainTracker.ts +22 -0
  95. package/src/transaction/FeeModel.ts +13 -0
  96. package/src/transaction/MerklePath.ts +259 -0
  97. package/src/transaction/Transaction.ts +602 -0
  98. package/src/transaction/TransactionInput.ts +63 -0
  99. package/src/transaction/TransactionOutput.ts +37 -0
  100. package/src/transaction/__tests/MerklePath.test.ts +181 -0
  101. package/src/transaction/__tests/Transaction.test.ts +413 -0
  102. package/src/transaction/__tests/bigtx.vectors.ts +4 -0
  103. package/src/transaction/__tests/bump.invalid.vectors.ts +8 -0
  104. package/src/transaction/__tests/bump.valid.vectors.ts +4 -0
  105. package/src/transaction/__tests/tx.invalid.vectors.ts +281 -0
  106. package/src/transaction/__tests/tx.valid.vectors.ts +364 -0
  107. package/src/transaction/broadcasters/ARC.ts +115 -0
  108. package/src/transaction/broadcasters/__tests/ARC.test.ts +115 -0
  109. package/src/transaction/broadcasters/index.ts +1 -0
  110. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +71 -0
  111. package/src/transaction/fee-models/index.ts +1 -0
  112. package/src/transaction/index.ts +6 -0
@@ -0,0 +1,115 @@
1
+ import ARC from '../../../../dist/cjs/src/transaction/broadcasters/ARC.js'
2
+ import Transaction from '../../../../dist/cjs/src/transaction/Transaction.js'
3
+
4
+ // Mock Transaction
5
+ jest.mock('../../Transaction', () => {
6
+ return {
7
+ default: jest.fn().mockImplementation(() => {
8
+ return {
9
+ toHex: () => 'mocked_transaction_hex'
10
+ }
11
+ })
12
+ }
13
+ })
14
+
15
+ describe('ARC Broadcaster', () => {
16
+ const URL = 'https://example.com'
17
+ const apiKey = 'test_api_key'
18
+ let broadcaster: ARC
19
+ let transaction: Transaction
20
+
21
+ beforeEach(() => {
22
+ broadcaster = new ARC(URL, apiKey)
23
+ transaction = new Transaction()
24
+ })
25
+
26
+ it('should broadcast successfully using window.fetch', async () => {
27
+ // Mocking window.fetch
28
+ const mockFetch = jest.fn().mockResolvedValue({
29
+ ok: true,
30
+ json: async () => await Promise.resolve({
31
+ txid: 'mocked_txid',
32
+ txStatus: 'success',
33
+ extraInfo: 'received'
34
+ })
35
+ })
36
+ global.window = { fetch: mockFetch } as any
37
+
38
+ const response = await broadcaster.broadcast(transaction)
39
+
40
+ expect(mockFetch).toHaveBeenCalled()
41
+ expect(response).toEqual({
42
+ status: 'success',
43
+ txid: 'mocked_txid',
44
+ message: 'success received'
45
+ })
46
+ })
47
+
48
+ it('should broadcast successfully using Node.js https', async () => {
49
+ // Mocking Node.js https module
50
+ jest.mock('https', () => ({
51
+ request: (url, options, callback) => {
52
+ // eslint-disable-next-line
53
+ callback({
54
+ statusCode: 200,
55
+ on: (event, handler) => {
56
+ if (event === 'data') handler(JSON.stringify({
57
+ txid: 'mocked_txid',
58
+ txStatus: 'success',
59
+ extraInfo: 'received'
60
+ }))
61
+ if (event === 'end') handler()
62
+ }
63
+ })
64
+ return {
65
+ on: jest.fn(),
66
+ write: jest.fn(),
67
+ end: jest.fn()
68
+ }
69
+ }
70
+ }))
71
+
72
+ delete global.window
73
+ const response = await broadcaster.broadcast(transaction)
74
+
75
+ expect(response).toEqual({
76
+ status: 'success',
77
+ txid: 'mocked_txid',
78
+ message: 'success received'
79
+ })
80
+ })
81
+
82
+ it('should handle network errors', async () => {
83
+ const mockFetch = jest.fn().mockRejectedValue(new Error('Network error'))
84
+ global.window = { fetch: mockFetch } as any
85
+
86
+ const response = await broadcaster.broadcast(transaction)
87
+
88
+ expect(mockFetch).toHaveBeenCalled()
89
+ expect(response).toEqual({
90
+ status: 'error',
91
+ code: '500',
92
+ description: 'Network error'
93
+ })
94
+ })
95
+
96
+ it('should handle non-200 responses', async () => {
97
+ const mockFetch = jest.fn().mockResolvedValue({
98
+ ok: false,
99
+ json: async () => await Promise.resolve({
100
+ status: '400',
101
+ detail: 'Bad request'
102
+ })
103
+ })
104
+ global.window = { fetch: mockFetch } as any
105
+
106
+ const response = await broadcaster.broadcast(transaction)
107
+
108
+ expect(mockFetch).toHaveBeenCalled()
109
+ expect(response).toEqual({
110
+ status: 'error',
111
+ code: '400',
112
+ description: 'Bad request'
113
+ })
114
+ })
115
+ })
@@ -0,0 +1 @@
1
+ export { default as ARC } from './ARC.js'
@@ -0,0 +1,71 @@
1
+ import FeeModel from '../FeeModel.js'
2
+ import Transaction from '../Transaction.js'
3
+ import BigNumber from '../../primitives/BigNumber.js'
4
+
5
+ /**
6
+ * Represents the "satoshis per kilobyte" transaction fee model.
7
+ */
8
+ export default class SatoshisPerKilobyte implements FeeModel {
9
+ /**
10
+ * @property
11
+ * Denotes the number of satoshis paid per kilobyte of transaction size.
12
+ */
13
+ value: number
14
+
15
+ /**
16
+ * Constructs an instance of the sat/kb fee model.
17
+ *
18
+ * @param {number} value - The number of satoshis per kilobyte to charge as a fee.
19
+ */
20
+ constructor(value: number) {
21
+ this.value = value
22
+ }
23
+
24
+ /**
25
+ * Computes the fee for a given transaction.
26
+ *
27
+ * @param tx The transaction for which a fee is to be computed.
28
+ * @returns The fee in satoshis for the transaction, as a BigNumber.
29
+ */
30
+ async computeFee(tx: Transaction): Promise<number> {
31
+ const getVarIntSize = (i: number): number => {
32
+ if (i > 2 ** 32) {
33
+ return 9
34
+ } else if (i > 2 ** 16) {
35
+ return 5
36
+ } else if (i > 253) {
37
+ return 3
38
+ } else {
39
+ return 1
40
+ }
41
+ }
42
+ // Compute the (potentially estimated) size of the transaction
43
+ let size = 4 // version
44
+ size += getVarIntSize(tx.inputs.length) // number of inputs
45
+ for (let i = 0; i < tx.inputs.length; i++) {
46
+ const input = tx.inputs[i]
47
+ size += 40 // txid, output index, sequence number
48
+ let scriptLength: number
49
+ if (typeof input.unlockingScript === 'object') {
50
+ scriptLength = input.unlockingScript.toBinary().length
51
+ } else if (typeof input.unlockingScriptTemplate === 'object') {
52
+ scriptLength = await input.unlockingScriptTemplate.estimateLength(tx, i)
53
+ } else {
54
+ throw new Error('All inputs must have an unlocking script or an unlocking script template for sat/kb fee computation.')
55
+ }
56
+ size += getVarIntSize(scriptLength) // unlocking script length
57
+ size += scriptLength // unlocking script
58
+ }
59
+ size += getVarIntSize(tx.outputs.length) // number of outputs
60
+ for (const out of tx.outputs) {
61
+ size += 8 // satoshis
62
+ const length = out.lockingScript.toBinary().length
63
+ size += getVarIntSize(length) // script length
64
+ size += length // script
65
+ }
66
+ size += 4 // lock time
67
+ // We'll use Math.ceil to ensure the miners get the extra satoshi.
68
+ const fee = Math.ceil((size / 1000) * this.value)
69
+ return fee
70
+ }
71
+ }
@@ -0,0 +1 @@
1
+ export { default as SatoshisPerKilobyte } from './SatoshisPerKilobyte.js'
@@ -0,0 +1,6 @@
1
+ export { default as Transaction } from './Transaction.js'
2
+ export { default as MerklePath } from './MerklePath.js'
3
+ export type { default as TransactionInput } from './TransactionInput.js'
4
+ export type { default as TransactionOutput } from './TransactionOutput.js'
5
+ export type * as Broadcaster from './Broadcaster.js'
6
+ export type { default as ChainTracker } from './ChainTracker.js'