-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HIP-540): change or remove existing keys from tokens
- Loading branch information
Showing
52 changed files
with
3,645 additions
and
231 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,166 @@ | ||
/* | ||
* | ||
* Hedera Swift SDK | ||
* | ||
* Copyright (C) 2022 - 2024 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 Hedera | ||
import SwiftDotenv | ||
|
||
@main | ||
internal enum Program { | ||
internal static func main() async throws { | ||
let env = try Dotenv.load() | ||
let client = try Client.forName(env.networkName) | ||
|
||
// Defaults the operator account ID and key such that all generated transactions will be paid for | ||
// by this account and be signed by this key | ||
client.setOperator(env.operatorAccountId, env.operatorKey) | ||
|
||
// Generate a higher-privileged key. | ||
let adminKey = PrivateKey.generateEd25519() | ||
|
||
// Generate the lower-privileged keys that will be modified. | ||
// Note: Lower-privileged keys are Wipe, Supply, and updated Supply key.. | ||
let supplyKey = PrivateKey.generateEd25519() | ||
let wipeKey = PrivateKey.generateEd25519() | ||
let newSupplyKey = PrivateKey.generateEd25519() | ||
|
||
let unusableKey = try PublicKey.fromStringEd25519( | ||
"0x0000000000000000000000000000000000000000000000000000000000000000") | ||
|
||
// Create an NFT token with admin, wipe, and supply key. | ||
let tokenId = try await TokenCreateTransaction() | ||
.name("Example NFT") | ||
.symbol("ENFT") | ||
.tokenType(TokenType.nonFungibleUnique) | ||
.treasuryAccountId(env.operatorAccountId) | ||
.adminKey(.single(adminKey.publicKey)) | ||
.wipeKey(.single(wipeKey.publicKey)) | ||
.supplyKey(.single(supplyKey.publicKey)) | ||
.expirationTime(Timestamp.now + .minutes(5)) | ||
.freezeWith(client) | ||
.sign(adminKey) | ||
.execute(client) | ||
.getReceipt(client) | ||
.tokenId! | ||
|
||
let tokenInfo = try await TokenInfoQuery() | ||
.tokenId(tokenId) | ||
.execute(client) | ||
|
||
print("Admin Key: \(tokenInfo.adminKey!)") | ||
print("Wipe Key: \(tokenInfo.wipeKey!)") | ||
print("Supply Key: \(tokenInfo.supplyKey!)") | ||
|
||
print("------------------------------------") | ||
print("Removing Wipe Key...") | ||
|
||
// Remove the wipe key with empty Keylist, signing with the admin key. | ||
let _ = try await TokenUpdateTransaction() | ||
.tokenId(tokenId) | ||
.wipeKey(.keyList([])) | ||
.keyVerificationMode(TokenKeyValidation.fullValidation) | ||
.freezeWith(client) | ||
.sign(adminKey) | ||
.execute(client) | ||
.getReceipt(client) | ||
|
||
let tokenInfoAfterWipeKeyUpdate = try await TokenInfoQuery() | ||
.tokenId(tokenId) | ||
.execute(client) | ||
|
||
print("Wipe Key (after removal): \(String(describing: tokenInfoAfterWipeKeyUpdate.wipeKey))") | ||
print("------------------------------------") | ||
print("Removing Admin Key...") | ||
|
||
// Remove the admin key with empty Keylist, signing with the admin key. | ||
let _ = try await TokenUpdateTransaction() | ||
.tokenId(tokenId) | ||
.adminKey(.keyList([])) | ||
.keyVerificationMode(TokenKeyValidation.noValidation) | ||
.freezeWith(client) | ||
.sign(adminKey) | ||
.execute(client) | ||
.getReceipt(client) | ||
|
||
let tokenInfoAfterAdminKeyUpdate = try await TokenInfoQuery() | ||
.tokenId(tokenId) | ||
.execute(client) | ||
|
||
print("Admin Key (after removal): \(String(describing:tokenInfoAfterAdminKeyUpdate.adminKey))") | ||
|
||
print("------------------------------------") | ||
print("Update Supply Key...") | ||
|
||
// Update the supply key with a new key, signing with the old supply key and the new supply key. | ||
let _ = try await TokenUpdateTransaction() | ||
.tokenId(tokenId) | ||
.supplyKey(.single(newSupplyKey.publicKey)) | ||
.keyVerificationMode(TokenKeyValidation.fullValidation) | ||
.freezeWith(client) | ||
.sign(supplyKey) | ||
.sign(newSupplyKey) | ||
.execute(client) | ||
.getReceipt(client) | ||
|
||
let tokenInfoAfterSupplyKeyUpdate = try await TokenInfoQuery() | ||
.tokenId(tokenId) | ||
.execute(client) | ||
|
||
print("Supply Key (after update): \(String(describing: tokenInfoAfterSupplyKeyUpdate.supplyKey))") | ||
|
||
print("------------------------------------") | ||
print("Removing Supply Key...") | ||
|
||
// Remove the supply key with unusable key, signing with the new supply key. | ||
let _ = try await TokenUpdateTransaction() | ||
.tokenId(tokenId) | ||
.supplyKey(.single(unusableKey)) | ||
.keyVerificationMode(TokenKeyValidation.noValidation) | ||
.freezeWith(client) | ||
.sign(newSupplyKey) | ||
.execute(client) | ||
.getReceipt(client) | ||
|
||
let tokenInfoAfterSupplyKeyRemoval = try await TokenInfoQuery() | ||
.tokenId(tokenId) | ||
.execute(client) | ||
|
||
print("Supply Key (after removal): \(String(describing: tokenInfoAfterSupplyKeyRemoval.supplyKey))") | ||
} | ||
} | ||
|
||
extension Environment { | ||
/// Account ID for the operator to use in this example. | ||
internal var operatorAccountId: AccountId { | ||
AccountId(self["OPERATOR_ID"]!.stringValue)! | ||
} | ||
|
||
/// Private key for the operator to use in this example. | ||
internal var operatorKey: PrivateKey { | ||
PrivateKey(self["OPERATOR_KEY"]!.stringValue)! | ||
} | ||
|
||
/// The name of the hedera network this example should be ran against. | ||
/// | ||
/// Testnet by default. | ||
internal var networkName: String { | ||
self["HEDERA_NETWORK"]?.stringValue ?? "testnet" | ||
} | ||
} |
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
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
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,54 @@ | ||
/* | ||
* | ||
* Hedera Swift SDK | ||
* | ||
* Copyright (C) 2022 - 2024 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 | ||
|
||
public enum TokenKeyValidation { | ||
/// Currently the default behaviour. It will perform all token key validations. | ||
case fullValidation | ||
/// Perform no validations at all for all passed token keys. | ||
case noValidation | ||
/// The passed token key is not recognized. | ||
case unrecognized(Int) | ||
} | ||
|
||
extension TokenKeyValidation: TryFromProtobuf { | ||
internal typealias Protobuf = Proto_TokenKeyValidation | ||
|
||
internal init(protobuf proto: Protobuf) throws { | ||
switch proto { | ||
case .fullValidation: self = .fullValidation | ||
case .noValidation: self = .noValidation | ||
case .UNRECOGNIZED(let value): self = .unrecognized(value) | ||
} | ||
} | ||
|
||
func toProtobuf() -> Protobuf { | ||
switch self { | ||
case .fullValidation: | ||
return .fullValidation | ||
case .noValidation: | ||
return .noValidation | ||
case .unrecognized(let value): | ||
return .UNRECOGNIZED(value) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.