forked from yenom/BitcoinKit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from BitcoinCashKit/add-crypto-opcodes
✨ Add and implement OpRipeMd160, OP_SHA1, OP_SHA256, OP_HASH…
- Loading branch information
Showing
10 changed files
with
155 additions
and
5 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
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
19 changes: 19 additions & 0 deletions
19
BitcoinCashKit/Core/Scripts/OP_CODE/Crypto Operators/OP_CODESEPARATOR.swift
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,19 @@ | ||
// | ||
// OP_CODESEPARATOR.swift | ||
// BitcoinCashKit | ||
// | ||
// Created by Shun Usami on 2018/08/09. | ||
// Copyright © 2018 BitcoinCashKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// All of the signature checking words will only match signatures to the data after the most recently-executed OP_CODESEPARATOR. | ||
public struct OpCodeSeparator: OpCodeProtocol { | ||
public var value: UInt8 { return 0xab } | ||
public var name: String { return "OP_CODESEPARATOR" } | ||
|
||
public func mainProcess(_ context: ScriptExecutionContext) throws { | ||
// do nothing | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BitcoinCashKit/Core/Scripts/OP_CODE/Crypto Operators/OP_HASH256.swift
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,25 @@ | ||
// | ||
// OP_HASH256.swift | ||
// BitcoinCashKit | ||
// | ||
// Created by Shun Usami on 2018/08/09. | ||
// Copyright © 2018 BitcoinCashKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// The input is hashed two times with SHA-256. | ||
public struct OpHash256: OpCodeProtocol { | ||
public var value: UInt8 { return 0xaa } | ||
public var name: String { return "OP_HASH256" } | ||
|
||
// input : in | ||
// output : hash | ||
public func mainProcess(_ context: ScriptExecutionContext) throws { | ||
try context.assertStackHeightGreaterThan(1) | ||
|
||
let data: Data = context.stack.removeLast() | ||
let hash: Data = Crypto.sha256sha256(data) | ||
context.stack.append(hash) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BitcoinCashKit/Core/Scripts/OP_CODE/Crypto Operators/OP_RIPEMD160.swift
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,25 @@ | ||
// | ||
// OP_RIPEMD160.swift | ||
// BitcoinCashKit | ||
// | ||
// Created by Shun Usami on 2018/08/09. | ||
// Copyright © 2018 BitcoinCashKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// The input is hashed using RIPEMD-160. | ||
public struct OpRipemd160: OpCodeProtocol { | ||
public var value: UInt8 { return 0xa6 } | ||
public var name: String { return "OP_RIPEMD160" } | ||
|
||
// input : in | ||
// output : hash | ||
public func mainProcess(_ context: ScriptExecutionContext) throws { | ||
try context.assertStackHeightGreaterThan(1) | ||
|
||
let data: Data = context.stack.removeLast() | ||
let hash: Data = Crypto.ripemd160(data) | ||
context.stack.append(hash) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BitcoinCashKit/Core/Scripts/OP_CODE/Crypto Operators/OP_SHA1.swift
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,25 @@ | ||
// | ||
// OP_SHA1.swift | ||
// BitcoinCashKit | ||
// | ||
// Created by Shun Usami on 2018/08/09. | ||
// Copyright © 2018 BitcoinCashKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// The input is hashed using SHA-1. | ||
public struct OpSha1: OpCodeProtocol { | ||
public var value: UInt8 { return 0xa7 } | ||
public var name: String { return "OP_SHA1" } | ||
|
||
// input : in | ||
// output : hash | ||
public func mainProcess(_ context: ScriptExecutionContext) throws { | ||
try context.assertStackHeightGreaterThan(1) | ||
|
||
let data: Data = context.stack.removeLast() | ||
let hash: Data = Crypto.sha1(data) | ||
context.stack.append(hash) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
BitcoinCashKit/Core/Scripts/OP_CODE/Crypto Operators/OP_SHA256.swift
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,25 @@ | ||
// | ||
// OP_SHA256.swift | ||
// BitcoinCashKit | ||
// | ||
// Created by Shun Usami on 2018/08/09. | ||
// Copyright © 2018 BitcoinCashKit developers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// The input is hashed using SHA-256. | ||
public struct OpSha256: OpCodeProtocol { | ||
public var value: UInt8 { return 0xa8 } | ||
public var name: String { return "OP_SHA256" } | ||
|
||
// input : in | ||
// output : hash | ||
public func mainProcess(_ context: ScriptExecutionContext) throws { | ||
try context.assertStackHeightGreaterThan(1) | ||
|
||
let data: Data = context.stack.removeLast() | ||
let hash: Data = Crypto.sha256(data) | ||
context.stack.append(hash) | ||
} | ||
} |
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