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

feat: Add DelegateContractId #79

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
88 changes: 88 additions & 0 deletions Sources/Hedera/Contract/DelegateContractId.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import Foundation
import HederaProtobufs

// fixme(sr,docs): describe what a Delegate contract ID is actually for / what it does / etc.
/// A unique identifier for a smart contract on Hedera.
public struct DelegateContractId: EntityId {
private init(_ inner: ContractId) {
self.inner = inner
}

private let inner: ContractId

/// A non-negative number identifying the shard containing this contract instance.
public var shard: UInt64 { inner.shard }

/// A non-negative number identifying the realm within the shard containing this contract instance.
public var realm: UInt64 { inner.realm }

/// A non-negative number identifying the entity within the realm containing this contract instance.
///
/// >Note: Exactly one of `evmAddress` and `num` must exist.
public var num: UInt64 { inner.num }

/// A checksum if the contract ID was read from a user inputted string which included a checksum
public var checksum: Checksum? { inner.checksum }

/// EVM address identifying the entity within the realm containing this contract instance.
///
/// >Note: Exactly one of `evm_address` and `num` must exist.
public var evmAddress: Data? { inner.evmAddress }

/// Create a DelegateContractId from the given shard/realm/num
public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64, checksum: Checksum?) {
inner = .init(shard: shard, realm: realm, num: num, checksum: checksum)
}

public init(shard: UInt64 = 0, realm: UInt64 = 0, num: UInt64) {
self.init(shard: shard, realm: realm, num: num, checksum: nil)
}

public init<S: StringProtocol>(parsing description: S) throws {
inner = try ContractId(parsing: description)
}

public var description: String {
String(describing: inner)
}

public static func fromBytes(_ bytes: Data) throws -> DelegateContractId {
try Self(protobufBytes: bytes)
}

public func toBytes() -> Data {
inner.toBytes()
}
}

extension DelegateContractId: TryProtobufCodable {
internal typealias Protobuf = ContractId.Protobuf

internal init(protobuf proto: Protobuf) throws {
self.init(try ContractId(protobuf: proto))
}

internal func toProtobuf() -> Protobuf {
inner.toProtobuf()
}
}
11 changes: 8 additions & 3 deletions Sources/Hedera/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import Foundation
import HederaProtobufs

/// Any method that can be used to authorize an operation on Hedera.
public enum Key: Equatable {
case single(PublicKey)
case contractId(ContractId)
case delegatableContractId(ContractId)

/// A delegatable contract ID.
case delegateContractId(DelegateContractId)

case keyList(KeyList)

/// Convert this key to protobuf encoded bytes.
public func toBytes() -> Data {
toProtobufBytes()
}
Expand Down Expand Up @@ -56,7 +61,7 @@ extension Key: TryProtobufCodable {
case .ecdsaSecp256K1(let ecdsaBytes):
self = .single(try .fromBytesEcdsa(ecdsaBytes))
case .delegatableContractID(let contractId):
self = .delegatableContractId(try .fromProtobuf(contractId))
self = .delegateContractId(try .fromProtobuf(contractId))
}
}

Expand All @@ -71,7 +76,7 @@ extension Key: TryProtobufCodable {
key = single.isEd25519() ? .ed25519(bytes) : .ecdsaSecp256K1(bytes)
case .contractId(let contractId):
key = .contractID(contractId.toProtobuf())
case .delegatableContractId(let delegatableContractId):
case .delegateContractId(let delegatableContractId):
key = .delegatableContractID(delegatableContractId.toProtobuf())
case .keyList(let keyList):
key = keyList.toProtobufKey()
Expand Down