Skip to content

Commit

Permalink
Add account identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Sep 28, 2021
1 parent f19a131 commit 4ff6262
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 53 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/src.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
on:
push:
paths:
- 'src/**'
jobs:
src:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: aviate-labs/[email protected]
with:
dfx-version: 0.8.0
install-moc: true
vessel-version: 0.6.2
- run: for i in src/*.mo ; do moc $(vessel sources) --check $i ; done
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
on: push
on:
push:
paths:
- 'test/**'
jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -8,4 +11,7 @@ jobs:
with:
dfx-version: 0.8.0
install-moc: true
- run: for i in test/*.mo ; do moc --package base $(dfx cache show)/base -r $i ; done
vessel-version: 0.6.2
- run: |
cd test
for i in *.mo ; do moc $(vessel sources) -r $i ; done
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vessel/
17 changes: 17 additions & 0 deletions package-set.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let upstream = https://github.com/dfinity/vessel-package-set/releases/download/mo-0.6.7-20210818/package-set.dhall sha256:c4bd3b9ffaf6b48d21841545306d9f69b57e79ce3b1ac5e1f63b068ca4f89957
let Package = { name : Text, version : Text, repo : Text, dependencies : List Text }

let additions = [
{ name = "sha"
, repo = "https://github.com/aviate-labs/sha.mo"
, version = "v0.1.1"
, dependencies = ["base", "encoding"]
},
{ name = "encoding"
, repo = "https://github.com/aviate-labs/encoding.mo"
, version = "v0.2.1"
, dependencies = ["base"]
}
] : List Package

in upstream # additions
33 changes: 33 additions & 0 deletions src/AccountIdentifier.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Array "mo:base/Array";
import Binary "mo:encoding/Binary";
import Blob "mo:base/Blob";
import Hex "mo:encoding/Hex";
import Principal "mo:base/Principal";
import SHA256 "mo:sha/SHA256";

import CRC32 "CRC32";

module {
public type AccountIdentifier = Text;
public type SubAccount = [Nat8];

private let prefix : [Nat8] = [10, 97, 99, 99, 111, 117, 110, 116, 45, 105, 100];

public func fromPrincipal(p : Principal, subAccount : ?SubAccount) : AccountIdentifier {
fromBlob(Principal.toBlob(p), subAccount);
};

public func fromBlob(data : Blob, subAccount : ?SubAccount) : AccountIdentifier {
fromArray(Blob.toArray(data), subAccount);
};

public func fromArray(data : [Nat8], subAccount : ?SubAccount) : AccountIdentifier {
let account : [Nat8] = switch (subAccount) {
case (null) { Array.freeze(Array.init<Nat8>(32, 0)); };
case (?sa) { sa; };
};
let hash = SHA256.sum224(Array.flatten<Nat8>([prefix, data, account]));
let crc = CRC32.checksum(hash);
Hex.encode(Array.append<Nat8>(Binary.BigEndian.fromNat32(crc), hash));
};
};
332 changes: 284 additions & 48 deletions src/CRC32.mo

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions test/AccountIdentifier.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Debug "mo:base/Debug";
import Principal "mo:base/Principal";

import AccountIdentifier "../src/AccountIdentifier";

let p = Principal.fromText("g42pg-k3n7u-4sals-ufza4-34yrp-mmvkt-psecp-7do7x-snvq4-llwrj-2ae");
assert(AccountIdentifier.fromPrincipal(p, null) == "A7218DB708C35689495871C3C6860504503AB2A545630809DD8609130331B0C2");
File renamed without changes.
9 changes: 9 additions & 0 deletions test/CRC32.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Blob "mo:base/Blob";
import Text "mo:base/Text";

import CRC32 "../src/CRC32";

import Debug "mo:base/Debug";

assert(CRC32.checksum([171,205,1]) == 591393286);
assert(CRC32.checksum(Blob.toArray(Text.encodeUtf8("The quick brown fox jumps over the lazy dog"))) == 1095738169);
File renamed without changes.
3 changes: 0 additions & 3 deletions test/crc32.mo

This file was deleted.

4 changes: 4 additions & 0 deletions vessel.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
dependencies = [ "base", "sha", "encoding" ],
compiler = None Text
}

0 comments on commit 4ff6262

Please sign in to comment.