Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/split keyreg schemas #9

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading