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