Skip to content

Commit

Permalink
Reformatted into functions (removed new, class, and prototype)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Calfee committed Sep 18, 2017
1 parent 2b5bf6e commit 7277843
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 359 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"main": "lib/index.js",
"scripts": {
"prepublish": "npm run build_lib && npm run docs",
"build_lib": "node_modules/babel-cli/bin/babel.js src --out-dir lib",
"docs": "npm run docs_common",
"docs_common": "npm run doc -- readme src/api_common.js --section \"Common API\" --shallow",
"doc": "node_modules/documentation/bin/documentation.js",
"build_lib": "node_modules/babel-cli/bin/babel.js src --out-dir lib",
"build": "mkdir -p dist && browserify -o dist/eosjs-ecc.js -s eosjs_ecc src/index.js && sha256sum dist/eosjs-ecc.js"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function crypt(private_key, public_key, nonce, message, checksum) {

// D E B U G
// console.log('crypt', {
// priv_to_pub: private_key.toPublicKey().toString(),
// priv_to_pub: private_key.toPublic().toString(),
// pub: public_key.toString(),
// nonce: nonce.toString(),
// message: message.length,
Expand Down
2 changes: 1 addition & 1 deletion src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function sign(curve, hash, d, nonce) {
s = n.subtract(s)
}

return new ECSignature(r, s)
return ECSignature(r, s)
}

function verifyRaw(curve, e, signature, Q) {
Expand Down
88 changes: 43 additions & 45 deletions src/ecsignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,47 @@ function ECSignature(r, s) {
enforceType(BigInteger, r)
enforceType(BigInteger, s)

this.r = r
this.s = s
function toCompact(i, compressed) {
if (compressed) i += 4
i += 27

var buffer = new Buffer(65)
buffer.writeUInt8(i, 0)

r.toBuffer(32).copy(buffer, 1)
s.toBuffer(32).copy(buffer, 33)

return buffer
}

function toDER() {
var rBa = r.toDERInteger()
var sBa = s.toDERInteger()

var sequence = []

// INTEGER
sequence.push(0x02, rBa.length)
sequence = sequence.concat(rBa)

// INTEGER
sequence.push(0x02, sBa.length)
sequence = sequence.concat(sBa)

// SEQUENCE
sequence.unshift(0x30, sequence.length)

return new Buffer(sequence)
}

function toScriptSignature(hashType) {
var hashTypeBuffer = new Buffer(1)
hashTypeBuffer.writeUInt8(hashType, 0)

return Buffer.concat([toDER(), hashTypeBuffer])
}

return {r, s, toCompact, toDER, toScriptSignature}
}

// Import operations
Expand All @@ -29,7 +68,7 @@ ECSignature.parseCompact = function(buffer) {
return {
compressed: compressed,
i: i,
signature: new ECSignature(r, s)
signature: ECSignature(r, s)
}
}

Expand Down Expand Up @@ -66,7 +105,7 @@ ECSignature.fromDER = function(buffer) {
assert(r.signum() >= 0, 'R value is negative')
assert(s.signum() >= 0, 'S value is negative')

return new ECSignature(r, s)
return ECSignature(r, s)
}

// FIXME: 0x00, 0x04, 0x80 are SIGHASH_* boundary constants, importing Transaction causes a circular dependency
Expand All @@ -82,45 +121,4 @@ ECSignature.parseScriptSignature = function(buffer) {
}
}

// Export operations
ECSignature.prototype.toCompact = function(i, compressed) {
if (compressed) i += 4
i += 27

var buffer = new Buffer(65)
buffer.writeUInt8(i, 0)

this.r.toBuffer(32).copy(buffer, 1)
this.s.toBuffer(32).copy(buffer, 33)

return buffer
}

ECSignature.prototype.toDER = function() {
var rBa = this.r.toDERInteger()
var sBa = this.s.toDERInteger()

var sequence = []

// INTEGER
sequence.push(0x02, rBa.length)
sequence = sequence.concat(rBa)

// INTEGER
sequence.push(0x02, sBa.length)
sequence = sequence.concat(sBa)

// SEQUENCE
sequence.unshift(0x30, sequence.length)

return new Buffer(sequence)
}

ECSignature.prototype.toScriptSignature = function(hashType) {
var hashTypeBuffer = new Buffer(1)
hashTypeBuffer.writeUInt8(hashType, 0)

return Buffer.concat([this.toDER(), hashTypeBuffer])
}

module.exports = ECSignature
Loading

0 comments on commit 7277843

Please sign in to comment.