@bsv/sdk 1.0.0 → 1.0.1

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 (123) hide show
  1. package/README.md +25 -3
  2. package/package.json +9 -5
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  4. package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
  5. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
  6. package/CHANGELOG.md +0 -72
  7. package/CONTRIBUTING.md +0 -85
  8. package/ROADMAP.md +0 -3
  9. package/docs/getting-started/COMMONJS.md +0 -94
  10. package/docs/getting-started/REACT-TS.md +0 -131
  11. package/docs/getting-started/TS-NODE.md +0 -106
  12. package/docs/getting-started/VUE.md +0 -103
  13. package/jest.config.js +0 -6
  14. package/mod.ts +0 -8
  15. package/src/compat/BSM.ts +0 -51
  16. package/src/compat/ECIES.ts +0 -557
  17. package/src/compat/HD.ts +0 -348
  18. package/src/compat/Mnemonic.ts +0 -295
  19. package/src/compat/__tests/BSM.test.ts +0 -38
  20. package/src/compat/__tests/ECIES.test.ts +0 -90
  21. package/src/compat/__tests/HD.test.ts +0 -405
  22. package/src/compat/__tests/Mnemonic.test.ts +0 -177
  23. package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
  24. package/src/compat/bip-39-wordlist-en.ts +0 -2053
  25. package/src/compat/index.ts +0 -4
  26. package/src/messages/EncryptedMessage.ts +0 -70
  27. package/src/messages/SignedMessage.ts +0 -87
  28. package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
  29. package/src/messages/__tests/SignedMessage.test.ts +0 -53
  30. package/src/messages/index.ts +0 -2
  31. package/src/primitives/AESGCM.ts +0 -479
  32. package/src/primitives/BasePoint.ts +0 -21
  33. package/src/primitives/BigNumber.ts +0 -4619
  34. package/src/primitives/Curve.ts +0 -1163
  35. package/src/primitives/DRBG.ts +0 -102
  36. package/src/primitives/ECDSA.ts +0 -164
  37. package/src/primitives/Hash.ts +0 -1420
  38. package/src/primitives/JacobianPoint.ts +0 -410
  39. package/src/primitives/K256.ts +0 -116
  40. package/src/primitives/Mersenne.ts +0 -123
  41. package/src/primitives/MontgomoryMethod.ts +0 -160
  42. package/src/primitives/Point.ts +0 -852
  43. package/src/primitives/PrivateKey.ts +0 -195
  44. package/src/primitives/PublicKey.ts +0 -154
  45. package/src/primitives/Random.ts +0 -55
  46. package/src/primitives/ReductionContext.ts +0 -528
  47. package/src/primitives/Signature.ts +0 -235
  48. package/src/primitives/SymmetricKey.ts +0 -75
  49. package/src/primitives/TransactionSignature.ts +0 -189
  50. package/src/primitives/__tests/AESGCM.test.ts +0 -338
  51. package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
  52. package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
  53. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
  54. package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
  55. package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
  56. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
  57. package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
  58. package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
  59. package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
  60. package/src/primitives/__tests/Curve.unit.test.ts +0 -192
  61. package/src/primitives/__tests/DRBG.test.ts +0 -18
  62. package/src/primitives/__tests/DRBG.vectors.ts +0 -167
  63. package/src/primitives/__tests/ECDH.test.ts +0 -31
  64. package/src/primitives/__tests/ECDSA.test.ts +0 -58
  65. package/src/primitives/__tests/HMAC.test.ts +0 -59
  66. package/src/primitives/__tests/Hash.test.ts +0 -121
  67. package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
  68. package/src/primitives/__tests/PrivateKey.test.ts +0 -17
  69. package/src/primitives/__tests/PublicKey.test.ts +0 -66
  70. package/src/primitives/__tests/Random.test.ts +0 -14
  71. package/src/primitives/__tests/Reader.test.ts +0 -296
  72. package/src/primitives/__tests/ReductionContext.test.ts +0 -279
  73. package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
  74. package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
  75. package/src/primitives/__tests/Writer.test.ts +0 -198
  76. package/src/primitives/__tests/sighash.vectors.ts +0 -3503
  77. package/src/primitives/__tests/utils.test.ts +0 -108
  78. package/src/primitives/index.ts +0 -8
  79. package/src/primitives/utils.ts +0 -665
  80. package/src/script/LockingScript.ts +0 -30
  81. package/src/script/OP.ts +0 -219
  82. package/src/script/Script.ts +0 -426
  83. package/src/script/ScriptChunk.ts +0 -7
  84. package/src/script/ScriptTemplate.ts +0 -36
  85. package/src/script/Spend.ts +0 -1379
  86. package/src/script/UnlockingScript.ts +0 -30
  87. package/src/script/__tests/Script.test.ts +0 -369
  88. package/src/script/__tests/Spend.test.ts +0 -248
  89. package/src/script/__tests/script.invalid.vectors.ts +0 -925
  90. package/src/script/__tests/script.valid.vectors.ts +0 -1120
  91. package/src/script/__tests/scriptFromVector.ts +0 -42
  92. package/src/script/__tests/spend.valid.vectors.ts +0 -2288
  93. package/src/script/index.ts +0 -7
  94. package/src/script/templates/P2PKH.ts +0 -109
  95. package/src/script/templates/RPuzzle.ts +0 -140
  96. package/src/script/templates/index.ts +0 -2
  97. package/src/transaction/Broadcaster.ts +0 -42
  98. package/src/transaction/ChainTracker.ts +0 -22
  99. package/src/transaction/FeeModel.ts +0 -13
  100. package/src/transaction/MerklePath.ts +0 -259
  101. package/src/transaction/Transaction.ts +0 -602
  102. package/src/transaction/TransactionInput.ts +0 -63
  103. package/src/transaction/TransactionOutput.ts +0 -37
  104. package/src/transaction/__tests/MerklePath.test.ts +0 -181
  105. package/src/transaction/__tests/Transaction.test.ts +0 -413
  106. package/src/transaction/__tests/bigtx.vectors.ts +0 -4
  107. package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
  108. package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
  109. package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
  110. package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
  111. package/src/transaction/broadcasters/ARC.ts +0 -106
  112. package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
  113. package/src/transaction/broadcasters/index.ts +0 -1
  114. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
  115. package/src/transaction/fee-models/index.ts +0 -1
  116. package/src/transaction/index.ts +0 -6
  117. package/ts2md.json +0 -5
  118. package/tsconfig.base.json +0 -26
  119. package/tsconfig.cjs.json +0 -11
  120. package/tsconfig.eslint.json +0 -12
  121. package/tsconfig.esm.json +0 -9
  122. package/tsconfig.json +0 -17
  123. package/tsconfig.types.json +0 -11
