From 14dd5ea407f512f3951b4834042b6111e10888aa Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Fri, 16 Aug 2013 22:22:50 -0400 Subject: [PATCH] Broken SIN stuff --- SIN.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ SINKey.js | 39 +++++++++++++++++++++++++++++++++++ sin-test.js | 7 +++++++ 3 files changed, 105 insertions(+) create mode 100644 SIN.js create mode 100644 SINKey.js create mode 100644 sin-test.js diff --git a/SIN.js b/SIN.js new file mode 100644 index 000000000..8d02906a1 --- /dev/null +++ b/SIN.js @@ -0,0 +1,59 @@ +require('classtool'); + +function ClassSpec(b) { + var superclass = b.superclass || require('./util/VersionedData').class(); + + var SIN_PERSIST_MAINNET = 0x01; // associated with sacrifice TX + var SIN_PERSIST_TESTNET = 0x11; // associated with sacrifice TX + var SIN_EPHEM = 0x02; // generate off-net at any time + + function SIN(type, payload) { + if (typeof type != 'number') { + SIN.super(this, arguments); + return; + }; + this.data = new Buffer(1 + 1 + payload.length); + this.__proto__ = this.encodings['binary']; + this.prefix(0x18); // SIN magic number, in numberspace + this.type(type); + this.payload(payload); + }; + SIN.superclass = superclass; + superclass.applyEncodingsTo(SIN); + + // get or set the prefix data (the first byte of the address) + SIN.prototype.prefix = function(num) { + if(num || (num === 0)) { + this.doAsBinary(function() {this.data.writeUInt8(num, 0);}); + return num; + } + return this.as('binary').readUInt8(0); + }; + + // get or set the SIN-type data (the second byte of the address) + SIN.prototype.type = function(num) { + if(num || (num === 0)) { + this.doAsBinary(function() {this.data.writeUInt8(num, 1);}); + return num; + } + return this.as('binary').readUInt8(1); + }; + + // get or set the payload data (as a Buffer object) + SIN.prototype.payload = function(data) { + if(data) { + this.doAsBinary(function() {data.copy(this.data, 2);}); + return data; + } + return this.as('binary').slice(1); + }; + + SIN.prototype.validate = function() { + this.doAsBinary(function() { + SIN.super(this, 'validate', arguments); + if (this.data.length != 22) throw new Error('invalid data length'); + }); + }; + return SIN; +}; +module.defineClass(ClassSpec); diff --git a/SINKey.js b/SINKey.js new file mode 100644 index 000000000..54bc54a97 --- /dev/null +++ b/SINKey.js @@ -0,0 +1,39 @@ +require('classtool'); + +function ClassSpec(b) { + var coinUtil = require('./util/util'); + var timeUtil = require('./util/time'); + var KeyModule = require('./Key'); + var SIN = require('./SIN').class(); + + function SINKey(cfg) { + if (typeof cfg != 'object') + cfg = {}; + + this.created = cfg.created; + this.privKey = cfg.privKey; + }; + + SINKey.prototype.generate = function() { + this.privKey = KeyModule.Key.generateSync(); + this.created = timeUtil.curtime(); + }; + + SINKey.prototype.storeObj = function() { + var pubKey = this.privKey.public.toString('hex'); + var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public); + var sin = new SIN(SIN.SIN_EPHEM, pubKeyHash); + var obj = { + created: this.created, + priv: this.privKey.private.toString('hex'), + pub: pubKey, + sin: sin.toString(), + }; + + return obj; + }; + + return SINKey; +}; +module.defineClass(ClassSpec); + diff --git a/sin-test.js b/sin-test.js new file mode 100644 index 000000000..df00923bf --- /dev/null +++ b/sin-test.js @@ -0,0 +1,7 @@ + +var SINKey = require('./SINKey').class(); + +var sk = new SINKey(); +sk.generate(); +console.dir(sk.storeObj()); +