Skip to content

Commit

Permalink
public key
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 21, 2024
1 parent 323551a commit dc00bd8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/1.concepts/protocol/access-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ NEAR accounts present the **unique** feature of being able to hold multiple [Acc
1. `Full-Access Keys`: Have full control over the account, and should **never be shared**
2. `Function-Call Keys`: Can sign calls to specific contract, and are **meant to be shared**


Currently supported curve types are `ed25519` (default curve type used for public key cryptography in NEAR) and `secp256k1` (supported for compatibility with other blockchains).

---

## Full-Access Keys {#full-access-keys}
Expand Down
36 changes: 36 additions & 0 deletions docs/2.build/2.smart-contracts/anatomy/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,39 @@ To simplify development, the SDK provides the `U64` and `U128` types which are a
start="2" end="84" />

</ExplainCode>


## PublicKey

Public key is in a binary format with base58 string serialization with human-readable curve.
The key types currently supported are `secp256k1` and `ed25519`.

Example:
```rust
use near_sdk::{PublicKey, CurveType};

pub fn stake_tokens(
&mut self,
amount: U128,
) -> Promise {
// Compressed ed25519 key
let ed: PublicKey = "ed25519:6E8sCci9badyRkXb3JoRpBj5p8C6Tw41ELDZoiihKEtp".parse()
.unwrap();

// Uncompressed secp256k1 key
let secp256k1: PublicKey =
"secp256k1:qMoRgcoXai4mBPsdbHi1wfyxF9TdbPCF4qSDQTRP3TfescSRoUdSx6nmeQoN3aiwGzwMyGXAb1gUjBTv5AY8DXj"
.parse().unwrap();

// Check the curve type of the public key
match ed.curve_type() {
near_sdk::CurveType::ED25519 => env::log_str("Using ED25519 curve."),
near_sdk::CurveType::SECP256K1 => env::log_str("Using SECP256K1 curve."),
_ => env::panic_str("Unsupported curve type!"),
}

// Use the staking contract to stake the tokens (using NEAR's PromiseAction::Stake)
Promise::new(env::predecessor_account_id().clone())
.stake(amount.0, public_key) // stake the tokens using the public key for verification
}
```

0 comments on commit dc00bd8

Please sign in to comment.