forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create KeyBuilder (neo-project#1748)
- Loading branch information
Showing
5 changed files
with
76 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Neo.IO; | ||
using Neo.Ledger; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Neo.SmartContract.Native | ||
{ | ||
internal class KeyBuilder | ||
{ | ||
private readonly int id; | ||
private readonly MemoryStream stream = new MemoryStream(); | ||
|
||
public KeyBuilder(int id, byte prefix) | ||
{ | ||
this.id = id; | ||
this.stream.WriteByte(prefix); | ||
} | ||
|
||
public KeyBuilder Add(ReadOnlySpan<byte> key) | ||
{ | ||
stream.Write(key); | ||
return this; | ||
} | ||
|
||
public KeyBuilder Add(ISerializable key) | ||
{ | ||
using (BinaryWriter writer = new BinaryWriter(stream, Utility.StrictUTF8, true)) | ||
{ | ||
key.Serialize(writer); | ||
writer.Flush(); | ||
} | ||
return this; | ||
} | ||
|
||
unsafe public KeyBuilder Add<T>(T key) where T : unmanaged | ||
{ | ||
return Add(new ReadOnlySpan<byte>(&key, sizeof(T))); | ||
} | ||
|
||
public static implicit operator StorageKey(KeyBuilder builder) | ||
{ | ||
using (builder.stream) | ||
{ | ||
return new StorageKey | ||
{ | ||
Id = builder.id, | ||
Key = builder.stream.ToArray() | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters