Sign data
Sign hashes or any data with wallet's private key.
Signs data with a wallet private key. The card receives a transaction hash and signs it. The resulting signature is relayed to the blockchain along with the transaction. It proves that the user is authorized to dispose of assets stored on the address associated with the walet’s public key.
For the simplicity we created two methods if you want to sign one or several hashes. Method for one hash will return response with one signature, for many – many.

Request parameters

hash[es] <Sring> or array of <String> One or several hashes of the data to be signed by the card.
walletPublicKey <Data> The wallet's public key. Since a Tangem card can contain more than one wallet, you need to specify the wallet's public key that you want to use to sign the data.
cardId <String> CID, Unique Tangem card ID number.
hdPath <DerivationPath> [optional] Derivation path of the wallet. COS v. 4.28 and higher.
initialMessage <Message> [optional] This message will be shown in the NFC session dialog. Default: "Tap the card to your phone exactly as it shown above".

Response

This command will return the SignHashResponse or SignHashesResponse (click to see more) object in response.

Example (sign one hash)

Swift
Kotlin
Java
JS (React Native)
JS (Cordova)
Dart
JSON-RPC
1
let hash = Data(repeating: 0x1, count: 32) // Test data
2
let walletPublicKey = card.wallets[0].publicKey //Sign with the first wallet
3
let cardId = card.cardId
4
5
tangemSdk.sign(hash: hash,
6
walletPublicKey: walletPublicKey,
7
cardId: cardId) { result in
8
switch result {
9
case .success(let response):
10
let signature = response.signature
11
print(signature)
12
case .failure(let error):
13
print(error)
14
}
15
}
Copied!
1
val hash = Random.nextBytes(ByteArray(32)) // Random data
2
val walletPublicKey = card.wallets.first().publicKey //Sign with the first wallet
3
val cardId = card.cardId
4
5
tangemSdk.sign(
6
hash: hash,
7
walletPublicKey = walletPublicKey,
8
cardId = cardId
9
) { result ->
10
when (result) {
11
is CompletionResult.Success -> {
12
runOnUiThread {
13
Log.d(TAG, result.signature)
14
}
15
}
16
is CompletionResult.Failure -> {
17
runOnUiThread {
18
Log.d(TAG, result.error)
19
}
20
}
21
}
22
}
Copied!
1
byte[] hash = new byte[32];
2
new Random().nextBytes(hash);
3
4
byte[] walletPublicKey = card.getWallets().get(0).getPublicKey();
5
String cardId = card.getCardId();
6
7
tangemSdk.sign(hash, walletPublicKey, cardId, null, null, result -> {
8
if (result instanceof CompletionResult.Success) {
9
SignHashResponse response = ((CompletionResult.Success<SignHashResponse>) result).getData();
10
Log.d(TAG, Arrays.toString(response.getSignature()));
11
} else if (result instanceof CompletionResult.Failure) {
12
TangemError error = ((CompletionResult.Failure<SignHashResponse>) result).getError();
13
Log.d(TAG, error.getCustomMessage());
14
}
15
return Unit.INSTANCE;
16
});
Copied!
1
Copied!
1
Copied!
1
Copied!
1
// Request
2
{
3
"jsonrpc":"2.0",
4
"method":"sign_hash",
5
"params":{
6
"walletPublicKey":"5130869115a2ff91959774c99d4dc2873f0c41af3e0bb23d027ab16d39de1348",
7
"hash":"f1642bb080e1f320924dde7238c1c5f8"
8
},
9
"id":1
10
}
11
12
// Response
13
{
14
"jsonrpc":"2.0",
15
"result":{
16
"cardId":"c000111122223333",
17
"signature":"eb7411c2b7d871c06dad51e58e44746583ad134f4e214e4899f2fc84802232a1",
18
"totalSignedHashes":2
19
},
20
"id":1
21
}
Copied!

Example (sign many hashes at once)

Swift
Kotlin
Java
JS (React Native)
JS (Cordova)
Dart
JSON-RPC
1
let hash1 = Data(repeating: 0x0, count: 32)
2
let hash2 = Data(repeating: 0x1, count: 32)
3
let walletPublicKey = card.wallets[0].publicKey //Sign with the first wallet
4
let cardId = card.cardId
5
6
tangemSdk.sign(hashes: [hash1, hash2],
7
walletPublicKey: walletPublicKey,
8
cardId: cardId) { result in
9
switch result {
10
case .success(let response):
11
let signature1 = response.signatures[0]
12
let signature2 = response.signatures[1]
13
print(signature1)
14
print(signature2)
15
case .failure(let error):
16
print(error)
17
}
18
}
Copied!
1
val hash1 = Random.nextBytes(ByteArray(32))
2
val hash2 = Random.nextBytes(ByteArray(32))
3
val hashes = arrayOf(hash1, hash2)
4
val walletPublicKey = card.wallets.first().publicKey //Sign with the first wallet
5
val cardId = card.cardId
6
7
tangemSdk.sign(
8
hashes: hashes,
9
walletPublicKey = walletPublicKey,
10
cardId = cardId
11
) { result ->
12
when (result) {
13
is CompletionResult.Success -> {
14
runOnUiThread {
15
Log.d(TAG, result.signature)
16
}
17
}
18
is CompletionResult.Failure -> {
19
runOnUiThread {
20
Log.d(TAG, result.error)
21
}
22
}
23
}
24
}
Copied!
1
byte[] hash1 = new byte[32];
2
byte[] hash2 = new byte[32];
3
new Random().nextBytes(hash1);
4
new Random().nextBytes(hash2);
5
byte[][] hashes = new byte[2][32];
6
hashes[0] = hash1;
7
hashes[1] = hash2;
8
9
byte[] walletPublicKey = card.getWallets().get(0).getPublicKey();
10
String cardId = card.getCardId();
11
12
tangemSdk.sign(hashes, walletPublicKey, cardId, null, null, result -> {
13
if (result instanceof CompletionResult.Success) {
14
SignResponse response = ((CompletionResult.Success<SignResponse>) result).getData();
15
Log.d(TAG, Arrays.toString(response.getSignatures().get(0)));
16
Log.d(TAG, Arrays.toString(response.getSignatures().get(1)));
17
} else if (result instanceof CompletionResult.Failure) {
18
TangemError error = ((CompletionResult.Failure<SignResponse>) result).getError();
19
Log.d(TAG, error.getCustomMessage());
20
}
21
return Unit.INSTANCE;
22
});
Copied!
1
const hashes = [
2
'44617461207573656420666f722068617368696e67',
3
'4461746120666f7220757365642068617368696e67',
4
];
5
const walletPublicKey = card.wallets[0].publicKey;
6
const hdPath = undefined
7
const cardId = card.id;
8
9
TangemSdk.sign(hashes, walletPublicKey, hdPath, cardId)
10
.then((result) => console.log(result))
11
.catch((err) => console.log(err));
Copied!
1
var hashes = [
2
'44617461207573656420666f722068617368696e67',
3
'4461746120666f7220757365642068617368696e67',
4
];
5
var hdPath = undefined;
6
var cardId = card.cardId;
7
var initialMessage = {"body": "body", "header": "header"};
8
var callback = function (response, error) {
9
if (error) {
10
alert({ "error": error });
11
} else {
12
alert({"response": response});
13
}
14
};
15
16
TangemSdk.sign(hashes, walletPublicKey, hdPath, cardId, initialMessage, callback);
Copied!
1
Copied!
1
// Request
2
{
3
"jsonrpc":"2.0",
4
"result":{
5
"cardId":"c000111122223333",
6
"signature":"eb7411c2b7d871c06dad51e58e44746583ad134f4e214e4899f2fc84802232a1",
7
"totalSignedHashes":2
8
},
9
"id":1
10
}
11
12
// Response
13
{
14
"jsonrpc":"2.0",
15
"result":{
16
"cardId":"c000111122223333",
17
"signatures":[
18
"eb7411c2b7d871c06dad51e58e44746583ad134f4e214e4899f2fc84802232a1",
19
"33443bd93f350b62a90a0c23d30c6d4e9bb164606e809ccace60cf0e2591e58c"
20
],
21
"totalSignedHashes":2
22
},
23
"id":1
24
}
Copied!