|
| 1 | +module iota_account2::iota_account; |
| 2 | + |
| 3 | +use iota::account::{Self, AuthenticatorInfoV1}; |
| 4 | +use iota::auth_context::AuthContext; |
| 5 | +use iota::dynamic_field; |
| 6 | +use iota::ecdsa_k1; |
| 7 | +use iota::ecdsa_r1; |
| 8 | +use iota::ed25519; |
| 9 | + |
| 10 | +/// A dynamic field name for the account owner public key. |
| 11 | +const IOTACCOUNT_OWNER_PUBKEY: vector<u8> = b"IOTACCOUNT_OWNER_PUBKEY"; |
| 12 | + |
| 13 | +/// This struct represents an IOTA account on-chain. |
| 14 | +/// It holds all the related data as dynamic fields to simplify updates and migrations. |
| 15 | +public struct IOTAccount has key { |
| 16 | + id: UID, |
| 17 | +} |
| 18 | + |
| 19 | +// --------------------------------------- Creation --------------------------------------- |
| 20 | + |
| 21 | +/// Creates a new `IOTAccount` as a shared object with the given authenticator. |
| 22 | +/// |
| 23 | +/// `authenticator` is expect to have the following signature: |
| 24 | +/// |
| 25 | +/// public fun authenticate(self: &IOTAccount, signature: vector<u8>, _: &AuthContext, _: &TxContext) { ... } |
| 26 | +/// |
| 27 | +/// And it is expected to verify the `signature` against the public key stored in the account. |
| 28 | +/// |
| 29 | +/// There are several ready-made authenticators available in this module: |
| 30 | +/// - `authenticate_ed25519` |
| 31 | +/// - `authenticate_secp256k1` |
| 32 | +/// - `authenticate_secp256r1` |
| 33 | +public fun create(pubkey: vector<u8>, authenticator: AuthenticatorInfoV1, ctx: &mut TxContext) { |
| 34 | + // Create an account object. |
| 35 | + let mut account = IOTAccount { id: object::new(ctx) }; |
| 36 | + |
| 37 | + let account_id = &mut account.id; |
| 38 | + |
| 39 | + // Add the account owner public key as a dynamic field. |
| 40 | + dynamic_field::add(account_id, IOTACCOUNT_OWNER_PUBKEY, pubkey); |
| 41 | + |
| 42 | + // Add the authenticator info as a dynamic field. |
| 43 | + dynamic_field::add(account_id, account::authenticator_df_name(), authenticator); |
| 44 | + |
| 45 | + // Turn the account object into a mutable shared object. |
| 46 | + iota::transfer::share_object(account); |
| 47 | +} |
| 48 | + |
| 49 | +// --------------------------------------- Field Operations --------------------------------------- |
| 50 | + |
| 51 | +/// Adds a new dynamic field to the account. |
| 52 | +public fun add_field<Name: copy + drop + store, Value: store>( |
| 53 | + self: &mut IOTAccount, |
| 54 | + name: Name, |
| 55 | + value: Value, |
| 56 | + ctx: &TxContext, |
| 57 | +) { |
| 58 | + // Check that the sender of this TX is the account. |
| 59 | + assert!(self.id.uid_to_address() == ctx.sender()); |
| 60 | + |
| 61 | + // Add a new field. |
| 62 | + dynamic_field::add(&mut self.id, name, value); |
| 63 | +} |
| 64 | + |
| 65 | +/// Removes a dynamic field from the account. |
| 66 | +public fun remove_field<Name: copy + drop + store, Value: store>( |
| 67 | + self: &mut IOTAccount, |
| 68 | + name: Name, |
| 69 | + ctx: &TxContext, |
| 70 | +): Value { |
| 71 | + // Check that the sender of this TX is the account. |
| 72 | + assert!(self.id.uid_to_address() == ctx.sender()); |
| 73 | + |
| 74 | + // Remove a new field. |
| 75 | + dynamic_field::remove(&mut self.id, name) |
| 76 | +} |
| 77 | + |
| 78 | +public fun borrow_field<Name: copy + drop + store, Value: store>( |
| 79 | + self: &IOTAccount, |
| 80 | + name: Name, |
| 81 | + ctx: &TxContext, |
| 82 | +): &Value { |
| 83 | + // Check that the sender of this TX is the account. |
| 84 | + assert!(self.id.uid_to_address() == ctx.sender()); |
| 85 | + |
| 86 | + dynamic_field::borrow(&self.id, name) |
| 87 | +} |
| 88 | + |
| 89 | +public fun borrow_field_mut<Name: copy + drop + store, Value: store>( |
| 90 | + self: &mut IOTAccount, |
| 91 | + name: Name, |
| 92 | + ctx: &TxContext, |
| 93 | +): &mut Value { |
| 94 | + // Check that the sender of this TX is the account. |
| 95 | + assert!(self.id.uid_to_address() == ctx.sender()); |
| 96 | + |
| 97 | + dynamic_field::borrow_mut(&mut self.id, name) |
| 98 | +} |
| 99 | + |
| 100 | +// --------------------------------------- Authentication --------------------------------------- |
| 101 | + |
| 102 | +/// Ed25519 signature authenticator. |
| 103 | +public fun authenticate_ed25519( |
| 104 | + self: &IOTAccount, |
| 105 | + signature: vector<u8>, |
| 106 | + _: &AuthContext, |
| 107 | + ctx: &TxContext, |
| 108 | +) { |
| 109 | + let pubkey: &vector<u8> = borrow_field(self, IOTACCOUNT_OWNER_PUBKEY, ctx); |
| 110 | + assert!(ed25519::ed25519_verify(&signature, pubkey, ctx.digest())); |
| 111 | +} |
| 112 | + |
| 113 | +/// Secp256k1 signature authenticator. |
| 114 | +public fun authenticate_secp256k1( |
| 115 | + self: &IOTAccount, |
| 116 | + signature: vector<u8>, |
| 117 | + _: &AuthContext, |
| 118 | + ctx: &TxContext, |
| 119 | +) { |
| 120 | + let pubkey: &vector<u8> = borrow_field(self, IOTACCOUNT_OWNER_PUBKEY, ctx); |
| 121 | + assert!(ecdsa_k1::secp256k1_verify(&signature, pubkey, ctx.digest(), 0)); |
| 122 | +} |
| 123 | + |
| 124 | +/// Secp256r1 signature authenticator. |
| 125 | +public fun authenticate_secp256r1( |
| 126 | + self: &IOTAccount, |
| 127 | + signature: vector<u8>, |
| 128 | + _: &AuthContext, |
| 129 | + ctx: &TxContext, |
| 130 | +) { |
| 131 | + let pubkey: &vector<u8> = borrow_field(self, IOTACCOUNT_OWNER_PUBKEY, ctx); |
| 132 | + assert!(ecdsa_r1::secp256r1_verify(&signature, pubkey, ctx.digest(), 0)); |
| 133 | +} |
0 commit comments