Skip to content

Commit

Permalink
custom contract; fix bug;
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Nov 8, 2016
1 parent 66999b5 commit 3834f19
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 198 deletions.
2 changes: 1 addition & 1 deletion src/AntShares/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void CheckSignatures()
for (int i = 0, j = 0; i < context.Miners.Length && j < context.M; i++)
if (context.Signatures[i] != null)
{
sc.Add(contract, context.Miners[i], context.Signatures[i]);
sc.AddSignature(contract, context.Miners[i], context.Signatures[i]);
j++;
}
sc.Signable.Scripts = sc.GetScripts();
Expand Down
195 changes: 0 additions & 195 deletions src/AntShares/Core/SignatureContext.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/AntShares/IO/Json/JObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static implicit operator JObject(double value)

public static implicit operator JObject(string value)
{
return new JString(value);
return value == null ? null : new JString(value);
}
}
}
58 changes: 58 additions & 0 deletions src/AntShares/Wallets/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public UInt160 ScriptHash

public int Size => PublicKeyHash.Size + ParameterList.Length.GetVarSize() + ParameterList.Length + RedeemScript.Length.GetVarSize() + RedeemScript.Length;

public ContractType Type
{
get
{
if (IsStandard) return ContractType.SignatureContract;
if (IsMultiSigContract()) return ContractType.MultiSigContract;
return ContractType.CustomContract;
}
}

public static Contract Create(UInt160 publicKeyHash, ContractParameterType[] parameterList, byte[] redeemScript)
{
return new Contract
Expand Down Expand Up @@ -169,6 +179,54 @@ public override int GetHashCode()
return ScriptHash.GetHashCode();
}

private bool IsMultiSigContract()
{
int m, n = 0;
int i = 0;
if (RedeemScript.Length < 37) return false;
if (RedeemScript[i] > (byte)ScriptOp.OP_16) return false;
if (RedeemScript[i] < (byte)ScriptOp.OP_1 && RedeemScript[i] != 1 && RedeemScript[i] != 2) return false;
switch (RedeemScript[i])
{
case 1:
m = RedeemScript[++i];
++i;
break;
case 2:
m = BitConverter.ToUInt16(RedeemScript, ++i);
i += 2;
break;
default:
m = RedeemScript[i++] - 80;
break;
}
if (m < 1 || m > 1024) return false;
while (RedeemScript[i] == 33)
{
i += 34;
if (RedeemScript.Length <= i) return false;
++n;
}
if (n < m || n > 1024) return false;
switch (RedeemScript[i])
{
case 1:
if (n != RedeemScript[++i]) return false;
++i;
break;
case 2:
if (n != BitConverter.ToUInt16(RedeemScript, ++i)) return false;
i += 2;
break;
default:
if (n != RedeemScript[i++] - 80) return false;
break;
}
if (RedeemScript[i++] != (byte)ScriptOp.OP_CHECKMULTISIG) return false;
if (RedeemScript.Length != i) return false;
return true;
}

/// <summary>
/// 序列化
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/AntShares/Wallets/ContractType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AntShares.Wallets
{
public enum ContractType : byte
{
SignatureContract,
MultiSigContract,
CustomContract
}
}
Loading

0 comments on commit 3834f19

Please sign in to comment.