Merge pull request #28 from jgarzik/sin

Broken SIN stuff
This commit is contained in:
Stephen Pair 2013-08-16 19:32:47 -07:00
commit 78adf271a4
3 changed files with 105 additions and 0 deletions

59
SIN.js Normal file
View File

@ -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);

39
SINKey.js Normal file
View File

@ -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);

7
sin-test.js Normal file
View File

@ -0,0 +1,7 @@
var SINKey = require('./SINKey').class();
var sk = new SINKey();
sk.generate();
console.dir(sk.storeObj());