forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecsignature.js
122 lines (99 loc) · 3.94 KB
/
ecsignature.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* global describe, it */
var assert = require('assert')
var BigInteger = require('bigi')
var ECSignature = require('../src/ecsignature')
var fixtures = require('./fixtures/ecsignature.json')
describe('ECSignature', function () {
describe('toCompact', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.compact.hex + ' correctly', function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
var buffer = signature.toCompact(f.compact.i, f.compact.compressed)
assert.equal(buffer.toString('hex'), f.compact.hex)
})
})
})
describe('parseCompact', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.compact.hex + ' correctly', function () {
var buffer = new Buffer(f.compact.hex, 'hex')
var parsed = ECSignature.parseCompact(buffer)
assert.equal(parsed.compressed, f.compact.compressed)
assert.equal(parsed.i, f.compact.i)
assert.equal(parsed.signature.r.toString(), f.signature.r)
assert.equal(parsed.signature.s.toString(), f.signature.s)
})
})
fixtures.invalid.compact.forEach(function (f) {
it('throws on ' + f.hex, function () {
var buffer = new Buffer(f.hex, 'hex')
assert.throws(function () {
ECSignature.parseCompact(buffer)
}, new RegExp(f.exception))
})
})
})
describe('toDER', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.DER + ' correctly', function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
var DER = signature.toDER()
assert.equal(DER.toString('hex'), f.DER)
})
})
})
describe('fromDER', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.DER + ' correctly', function () {
var buffer = new Buffer(f.DER, 'hex')
var signature = ECSignature.fromDER(buffer)
assert.equal(signature.r.toString(), f.signature.r)
assert.equal(signature.s.toString(), f.signature.s)
})
})
fixtures.invalid.DER.forEach(function (f) {
it('throws on ' + f.hex, function () {
var buffer = new Buffer(f.hex, 'hex')
assert.throws(function () {
ECSignature.fromDER(buffer)
}, new RegExp(f.exception))
})
})
})
describe('toScriptSignature', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.scriptSignature.hex + ' correctly', function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType)
assert.equal(scriptSignature.toString('hex'), f.scriptSignature.hex)
})
})
fixtures.invalid.scriptSignature.forEach(function (f) {
it('throws ' + f.exception, function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
assert.throws(function () {
signature.toScriptSignature(f.hashType)
}, new RegExp(f.exception))
})
})
})
describe('parseScriptSignature', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.scriptSignature.hex + ' correctly', function () {
var buffer = new Buffer(f.scriptSignature.hex, 'hex')
var parsed = ECSignature.parseScriptSignature(buffer)
assert.equal(parsed.signature.r.toString(), f.signature.r)
assert.equal(parsed.signature.s.toString(), f.signature.s)
assert.equal(parsed.hashType, f.scriptSignature.hashType)
})
})
fixtures.invalid.scriptSignature.forEach(function (f) {
it('throws on ' + f.hex, function () {
var buffer = new Buffer(f.hex, 'hex')
assert.throws(function () {
ECSignature.parseScriptSignature(buffer)
}, new RegExp(f.exception))
})
})
})
})