Hedera
Search…
Create a threshold key
Create a key structure that requires the defined threshold value to sign. You can use either the public key or the private key to create the key structure. If the threshold requirement is not met when signing transactions, the network will return an "INVALID_SIGNATURE" error.
V2
V1
Method
Type
Description
KeyList.withThreshold(<thresholdValue>)
int
The number of keys required to sign transactions to modify the account i.e. transfers, update, etc
Java
1
//Generate 3 keys
2
PrivateKey key1 = PrivateKey.generate();.
3
PublicKey publicKey1 = key1.getPublicKey();
4
5
PrivateKey key2 = PrivateKey.generate();
6
PublicKey publicKey2 = key2.getPublicKey();
7
8
PrivateKey key3 = PrivateKey.generate();
9
PublicKey publicKey3 = key3.getPublicKey();
10
11
PrivateKey[] keys = new PrivateKey[3]; //You can also use the 3 public keys here
12
13
keys[0] = key1;
14
keys[1] = key2;
15
keys[2] = key3;
16
17
18
//A key structure that requires one of the 3 keys to sign
19
KeyList thresholdKey = KeyList.withThreshold(1);
20
21
//Add the three keys to the thresholdKey
22
Collections.addAll(thresholdKey, keys);
23
24
System.out.println("The 1/3 threshold key structure" +thresholdKey);
25
26
//v2.0.0
Copied!
JavaScript
1
// Generate our key lists
2
const privateKeyList = [];
3
const publicKeyList = [];
4
for (let i = 0; i < 4; i += 1) {
5
// eslint-disable-next-line no-await-in-loop
6
const privateKey = await PrivateKey.generate();
7
const publicKey = privateKey.publicKey;
8
privateKeyList.push(privateKey);
9
publicKeyList.push(publicKey);
10
console.log(`${i}: pub key:${publicKey}`);
11
console.log(`${i}: priv key:${privateKey}`);
12
}
13
14
// Create our threshold key
15
const thresholdKey = new KeyList(publicKeyList,1);
16
17
console.log("The 1/3 threshold key structure" +thresholdKey);
18
19
//2.0.2
Copied!
Go
1
//Generate 3 keys
2
key1, err := hedera.GeneratePrivateKey()
3
4
if err != nil {
5
panic(err)
6
}
7
8
publicKey1 := key1.PublicKey()
9
10
key2, err := hedera.GeneratePrivateKey()
11
12
if err != nil {
13
panic(err)
14
}
15
16
publicKey2:= key2.PublicKey()
17
18
key3, err := hedera.GeneratePrivateKey()
19
20
if err != nil {
21
panic(err)
22
}
23
24
publicKey3 := key3.PublicKey()
25
26
//Create a key list where all 3 keys are required to sign
27
28
keys := make([]hedera.PublicKey, 3)
29
30
keys[0] = publicKey1
31
keys[1] = publicKey2
32
keys[2] = publicKey3
33
34
//A key structure that requires one of the 3 keys to sign
35
thresholdKey := hedera.KeyListWithThreshold(1).
36
AddAllPublicKeys(keys)
37
38
fmt.Printf("The 1/3 threshold key structure %v\n", thresholdKey)
39
40
//v2.0.0
Copied!
Sample Output:
KeyList{threshold=1, keys=[302e020100300506032b657004220420984bd6b4e0cac783654f30c8797655953c6ab432e78bc09a34fbda594c6395ed, 302e020100300506032b657004220420a4a7bd506f33868416d53eff55b3e8a254e17accf6cb37f44975792ededac120, 302e020100300506032b657004220420f8a6f2ba3174391e619a87506fb0b86c6e481809563a797f4f84715d1a471695] }
Constructor
Type
Description
new ThresholdKey(<threshold>)
int
Initializes a ThresholdKey object
1
new ThresholdKey(<threshold>)
Copied!
Method
Type
Description
add(<key>)
Ed25519PublicKey
Add one public key to the key list
addAll(<keys>)
Ed25519PublicKey
Add all keys to the key list
Java
1
//Generate 3 keys
2
Ed25519PrivateKey key1 = Ed25519PrivateKey.generate();
3
Ed25519PublicKey publicKey1 = key1.publicKey;
4
5
Ed25519PrivateKey key2 = Ed25519PrivateKey.generate();
6
Ed25519PublicKey publicKey2 = key2.publicKey;
7
8
Ed25519PrivateKey key3 = Ed25519PrivateKey.generate();
9
Ed25519PublicKey publicKey3 = key3.publicKey;
10
11
// require 2 of the 3 keys we generated to sign on anything modifying this account
12
ThresholdKey thresholdKeys = new ThresholdKey(2).add(publicKey1).add(publicKey2).add(publicKey3);
13
14
//v1.2.2
Copied!
JavaScript
1
//Generate 3 keys
2
const key1 = await Ed25519PrivateKey.generate();
3
const publicKey1 = key1.publicKey;
4
5
const key2 = await Ed25519PrivateKey.generate();
6
const publicKey2 = key2.publicKey;
7
8
const key3 = await Ed25519PrivateKey.generate();
9
const publicKey3 = key3.publicKey;
10
11
//Create a threshold key of 2/3
12
const thresholdKeys = new ThresholdKey(2).add(publicKey1).add(publicKey2).add(publicKey3);
13
14
//v1.4.2
Copied!
Last modified 6d ago
Copy link