|
| 1 | +import path from "path" |
| 2 | +import { |
| 3 | + emulator, |
| 4 | + init, |
| 5 | + getAccountAddress, |
| 6 | + executeScript, |
| 7 | + createAccount, |
| 8 | + HashAlgorithm, |
| 9 | + SignatureAlgorithm, |
| 10 | +} from "../src" |
| 11 | +import {playgroundImport} from "../src/transformers" |
| 12 | +import * as fcl from "@onflow/fcl" |
| 13 | +import {validateKeyPair} from "./util/validate-key-pair" |
| 14 | +import {permute} from "./util/permute" |
| 15 | +import {isAddress} from "../src" |
| 16 | + |
| 17 | +beforeEach(async () => { |
| 18 | + const basePath = path.resolve(__dirname, "./cadence") |
| 19 | + await init(basePath) |
| 20 | + return emulator.start() |
| 21 | +}) |
| 22 | + |
| 23 | +afterEach(async () => { |
| 24 | + emulator.stop() |
| 25 | +}) |
| 26 | + |
| 27 | +it("createAccount - should work with name and resolves to correct getAccountAddress", async () => { |
| 28 | + const addr1 = await createAccount({ |
| 29 | + name: "Billy", |
| 30 | + }) |
| 31 | + |
| 32 | + const addr2 = await getAccountAddress("Billy") |
| 33 | + |
| 34 | + expect(addr1).toBe(addr2) |
| 35 | +}) |
| 36 | + |
| 37 | +it("createAccount - should work without name and returns address", async () => { |
| 38 | + const Billy = await createAccount({ |
| 39 | + name: "Billy", |
| 40 | + }) |
| 41 | + |
| 42 | + expect(isAddress(Billy)).toBe(true) |
| 43 | +}) |
| 44 | + |
| 45 | +test.each(permute(Object.keys(HashAlgorithm), Object.keys(SignatureAlgorithm)))( |
| 46 | + "createAccount - should work with custom keys - hash algorithm %s - signing algorithm %s", |
| 47 | + async (hashAlgorithm, signatureAlgorithm) => { |
| 48 | + const privateKey = "1234" |
| 49 | + const Billy = await createAccount({ |
| 50 | + name: "Billy", |
| 51 | + keys: [ |
| 52 | + { |
| 53 | + privateKey, |
| 54 | + hashAlgorithm: HashAlgorithm[hashAlgorithm], |
| 55 | + signatureAlgorithm: SignatureAlgorithm[signatureAlgorithm], |
| 56 | + }, |
| 57 | + ], |
| 58 | + }) |
| 59 | + |
| 60 | + expect(isAddress(Billy)).toBe(true) |
| 61 | + |
| 62 | + const keys = await fcl.account(Billy).then(d => d.keys) |
| 63 | + |
| 64 | + expect(keys.length).toBe(1) |
| 65 | + expect(keys[0].hashAlgoString).toBe(hashAlgorithm) |
| 66 | + expect(keys[0].signAlgoString).toBe(signatureAlgorithm) |
| 67 | + expect(keys[0].weight).toBe(1000) |
| 68 | + expect( |
| 69 | + validateKeyPair(keys[0].publicKey, privateKey, signatureAlgorithm) |
| 70 | + ).toBe(true) |
| 71 | + } |
| 72 | +) |
| 73 | + |
| 74 | +test("createAccount - should work with custom keys - defaults to SHA3_256, ECDSA_P256", async () => { |
| 75 | + const privateKey = "1234" |
| 76 | + const Billy = await createAccount({ |
| 77 | + name: "Billy", |
| 78 | + keys: [ |
| 79 | + { |
| 80 | + privateKey, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }) |
| 84 | + |
| 85 | + expect(isAddress(Billy)).toBe(true) |
| 86 | + |
| 87 | + const keys = await fcl.account(Billy).then(d => d.keys) |
| 88 | + |
| 89 | + expect(keys.length).toBe(1) |
| 90 | + expect(keys[0].hashAlgoString).toBe("SHA3_256") |
| 91 | + expect(keys[0].signAlgoString).toBe("ECDSA_P256") |
| 92 | + expect(keys[0].weight).toBe(1000) |
| 93 | + expect( |
| 94 | + validateKeyPair( |
| 95 | + keys[0].publicKey, |
| 96 | + privateKey, |
| 97 | + SignatureAlgorithm.ECDSA_P256 |
| 98 | + ) |
| 99 | + ).toBe(true) |
| 100 | +}) |
| 101 | + |
| 102 | +it("createAccount - should add universal private key to account by default", async () => { |
| 103 | + const Billy = await createAccount({ |
| 104 | + name: "Billy", |
| 105 | + }) |
| 106 | + |
| 107 | + expect(isAddress(Billy)).toBe(true) |
| 108 | +}) |
| 109 | + |
| 110 | +it("getAccountAddress - should return proper playground addresses", async () => { |
| 111 | + const accounts = ["Alice", "Bob", "Charlie", "Dave", "Eve"] |
| 112 | + for (const i in accounts) { |
| 113 | + await getAccountAddress(accounts[i]) |
| 114 | + } |
| 115 | + |
| 116 | + const code = ` |
| 117 | + pub fun main(address:Address):Address{ |
| 118 | + return getAccount(address).address |
| 119 | + } |
| 120 | + ` |
| 121 | + |
| 122 | + const playgroundAddresses = ["0x01", "0x02", "0x03", "0x04", "0x05"] |
| 123 | + for (const i in playgroundAddresses) { |
| 124 | + const [result] = await executeScript({ |
| 125 | + code, |
| 126 | + transformers: [playgroundImport(accounts)], |
| 127 | + args: [playgroundAddresses[i]], |
| 128 | + }) |
| 129 | + const account = await getAccountAddress(accounts[i]) |
| 130 | + expect(result).toBe(account) |
| 131 | + } |
| 132 | +}) |
| 133 | + |
| 134 | +it("getAccountAddress - should create an account if does not exist", async () => { |
| 135 | + const Billy = await getAccountAddress("Billy") |
| 136 | + |
| 137 | + expect(isAddress(Billy)).toBe(true) |
| 138 | +}) |
| 139 | + |
| 140 | +it("getAccountAddress - should resolve an already created account", async () => { |
| 141 | + const Billy1 = await getAccountAddress("Billy") |
| 142 | + const Billy2 = await getAccountAddress("Billy") |
| 143 | + |
| 144 | + expect(isAddress(Billy1)).toBe(true) |
| 145 | + expect(Billy1).toMatch(Billy2) |
| 146 | +}) |
0 commit comments