-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Erik Zhang
committed
Jul 19, 2016
1 parent
293c367
commit 1a4c62b
Showing
2 changed files
with
120 additions
and
0 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,77 @@ | ||
namespace AntShares.Core | ||
{ | ||
export class SignatureContext | ||
{ | ||
public signable: ISignable; | ||
public scriptHashes: Uint160[]; | ||
private redeemScripts: ArrayBuffer[]; | ||
private signatures: Map<string, ArrayBuffer>[]; | ||
private completed: boolean[]; | ||
|
||
public add(contract: Wallets.Contract, pubkey: Cryptography.ECPoint, signature: ArrayBuffer): boolean | ||
{ | ||
for (let i = 0; i < this.scriptHashes.length; i++) | ||
{ | ||
if (this.scriptHashes[i].equals(contract.scriptHash)) | ||
{ | ||
if (this.redeemScripts[i] == null) | ||
this.redeemScripts[i] = contract.redeemScript; | ||
if (this.signatures[i] == null) | ||
this.signatures[i] = new Map<string, ArrayBuffer>(); | ||
this.signatures[i].set(pubkey.toString(), signature); | ||
let completed = contract.parameterList.length == this.signatures[i].size; | ||
for (let j = 0; j < contract.parameterList.length && completed; j++) | ||
if (contract.parameterList[j] != Wallets.ContractParameterType.Signature) | ||
completed = false; | ||
this.completed[i] = this.completed[i] || completed; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static create(signable: ISignable): PromiseLike<SignatureContext> | ||
{ | ||
return signable.getScriptHashesForVerifying().then(result => | ||
{ | ||
let context = new SignatureContext(); | ||
context.signable = signable; | ||
context.scriptHashes = result; | ||
context.redeemScripts = new Array<ArrayBuffer>(result.length); | ||
context.signatures = new Array<Map<string, ArrayBuffer>>(result.length); | ||
context.completed = new Array<boolean>(result.length); | ||
return context; | ||
}); | ||
} | ||
|
||
public getScripts(): Core.Scripts.Script[] | ||
{ | ||
if (!this.isCompleted()) throw new Error(); | ||
let scripts = new Array<Core.Scripts.Script>(this.signatures.length); | ||
for (let i = 0; i < scripts.length; i++) | ||
{ | ||
let array = new Array<{ pubkey: Cryptography.ECPoint, signature: ArrayBuffer }>(); | ||
this.signatures[i].forEach((signature, key) => | ||
{ | ||
array.push({ pubkey: Cryptography.ECPoint.parse(key, Cryptography.ECCurve.secp256r1), signature: signature }); | ||
}); | ||
array.sort((a, b) => a.pubkey.compareTo(b.pubkey)); | ||
let sb = new Core.Scripts.ScriptBuilder(); | ||
for (let j = 0; j < array.length; j++) | ||
sb.push(array[j].signature); | ||
scripts[i] = new Core.Scripts.Script(); | ||
scripts[i].stackScript = sb.toArray(); | ||
scripts[i].redeemScript = this.redeemScripts[i]; | ||
} | ||
return scripts; | ||
} | ||
|
||
public isCompleted(): boolean | ||
{ | ||
for (let i = 0; i < this.completed.length; i++) | ||
if (!this.completed[i]) | ||
return false; | ||
return true; | ||
} | ||
} | ||
} |
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