1 module tagion.dart.DARTFakeNet;
2 
3 import std.typecons : Typedef;
4 
5 //import tagion.gossip.InterfaceNet : SecureNet, HashNet;
6 import tagion.basic.Types : Buffer, Control;
7 import tagion.crypto.SecureNet : StdSecureNet;
8 import tagion.crypto.Types : BufferType, Fingerprint;
9 import tagion.dart.DART;
10 import tagion.dart.DARTFile : DARTFile;
11 import tagion.dart.Recorder : RecordFactory;
12 import tagion.hibon.Document : Document;
13 import tagion.hibon.HiBON : HiBON;
14 import tagion.hibon.HiBONRecord : HiBONPrefix;
15 
16 /**
17 * This is the raw-hash value of a message and is used when message is signed.
18 */
19 alias DARTIndex = Typedef!(Buffer, null, BufferType.HASHPOINTER.stringof);
20 
21 @safe
22 class DARTFakeNet : StdSecureNet {
23     enum FAKE = "$fake#";
24     this(string passphrase) {
25         this();
26         generateKeyPair(passphrase);
27     }
28 
29     this() {
30         super();
31 
32     }
33 
34     override Fingerprint calcHash(scope const(ubyte[]) h) const {
35         if (h.length is ulong.sizeof) {
36             scope ubyte[] fake_h;
37             fake_h.length = hashSize;
38             fake_h[0 .. ulong.sizeof] = h;
39             return Fingerprint(fake_h.idup);
40         }
41         return Fingerprint(super.rawCalcHash(h));
42     }
43 
44     @trusted
45     override Fingerprint calcHash(const(Document) doc) const {
46         import std.exception : assumeUnique;
47         import tagion.hibon.HiBONBase : Type;
48 
49         if (doc.hasMember(FAKE) && (doc[FAKE].type is Type.UINT64)) {
50             const x = doc[FAKE].get!ulong;
51             import std.bitmanip : nativeToBigEndian;
52 
53             ubyte[] fingerprint;
54             fingerprint.length = hashSize;
55             fingerprint[0 .. ulong.sizeof] = nativeToBigEndian(x);
56             return Fingerprint(assumeUnique(fingerprint));
57         }
58         return super.calcHash(doc);
59         //return rawCalcHash(doc.serialize);
60     }
61 
62     static const(Document) fake_doc(const ulong x) {
63         auto hibon = new HiBON;
64         hibon[FAKE] = x;
65         return Document(hibon);
66     }
67 
68     enum hashname = "fake256";
69     override string multihash() const pure nothrow @nogc {
70         return hashname;
71     }
72 }