--- tags: iota --- # IOTA MAM 筆記 ## MAM架構 MAM 使用 Merkle tree based signature scheme 作為其簽章的基礎,並且採用其所產生的 Markle tree root 作為MAM的廣播地址,也同時是驗證 MAM 所使用的 publicKey (可以想像成平常交易使用的地址),此外,注意此處使用的「廣播」二字,即表達 MAM 的通訊是單向而非雙向的,並且監聽頻道的使用者無法向前追溯訊息。加密則是採用了one-time symmetric key encryption。 在這樣的架構之下,MAM 提供 Channel spliting 和多種廣播的方式: - Public - Private - Restricted 根據官方的說法,MAM中訊息之間的結構也可以理解成父資料夾與子資料夾之間的關係,皆可以隱藏或是加密(對應到 Private 和 Restricted) 以下皆是針對 Public 模式說明 - `subscribed`: [] - `channel`: - `side_key`: `String` Encryption key - `mode`: `String` (public private restricted) - `next_root`: `String` - `security`: `Int` 1,2,3 - `start`: `Int` - `count`: 1, - `next_count`: 1, - `index`: 0 - `seed`: `String` ``` 構造一個 MAM 訊息需要以下部份: - `message`: `String` Tryte-encoded plain text - `merkleTree`: `Object` Merkle tree - `index`: `Int` Index of the merkle tree leaf key - `nextRoot`: `String` Root of the next merkle tree - `channelKey`: `String` Channel key (used for data encryption) 建立後程式會回傳下一個訊息使用的金鑰`nextKey` , 其實就是 hash(`channelKey`+ 1 + salt ), ``` 在一個 MAM 訊息中,有以下幾個部份,並且在`tag`部份帶有每次加密時皆會使用到的 salt: - `root`: `String` Root of the merkle tree - `nextRoot`: `String` Root of the next merkle tree - `nextKey`: `String` Tryte-encoded key for the next message in this chain - `message`: `String` Tryte-encoded message `root` 和 `nextRoot` 指的是 Markle tree root ![](https://i.imgur.com/Ipez2kv.png) 以上 A, B, C, D, E, F, G, H 皆是由同一個 seed 所產生的 key (注意,此非加密時使用的 key ) ## MAM 特色 IOTA在Medium上的MAM文章中提到一句 "MAM uses a [Merkle tree based signature scheme](https://www.imperialviolet.org/2013/07/18/hashsig.html) to sign the cipher digest of an encrypted message." 但實際上IOTA上的實做與該文章不同, 而是在結合了加密貨幣的address這個概念後,更進一步的利用Merkel tree的特點,以merkle tree root作為訊息傳送的地址大量簡化了流程,驗證時即是利用獲得的 key 以及其餘 merkle tree 的節點 hash 來重組出root,再檢查 root == message address 即為通過驗證。 Merkle tree的葉節點數量表示可以在該 root(address) 上發送幾次訊息,下一次廣播訊息時就直接使用其他葉節點作為 key,也就是說能證明自己擁有 merkle tree 就是證明自己是該 channel 的"擁有者"。 `public mode`: publish to root, use root to encrypt `private mode`: publish to hash(root), use root to encrypt `restricted mode`: publish to hash(root+side_key), use root+side_key to encrypt ## MAM 流程 ![](https://i.imgur.com/VzQt7C0.png) ![](https://i.imgur.com/RKzv024.png) ## 問題 * 與不同對象的通訊需要使用多個seed產生不同的markle tree嗎? -> 需要 * MAM是將訊息丟向某個廣播地址,如果帶 token 應該是拿不回來? -> 對 * MAM的地址由seed產生,所以是否跟平常使用之地址無異呢? -> 是的 ## 參考資料 https://github.com/iotaledger/mam.client.js/blob/master/Overview.md https://blog.iota.org/introducing-masked-authenticated-messaging-e55c1822d50e