Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable converting between public keys, script hashes and address #1428

Merged
merged 40 commits into from
Apr 8, 2020
Merged
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
36c1d16
Merge pull request #1 from neo-project/master
Qiao-Jin Oct 22, 2019
a1af198
Merge pull request #3 from neo-project/master
Qiao-Jin Oct 23, 2019
3b8b3fe
Merge pull request #6 from neo-project/master
Qiao-Jin Jan 21, 2020
e9cf99b
Enable converting between public keys, script hashes and address
Jan 21, 2020
2e5dbda
Add test cases
Jan 22, 2020
c42daf5
Merge branch 'master' into master
shargon Feb 5, 2020
914f831
Merge branch 'master' into master
Qiao-Jin Feb 12, 2020
7af4612
Code optimization
Feb 19, 2020
a5d6d89
Fee adjusting
Feb 19, 2020
f747e5a
Code optimization
Feb 19, 2020
111b5c7
Merge pull request #8 from neo-project/master
Qiao-Jin Feb 25, 2020
b7c04c5
Add comment
Feb 25, 2020
3afeb33
Merge branch 'master' into master
Qiao-Jin Mar 2, 2020
5117513
Merge branch 'master' into master
Qiao-Jin Mar 4, 2020
e7df5b1
Code optimization
Mar 9, 2020
4bf1d92
Merge pull request #9 from neo-project/master
Qiao-Jin Mar 13, 2020
b100d39
Code optimization
Mar 13, 2020
c7a332b
Merge from master
Mar 16, 2020
f452a68
Merge branch 'master' into master
Qiao-Jin Mar 16, 2020
9cbcfdc
Update InteropService.Contract.cs
erikzhang Mar 17, 2020
1962b94
Merge pull request #11 from neo-project/master
Qiao-Jin Mar 18, 2020
93fe8a7
Code optimization
Mar 18, 2020
e9f2214
Merge branch 'master' into master
shargon Mar 18, 2020
5efda66
Add wrong public key test cases
Mar 19, 2020
fa70610
Kick off new test
Mar 19, 2020
0e2a04b
format changing
Mar 19, 2020
bc2678c
Merge pull request #14 from neo-project/master
Qiao-Jin Mar 23, 2020
d306fb7
Merge branch 'master' into master
shargon Mar 23, 2020
57d1aeb
Merge pull request #21 from neo-project/master
Qiao-Jin Mar 24, 2020
5bb6d4e
Merge pull request #27 from neo-project/master
Qiao-Jin Mar 24, 2020
308b0f0
Merge branch 'master' into master
Qiao-Jin Mar 26, 2020
929aa41
Merge branch 'master' into master
shargon Mar 26, 2020
0a37b33
Code optimization
Mar 27, 2020
1913033
Merge branch 'master' into master
Qiao-Jin Mar 27, 2020
4a1c746
Merge branch 'master' into master
Qiao-Jin Mar 31, 2020
09b2852
Add comment
Apr 1, 2020
8700661
Merge branch 'master' into master
Qiao-Jin Apr 2, 2020
3075410
Merge branch 'master' into master
Qiao-Jin Apr 3, 2020
8eb00a7
Merge branch 'master' into master
Qiao-Jin Apr 3, 2020
c8fb1be
Merge branch 'master' into master
Qiao-Jin Apr 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/neo/SmartContract/InteropService.Encode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Neo.Cryptography;
using Neo.IO;
using Neo.VM;
using Neo.Wallets;
using System;
using System.Linq;

namespace Neo.SmartContract
{
partial class InteropService
{
public static class Encode
Qiao-Jin marked this conversation as resolved.
Show resolved Hide resolved
{
public static readonly InteropDescriptor PubKey2Address = Register("Neo.Crypto.PubKey2Address", Crypto_PubKey2Address, 0_01000000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor PubKey2ScriptHash = Register("Neo.Crypto.PubKey2ScriptHash", Crypto_PubKey2ScriptHash, 0_01000000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor Address2ScriptHash = Register("Neo.Crypto.Address2ScriptHash", Crypto_Address2ScriptHash, 0_01000000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor ScriptHash2Address = Register("Neo.Crypto.ScriptHash2Address", Crypto_ScriptHash2Address, 0_01000000, TriggerType.All, CallFlags.None);

private static bool Crypto_PubKey2Address(ApplicationEngine engine)
{
ReadOnlySpan<byte> pubKey = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
byte[] scriptHash = Cryptography.ECC.ECPoint.FromBytes(pubKey.ToArray(), Cryptography.ECC.ECCurve.Secp256r1).EncodePoint(true).ToScriptHash().ToAddress().HexToBytes(); ;
engine.CurrentContext.EvaluationStack.Push(scriptHash);
return true;
}

private static bool Crypto_PubKey2ScriptHash(ApplicationEngine engine)
{
ReadOnlySpan<byte> pubKey = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
byte[] scriptHash = Cryptography.ECC.ECPoint.FromBytes(pubKey.ToArray(), Cryptography.ECC.ECCurve.Secp256r1).EncodePoint(true).ToScriptHash().ToArray();
engine.CurrentContext.EvaluationStack.Push(scriptHash);
return true;
}

private static bool Crypto_Address2ScriptHash(ApplicationEngine engine)
{
ReadOnlySpan<byte> address = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
byte[] scriptHash = address.ToHexString().ToScriptHash().ToArray();
engine.CurrentContext.EvaluationStack.Push(scriptHash);
return true;
}

private static bool Crypto_ScriptHash2Address(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
byte[] address = new UInt160(hash).ToAddress().HexToBytes();
engine.CurrentContext.EvaluationStack.Push(address);
return true;
}
}
}
}