Meki Cherkaoui | b0365a7 | 2012-02-18 00:59:57 -0800 | [diff] [blame] | 1 | var CCNProtocolDTags = require('./CCNProtocolDTags').CCNProtocolDTags; |
| 2 | |
| 3 | var SignedInfo = function SignedInfo(_publisher,_timestamp,_type,_locator,_freshnessSeconds,_finalBlockID){ |
| 4 | |
| 5 | //TODO, Check types |
| 6 | |
| 7 | this.Publisher = _publisher; //PublisherPublicKeyDigest |
| 8 | this.Timestamp=_timestamp; // CCN Time |
| 9 | this.Type=_type; // ContentType |
| 10 | this.Locator =_locator;//KeyLocator |
| 11 | this.FreshnessSeconds =_freshnessSeconds; // Integer |
| 12 | this.FinalBlockID=_finalBlockID; //byte array |
| 13 | |
| 14 | }; |
| 15 | |
| 16 | //exports.SignedInfo.Working; |
| 17 | |
| 18 | |
| 19 | SignedInfo.prototype.decode = function( decoder){ |
| 20 | |
| 21 | decoder.readStartElement( this.getElementLabel() ); |
| 22 | |
| 23 | if (decoder.peekStartElement(CCNProtocolDTags.PublisherPublicKeyDigest)) { |
| 24 | this.Publisher = new PublisherPublicKeyDigest(); |
| 25 | this.Publisher.decode(decoder); |
| 26 | } |
| 27 | |
| 28 | if (decoder.peekStartElement(CCNProtocolDTags.Timestamp)) { |
| 29 | this.Timestamp = decoder.readDateTime(CCNProtocolDTags.Timestamp); |
| 30 | } |
| 31 | |
| 32 | if (decoder.peekStartElement(CCNProtocolDTags.Type)) { |
| 33 | binType = decoder.readBinaryElement(CCNProtocolDTags.Type);//byte [] |
| 34 | //TODO Implement valueToType |
| 35 | |
| 36 | this.Type = valueToType(binType); |
| 37 | if (null == this.Type) { |
| 38 | throw new Exception("Cannot parse signedInfo type: bytes."); |
| 39 | } |
| 40 | |
| 41 | } else { |
| 42 | this.Type = ContentType.DATA; // default |
| 43 | } |
| 44 | |
| 45 | if (decoder.peekStartElement(CCNProtocolDTags.FreshnessSeconds)) { |
| 46 | this.FreshnessSeconds = decoder.readIntegerElement(CCNProtocolDTags.FreshnessSeconds); |
| 47 | } |
| 48 | |
| 49 | if (decoder.peekStartElement(CCNProtocolDTags.FinalBlockID)) { |
| 50 | this.FinalBlockID = decoder.readBinaryElement(CCNProtocolDTags.FinalBlockID); |
| 51 | } |
| 52 | |
| 53 | if (decoder.peekStartElement(CCNProtocolDTags.KeyLocator)) { |
| 54 | this.Locator = new KeyLocator(); |
| 55 | this.Locator.decode(decoder); |
| 56 | } |
| 57 | |
| 58 | decoder.readEndElement(); |
| 59 | }; |
| 60 | |
| 61 | SignedInfo.prototype.encode = function( encoder) { |
| 62 | if (!this.validate()) { |
| 63 | throw new Exception("Cannot encode : field values missing."); |
| 64 | } |
| 65 | encoder.writeStartElement(this.getElementLabel()); |
| 66 | |
| 67 | if (null!=this.Publisher) { |
| 68 | this.Publisher.encode(encoder); |
| 69 | } |
| 70 | |
| 71 | if (null!=this.Timestamp) { |
| 72 | encoder.writeDateTime(CCNProtocolDTags.Timestamp, this.Timestamp); |
| 73 | } |
| 74 | |
| 75 | if (null!=this.Type) { |
| 76 | |
| 77 | encoder.writeElement(CCNProtocolDTags.Type, this.Type); |
| 78 | } |
| 79 | |
| 80 | if (null!=this.FreshnessSeconds) { |
| 81 | encoder.writeElement(CCNProtocolDTags.FreshnessSeconds, this.FreshnessSeconds); |
| 82 | } |
| 83 | |
| 84 | if (null!=this.FinalBlockID) { |
| 85 | encoder.writeElement(CCNProtocolDTags.FinalBlockID, this.FinalBlockID); |
| 86 | } |
| 87 | |
| 88 | if (null!=this.Locator) { |
| 89 | this.Locator.encode(encoder); |
| 90 | } |
| 91 | |
| 92 | encoder.writeEndElement(); |
| 93 | }; |
| 94 | |
| 95 | SignedInfo.prototype.getElementLabel = function() { |
| 96 | return CCNProtocolDTags.SignedInfo; |
| 97 | }; |
| 98 | |
| 99 | SignedInfo.prototype.validate = function() { |
| 100 | // We don't do partial matches any more, even though encoder/decoder |
| 101 | // is still pretty generous. |
| 102 | if (null ==this.Publisher || null==this.Timestamp ||null== this.Locator) |
| 103 | return false; |
| 104 | return true; |
| 105 | }; |
| 106 | |