Skip to content

Commit

Permalink
Merge pull request #9 from algorandfoundation/feat/split-keyreg-schemas
Browse files Browse the repository at this point in the history
Feat/split keyreg schemas
  • Loading branch information
ehanoc authored Oct 18, 2023
2 parents 874b684 + 29ac33d commit 0f4cb16
Show file tree
Hide file tree
Showing 4 changed files with 620 additions and 29 deletions.
51 changes: 28 additions & 23 deletions lib/algorand.transaction.crafter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as msgpack from "algo-msgpack-with-bigint"
import { AlgorandTransactionCrafter } from "./algorand.transaction.crafter"
import { PayTransaction } from "./algorand.transaction.pay"
import { KeyregTransaction } from "./algorand.transaction.keyreg"
import Ajv, {JSONSchemaType} from "ajv"
import Ajv, {type JSONSchemaType} from "ajv"
import path from "path"
import fs from 'fs'

Expand Down Expand Up @@ -60,11 +60,7 @@ describe("Algorand Transaction Crafter", () => {
})

describe("Pay Transactions", () => {
let paySchema: JSONSchemaType<PayTransaction>

beforeAll(async () => {
paySchema = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/pay.transaction.json"), "utf8"))
})
let paySchema: JSONSchemaType<PayTransaction> = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/pay.transaction.json"), "utf8"))

it("(OK) Craft Pay Transaction", async () => {
// from algorand address
Expand Down Expand Up @@ -105,12 +101,8 @@ describe("Algorand Transaction Crafter", () => {
})
})

describe("KeyReg Transactions", () => {
let keyRegSchema: JSONSchemaType<KeyregTransaction>

beforeAll(async () => {
keyRegSchema = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/keyreg.transaction.json"), "utf8"))
})
describe("KeyReg Online Transactions", () => {
let keyRegSchema: JSONSchemaType<KeyregTransaction> = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/keyreg.transaction.online.json"), "utf8"))

it("(OK) Craft Keyreg change-online transaction", async () => {
// from algorand address
Expand Down Expand Up @@ -139,6 +131,11 @@ describe("Algorand Transaction Crafter", () => {

expect(txn).toBeDefined()
expect(txn).toBeInstanceOf(KeyregTransaction)

const ajv = new Ajv()
const validate = ajv.compile(keyRegSchema)
expect(validate(txn)).toBe(true)

expect(txn).toEqual({
snd: algoEncoder.decodeAddress(from),
votekey: new Uint8Array(Buffer.from(voteKey, "base64")),
Expand All @@ -154,13 +151,13 @@ describe("Algorand Transaction Crafter", () => {
fee: 1000,
type: "keyreg",
})

const ajv = new Ajv()
const validate = ajv.compile(keyRegSchema)
expect(validate(txn)).toBe(true)
})

it("(OK) Craft Keyreg change-ofline transaction", async () => {
})

describe("KeyReg Offline Transactions", () => {
let keyRegSchema: JSONSchemaType<KeyregTransaction> = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/keyreg.transaction.offline.json"), "utf8"))

it("(OK) Craft Keyreg change-offline transaction", async () => {
// from algorand address
const from: string = algoEncoder.encodeAddress(Buffer.from(randomBytes(32)))

Expand All @@ -187,8 +184,19 @@ describe("Algorand Transaction Crafter", () => {
fee: 1000,
type: "keyreg",
})

const ajv = new Ajv()
const validate = ajv.compile(keyRegSchema)
expect(validate(txn)).toBe(true)
})
})

describe("KeyReg Non-participation Transactions", () => {
let keyRegSchema: JSONSchemaType<KeyregTransaction>

beforeAll(async () => {
keyRegSchema = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/keyreg.transaction.nonparticipation.json"), "utf8"))
})

it("(OK) Craft Keyreg non-participation transaction", async () => {
// from algorand address
const from: string = algoEncoder.encodeAddress(Buffer.from(randomBytes(32)))
Expand Down Expand Up @@ -218,11 +226,8 @@ describe("Algorand Transaction Crafter", () => {
nonpart: true,
})

// key reg no participation
const keyRegNonParticipationSchema: JSONSchemaType<KeyregTransaction> = JSON.parse(fs.readFileSync(path.resolve(__dirname, "schemas/keyreg.transaction.nonparticipation.json"), "utf8"))

const ajv = new Ajv()
const validate = ajv.compile(keyRegNonParticipationSchema)
const validate = ajv.compile(keyRegSchema)
expect(validate(txn)).toBe(true)
})
})
Expand Down
9 changes: 5 additions & 4 deletions lib/schemas/keyreg.transaction.nonparticipation.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
"type": "string",
"const": "keyreg"
},
"nonpart": {
"type": "boolean",
"const": true
},
"fee": {
"type": "integer",
"minimum": 0
},
"snd": {
"$ref": "http://algo-models.com/schemas/bytes32.json"
},
"nonpart": {
"type": "boolean"
},
"fv": {
"type": "integer",
"minimum": 0
Expand All @@ -33,9 +34,9 @@
"required": [
"gh",
"type",
"nonpart",
"fee",
"snd",
"nonpart",
"fv",
"lv",
"note"
Expand Down
Loading

0 comments on commit 0f4cb16

Please sign in to comment.