@@ -1,235 +0,0 @@
1
- import BigNumber from './BigNumber.js'
2
- import PublicKey from './PublicKey.js'
3
- import { verify } from './ECDSA.js'
4
- import { sha256 } from './Hash.js'
5
- import { toArray, toHex, toBase64 } from './utils.js'
6
-
7
- /**
8
- * Represents a digital signature.
9
- *
10
- * A digital signature is a mathematical scheme for verifying the authenticity of
11
- * digital messages or documents. In many scenarios, it is equivalent to a handwritten signature or stamped seal.
12
- * The signature pair (R, S) corresponds to the raw ECDSA ([Elliptic Curve Digital Signature Algorithm](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)) signature.
13
- * Signatures are often serialized into a format known as '[DER encoding](https://en.wikipedia.org/wiki/X.690#DER_encoding)' for transmission.
14
- *
15
- * @class Signature
16
- */
17
- export default class Signature {
18
- /**
19
- * @property Represents the "r" component of the digital signature
20
- */
21
- r: BigNumber
22
-
23
- /**
24
- * @property Represents the "s" component of the digital signature
25
- */
26
- s: BigNumber
27
-
28
- /**
29
- * Takes an array of numbers or a string and returns a new Signature instance.
30
- * This method will throw an error if the DER encoding is invalid.
31
- * If a string is provided, it is assumed to represent a hexadecimal sequence.
32
- *
33
- * @static
34
- * @method fromDER
35
- * @param data - The sequence to decode from DER encoding.
36
- * @param enc - The encoding of the data string.
37
- * @returns The decoded data in the form of Signature instance.
38
- *
39
- * @example
40
- * const signature = Signature.fromDER('30440220018c1f5502f8...', 'hex');
41
- */
42
- static fromDER(data: number[] | string, enc?: 'hex' | 'base64'): Signature {
43
- const getLength = (buf, p): number => {
44
- const initial = buf[p.place++]
45
- if ((initial & 0x80) === 0) {
46
- return initial
47
- } else {
48
- throw new Error('Invalid DER entity length')
49
- }
50
- }
51
-
52
- class Position {
53
- place: number
54
- constructor() {
55
- this.place = 0
56
- }
57
- }
58
- data = toArray(data, enc)
59
-
60
- // Support compact signatures
61
- if (data.length === 65) {
62
- return new Signature(
63
- new BigNumber(data.slice(1, 33)),
64
- new BigNumber(data.slice(33, 65))
65
- )
66
- }
67
-
68
- const p = new Position()
69
- if (data[p.place++] !== 0x30) {
70
- throw new Error('Signature DER must start with 0x30')
71
- }
72
- const len = getLength(data, p)
73
- if ((len + p.place) !== data.length) {
74
- throw new Error('Signature DER invalid')
75
- }
76
- if (data[p.place++] !== 0x02) {
77
- throw new Error('Signature DER invalid')
78
- }
79
- const rlen = getLength(data, p)
80
- let r = data.slice(p.place, rlen + p.place)
81
- p.place += rlen
82
- if (data[p.place++] !== 0x02) {
83
- throw new Error('Signature DER invalid')
84
- }
85
- const slen = getLength(data, p)
86
- if (data.length !== slen + p.place) {
87
- throw new Error('Invalid R-length in signature DER')
88
- }
89
- let s = data.slice(p.place, slen + p.place)
90
- if (r[0] === 0) {
91
- if ((r[1] & 0x80) !== 0) {
92
- r = r.slice(1)
93
- } else {
94
- throw new Error('Invalid R-value in signature DER')
95
- }
96
- }
97
- if (s[0] === 0) {
98
- if ((s[1] & 0x80) !== 0) {
99
- s = s.slice(1)
100
- } else {
101
- throw new Error('Invalid S-value in signature DER')
102
- }
103
- }
104
-
105
- return new Signature(
106
- new BigNumber(r),
107
- new BigNumber(s)
108
- )
109
- }
110
-
111
- /**
112
- * Creates an instance of the Signature class.
113
- *
114
- * @constructor
115
- * @param r - The R component of the signature.
116
- * @param s - The S component of the signature.
117
- *
118
- * @example
119
- * const r = new BigNumber('208755674028...');
120
- * const s = new BigNumber('564745627577...');
121
- * const signature = new Signature(r, s);
122
- */
123
- constructor(r: BigNumber, s: BigNumber) {
124
- this.r = r
125
- this.s = s
126
- }
127
-
128
- /**
129
- * Verifies a digital signature.
130
- *
131
- * This method will return true if the signature, key, and message hash match.
132
- * If the data or key do not match the signature, the function returns false.
133
- *
134
- * @method verify
135
- * @param msg - The message to verify.
136
- * @param key - The public key used to sign the original message.
137
- * @param enc - The encoding of the msg string.
138
- * @returns A boolean representing whether the signature is valid.
139
- *
140
- * @example
141
- * const msg = 'The quick brown fox jumps over the lazy dog';
142
- * const publicKey = PublicKey.fromString('04188ca1050...');
143
- * const isVerified = signature.verify(msg, publicKey);
144
- */
145
- verify(msg: number[] | string, key: PublicKey, enc?: 'hex'): boolean {
146
- const msgHash = new BigNumber(sha256(msg, enc), 16)
147
- return verify(msgHash, this, key)
148
- }
149
-
150
- /**
151
- * Converts an instance of Signature into DER encoding.
152
- * An alias for the toDER method.
153
- *
154
- * If the encoding parameter is set to 'hex', the function will return a hex string.
155
- * If 'base64', it will return a base64 string.
156
- * Otherwise, it will return an array of numbers.
157
- *
158
- * @method toDER
159
- * @param enc - The encoding to use for the output.
160
- * @returns The current instance in DER encoding.
161
- *
162
- * @example
163
- * const der = signature.toString('base64');
164
- */
165
- toString(enc?: 'hex' | 'base64') {
166
- return this.toDER(enc)
167
- }
168
-
169
- /**
170
- * Converts an instance of Signature into DER encoding.
171
- *
172
- * If the encoding parameter is set to 'hex', the function will return a hex string.
173
- * If 'base64', it will return a base64 string.
174
- * Otherwise, it will return an array of numbers.
175
- *
176
- * @method toDER
177
- * @param enc - The encoding to use for the output.
178
- * @returns The current instance in DER encoding.
179
- *
180
- * @example
181
- * const der = signature.toDER('hex');
182
- */
183
- toDER(enc?: 'hex' | 'base64'): number[] | string {
184
- const constructLength = (arr, len): void => {
185
- if (len < 0x80) {
186
- arr.push(len)
187
- } else {
188
- throw new Error('len must be < 0x80')
189
- }
190
- }
191
-
192
- const rmPadding = (buf: number[]): number[] => {
193
- let i = 0
194
- const len = buf.length - 1
195
- while ((buf[i] === 0) && ((buf[i + 1] & 0x80) === 0) && i < len) {
196
- i++
197
- }
198
- if (i === 0) {
199
- return buf
200
- }
201
- return buf.slice(i)
202
- }
203
-
204
- let r = this.r.toArray()
205
- let s = this.s.toArray()
206
-
207
- // Pad values
208
- if ((r[0] & 0x80) !== 0) { r = [0].concat(r) }
209
- // Pad values
210
- if ((s[0] & 0x80) !== 0) { s = [0].concat(s) }
211
-
212
- r = rmPadding(r)
213
- s = rmPadding(s)
214
-
215
- while ((s[0] === 0) && (s[1] & 0x80) === 0) {
216
- s = s.slice(1)
217
- }
218
- let arr = [0x02]
219
- constructLength(arr, r.length)
220
- arr = arr.concat(r)
221
- arr.push(0x02)
222
- constructLength(arr, s.length)
223
- const backHalf = arr.concat(s)
224
- let res = [0x30]
225
- constructLength(res, backHalf.length)
226
- res = res.concat(backHalf)
227
- if (enc === 'hex') {
228
- return toHex(res)
229
- } else if (enc === 'base64') {
230
- return toBase64(res)
231
- } else {
232
- return res
233
- }
234
- }
235
- }
@@ -1,75 +0,0 @@
1
- import BigNumber from './BigNumber.js'
2
- import { AESGCM, AESGCMDecrypt } from './AESGCM.js'
3
- import Random from './Random.js'
4
- import { toArray, encode } from './utils.js'
5
-
6
- /**
7
- * `SymmetricKey` is a class that extends the `BigNumber` class and implements symmetric encryption and decryption methods.
8
- * Symmetric-Key encryption is a form of encryption where the same key is used to encrypt and decrypt the message.
9
- * It leverages the Advanced Encryption Standard Galois/Counter Mode (AES-GCM) for encryption and decryption of messages.
10
- *
11
- * @class SymmetricKey
12
- * @extends {BigNumber}
13
- */
14
- export default class SymmetricKey extends BigNumber {
15
- /**
16
- * Encrypts a given message using AES-GCM encryption.
17
- * The generated Initialization Vector (IV) is attached to the encrypted message for decryption purposes.
18
- * The OpenSSL format of |IV|encryptedContent|authTag| is used.
19
- *
20
- * @method encrypt
21
- * @param msg - The message to be encrypted. It can be a string or an array of numbers.
22
- * @param enc - optional. The encoding of the message. If hex, the string is assumed to be hex, UTF-8 otherwise.
23
- * @returns Returns the encrypted message as a string or an array of numbers, depending on `enc` argument.
24
- *
25
- * @example
26
- * const key = new SymmetricKey(1234);
27
- * const encryptedMessage = key.encrypt('plainText', 'utf8');
28
- */
29
- encrypt (msg: number[] | string, enc?: 'hex'): string | number[] {
30
- const iv = Random(32)
31
- msg = toArray(msg, enc)
32
- const { result, authenticationTag } = AESGCM(
33
- msg,
34
- [],
35
- iv,
36
- this.toArray()
37
- )
38
- return encode([...iv, ...result, ...authenticationTag], enc)
39
- }
40
-
41
- /**
42
- * Decrypts a given AES-GCM encrypted message using the same key that was used for encryption.
43
- * The method extracts the IV and the authentication tag from the encrypted message, then attempts to decrypt it.
44
- * If the decryption fails (e.g., due to message tampering), an error is thrown.
45
- *
46
- * @method decrypt
47
- * @param msg - The encrypted message to be decrypted. It can be a string or an array of numbers.
48
- * @param enc - optional. The encoding of the message (if no encoding is provided, uses utf8 for strings, unless specified as hex).
49
- * @returns Returns the decrypted message as a string or an array of numbers, depending on `enc` argument. If absent, an array of numbers is returned.
50
- *
51
- * @example
52
- * const key = new SymmetricKey(1234);
53
- * const decryptedMessage = key.decrypt(encryptedMessage, 'utf8');
54
- *
55
- * @throws {Error} Will throw an error if the decryption fails, likely due to message tampering or incorrect decryption key.
56
- */
57
- decrypt (msg: number[] | string, enc?: 'hex' | 'utf8'): string | number[] {
58
- msg = toArray(msg, enc) as number[]
59
- const iv = msg.slice(0, 32)
60
- const ciphertextWithTag = msg.slice(32)
61
- const messageTag = ciphertextWithTag.slice(-16)
62
- const ciphertext = ciphertextWithTag.slice(0, -16)
63
- const result = AESGCMDecrypt(
64
- ciphertext,
65
- [],
66
- iv,
67
- messageTag,
68
- this.toArray()
69
- )
70
- if (result === null) {
71
- throw new Error('Decryption failed!')
72
- }
73
- return encode(result, enc)
74
- }
75
- }
@@ -1,189 +0,0 @@
1
- import Signature from './Signature.js'
2
- import BigNumber from './BigNumber.js'
3
- import * as Hash from './Hash.js'
4
- import { toArray, Writer } from './utils.js'
5
- import Script from '../script/Script.js'
6
- import TransactionInput from '../transaction/TransactionInput.js'
7
- import TransactionOutput from '../transaction/TransactionOutput.js'
8
-
9
- export default class TransactionSignature extends Signature {
10
- public static readonly SIGHASH_ALL = 0x00000001
11
- public static readonly SIGHASH_NONE = 0x00000002
12
- public static readonly SIGHASH_SINGLE = 0x00000003
13
- public static readonly SIGHASH_FORKID = 0x00000040
14
- public static readonly SIGHASH_ANYONECANPAY = 0x00000080
15
-
16
- scope: number
17
-
18
- static format(params: {
19
- sourceTXID: string
20
- sourceOutputIndex: number
21
- sourceSatoshis: number
22
- transactionVersion: number
23
- otherInputs: TransactionInput[]
24
- outputs: TransactionOutput[]
25
- inputIndex: number
26
- subscript: Script
27
- inputSequence: number
28
- lockTime: number
29
- scope: number
30
- }): number[] {
31
- const currentInput = {
32
- sourceTXID: params.sourceTXID,
33
- sourceOutputIndex: params.sourceOutputIndex,
34
- sequence: params.inputSequence
35
- }
36
- const inputs = [...params.otherInputs]
37
- inputs.splice(params.inputIndex, 0, currentInput)
38
-
39
- const getPrevoutHash = (): number[] => {
40
- const writer = new Writer()
41
- for (const input of inputs) {
42
- if (typeof input.sourceTransaction === 'undefined') {
43
- writer.writeReverse(toArray(input.sourceTXID, 'hex'))
44
- } else {
45
- writer.writeReverse(input.sourceTransaction.id() as number[])
46
- }
47
- writer.writeUInt32LE(input.sourceOutputIndex)
48
- }
49
-
50
- const buf = writer.toArray()
51
- const ret = Hash.hash256(buf) as number[]
52
- return ret
53
- }
54
-
55
- const getSequenceHash = (): number[] => {
56
- const writer = new Writer()
57
-
58
- for (const input of inputs) {
59
- writer.writeUInt32LE(input.sequence)
60
- }
61
-
62
- const buf = writer.toArray()
63
- const ret = Hash.hash256(buf) as number[]
64
- return ret
65
- }
66
-
67
- function getOutputsHash(outputIndex?: number): number[] {
68
- const writer = new Writer()
69
-
70
- if (typeof outputIndex === 'undefined') {
71
- let script: number[]
72
- for (const output of params.outputs) {
73
- writer.writeUInt64LE(output.satoshis)
74
- script = output.lockingScript.toBinary()
75
- writer.writeVarIntNum(script.length)
76
- writer.write(script)
77
- }
78
- } else {
79
- const output = params.outputs[outputIndex]
80
- writer.writeUInt64LE(output.satoshis)
81
- const script = output.lockingScript.toBinary()
82
- writer.writeVarIntNum(script.length)
83
- writer.write(script)
84
- }
85
-
86
- const buf = writer.toArray()
87
- const ret = Hash.hash256(buf) as number[]
88
- return ret
89
- }
90
-
91
- let hashPrevouts = new Array(32).fill(0)
92
- let hashSequence = new Array(32).fill(0)
93
- let hashOutputs = new Array(32).fill(0)
94
-
95
- if ((params.scope & TransactionSignature.SIGHASH_ANYONECANPAY) === 0) {
96
- hashPrevouts = getPrevoutHash()
97
- }
98
-
99
- if ((params.scope & TransactionSignature.SIGHASH_ANYONECANPAY) === 0 &&
100
- (params.scope & 31) !== TransactionSignature.SIGHASH_SINGLE &&
101
- (params.scope & 31) !== TransactionSignature.SIGHASH_NONE) {
102
- hashSequence = getSequenceHash()
103
- }
104
-
105
- if ((params.scope & 31) !== TransactionSignature.SIGHASH_SINGLE && (params.scope & 31) !== TransactionSignature.SIGHASH_NONE) {
106
- hashOutputs = getOutputsHash()
107
- } else if ((params.scope & 31) === TransactionSignature.SIGHASH_SINGLE && params.inputIndex < params.outputs.length) {
108
- hashOutputs = getOutputsHash(params.inputIndex)
109
- }
110
-
111
- const writer = new Writer()
112
-
113
- // Version
114
- writer.writeInt32LE(params.transactionVersion)
115
-
116
- // Input prevouts/nSequence (none/all, depending on flags)
117
- writer.write(hashPrevouts)
118
- writer.write(hashSequence)
119
-
120
- // outpoint (32-byte hash + 4-byte little endian)
121
- writer.writeReverse(toArray(params.sourceTXID, 'hex'))
122
- writer.writeUInt32LE(params.sourceOutputIndex)
123
-
124
- // scriptCode of the input (serialized as scripts inside CTxOuts)
125
- writer.writeVarIntNum(params.subscript.toBinary().length)
126
- writer.write(params.subscript.toBinary())
127
-
128
- // value of the output spent by this input (8-byte little endian)
129
- writer.writeUInt64LE(params.sourceSatoshis)
130
-
131
- // nSequence of the input (4-byte little endian)
132
- const sequenceNumber = currentInput.sequence
133
- writer.writeUInt32LE(sequenceNumber)
134
-
135
- // Outputs (none/one/all, depending on flags)
136
- writer.write(hashOutputs)
137
-
138
- // Locktime
139
- writer.writeUInt32LE(params.lockTime)
140
-
141
- // sighashType
142
- writer.writeUInt32LE(params.scope >>> 0)
143
-
144
- const buf = writer.toArray()
145
- return buf
146
- }
147
-
148
- // The format used in a tx
149
- static fromChecksigFormat(buf: number[]): TransactionSignature {
150
- if (buf.length === 0) {
151
- // allow setting a "blank" signature
152
- const r = new BigNumber(1)
153
- const s = new BigNumber(1)
154
- const scope = 1
155
- return new TransactionSignature(r, s, scope)
156
- }
157
- const scope = buf[buf.length - 1]
158
- const derbuf = buf.slice(0, buf.length - 1)
159
- const tempSig = Signature.fromDER(derbuf)
160
- return new TransactionSignature(tempSig.r, tempSig.s, scope)
161
- }
162
-
163
- constructor(r: BigNumber, s: BigNumber, scope: number) {
164
- super(r, s)
165
- this.scope = scope
166
- }
167
-
168
- /**
169
- * Compares to bitcoind's IsLowDERSignature
170
- * See also Ecdsa signature algorithm which enforces this.
171
- * See also Bip 62, "low S values in signatures"
172
- */
173
- public hasLowS(): boolean {
174
- if (
175
- this.s.ltn(1) ||
176
- this.s.gt(new BigNumber(
177
- '7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex'
178
- ))
179
- ) {
180
- return false
181
- }
182
- return true
183
- }
184
-
185
- toChecksigFormat(): number[] {
186
- const derbuf = this.toDER() as number[]
187
- return [...derbuf, this.scope]
188
- }
189
- }