Skip to content

Commit 11e3df1

Browse files
committed
refactor: use new enum export without op namespace
1 parent ffff35e commit 11e3df1

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

src/impl/crypto.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { arc4, bytes, Bytes, gtxn, internal } from '@algorandfoundation/algorand-typescript'
1+
import { arc4, bytes, Bytes, Ecdsa, gtxn, internal, VrfVerify } from '@algorandfoundation/algorand-typescript'
22
import algosdk from 'algosdk'
33
import { ec } from 'elliptic'
44
import { sha256 as js_sha256 } from 'js-sha256'
@@ -68,7 +68,7 @@ export const ed25519verify = (
6868
}
6969

7070
export const ecdsaVerify = (
71-
v: internal.opTypes.Ecdsa,
71+
v: Ecdsa,
7272
a: internal.primitives.StubBytesCompat,
7373
b: internal.primitives.StubBytesCompat,
7474
c: internal.primitives.StubBytesCompat,
@@ -91,13 +91,13 @@ export const ecdsaVerify = (
9191
}
9292

9393
export const ecdsaPkRecover = (
94-
v: internal.opTypes.Ecdsa,
94+
v: Ecdsa,
9595
a: internal.primitives.StubBytesCompat,
9696
b: internal.primitives.StubUint64Compat,
9797
c: internal.primitives.StubBytesCompat,
9898
d: internal.primitives.StubBytesCompat,
9999
): readonly [bytes, bytes] => {
100-
if (v !== internal.opTypes.Ecdsa.Secp256k1) {
100+
if (v !== Ecdsa.Secp256k1) {
101101
internal.errors.internalError(`Unsupported ECDSA curve: ${v}`)
102102
}
103103
const dataBytes = internal.primitives.BytesCls.fromCompat(a)
@@ -117,7 +117,7 @@ export const ecdsaPkRecover = (
117117
return [Bytes(x), Bytes(y)]
118118
}
119119

120-
export const ecdsaPkDecompress = (v: internal.opTypes.Ecdsa, a: internal.primitives.StubBytesCompat): readonly [bytes, bytes] => {
120+
export const ecdsaPkDecompress = (v: Ecdsa, a: internal.primitives.StubBytesCompat): readonly [bytes, bytes] => {
121121
const bytesA = internal.primitives.BytesCls.fromCompat(a)
122122

123123
const ecdsa = new ec(curveMap[v])
@@ -130,7 +130,7 @@ export const ecdsaPkDecompress = (v: internal.opTypes.Ecdsa, a: internal.primiti
130130
}
131131

132132
export const vrfVerify = (
133-
_s: internal.opTypes.VrfVerify,
133+
_s: VrfVerify,
134134
_a: internal.primitives.StubBytesCompat,
135135
_b: internal.primitives.StubBytesCompat,
136136
_c: internal.primitives.StubBytesCompat,
@@ -145,6 +145,6 @@ export const EllipticCurve = new Proxy({} as internal.opTypes.EllipticCurveType,
145145
})
146146

147147
const curveMap = {
148-
[internal.opTypes.Ecdsa.Secp256k1]: 'secp256k1',
149-
[internal.opTypes.Ecdsa.Secp256r1]: 'p256',
148+
[Ecdsa.Secp256k1]: 'secp256k1',
149+
[Ecdsa.Secp256r1]: 'p256',
150150
}

src/impl/pure.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { biguint, Bytes, bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
1+
import { Base64, biguint, Bytes, bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { BITS_IN_BYTE, MAX_BYTES_SIZE, MAX_UINT64, MAX_UINT8, UINT64_SIZE } from '../constants'
33
import { notImplementedError, testInvariant } from '../errors'
44
import { asBigUint, asBytes, asMaybeBytesCls, asMaybeUint64Cls, asUint64Cls, binaryStringToBytes } from '../util'
@@ -10,8 +10,8 @@ export const addw = (a: internal.primitives.StubUint64Compat, b: internal.primit
1010
return toUint128(sum)
1111
}
1212

13-
export const base64Decode = (e: internal.opTypes.Base64, a: internal.primitives.StubBytesCompat): bytes => {
14-
const encoding = e === internal.opTypes.Base64.StdEncoding ? 'base64' : 'base64url'
13+
export const base64Decode = (e: Base64, a: internal.primitives.StubBytesCompat): bytes => {
14+
const encoding = e === Base64.StdEncoding ? 'base64' : 'base64url'
1515
const bytesValue = internal.primitives.BytesCls.fromCompat(a)
1616
const stringValue = bytesValue.toString()
1717

tests/crypto-op-codes.spec.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AlgoAmount } from '@algorandfoundation/algokit-utils/types/amount'
22
import { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
3-
import { Bytes, internal, op as publicOps, uint64 } from '@algorandfoundation/algorand-typescript'
3+
import { Bytes, Ec, Ecdsa, internal, uint64, VrfVerify } from '@algorandfoundation/algorand-typescript'
44
import algosdk from 'algosdk'
55
import { ec } from 'elliptic'
66
import { keccak256 as js_keccak256 } from 'js-sha3'
@@ -15,8 +15,8 @@ import { asUint8Array, getPaddedBytes } from './util'
1515

1616
const MAX_ARG_LEN = 2048
1717
const curveMap = {
18-
[internal.opTypes.Ecdsa.Secp256k1]: 'secp256k1',
19-
[internal.opTypes.Ecdsa.Secp256r1]: 'p256',
18+
[Ecdsa.Secp256k1]: 'secp256k1',
19+
[Ecdsa.Secp256r1]: 'p256',
2020
}
2121

2222
vi.mock('../src/impl/crypto', async (importOriginal) => {
@@ -157,7 +157,7 @@ describe('crypto op codes', async () => {
157157
asUint8Array(pubkeyX),
158158
asUint8Array(pubkeyY),
159159
)
160-
const result = op.ecdsaVerify(internal.opTypes.Ecdsa.Secp256k1, messageHash, sigR, sigS, pubkeyX, pubkeyY)
160+
const result = op.ecdsaVerify(Ecdsa.Secp256k1, messageHash, sigR, sigS, pubkeyX, pubkeyY)
161161

162162
expect(result).toEqual(avmResult)
163163
})
@@ -177,15 +177,15 @@ describe('crypto op codes', async () => {
177177
asUint8Array(pubkeyX),
178178
asUint8Array(pubkeyY),
179179
)
180-
const result = op.ecdsaVerify(internal.opTypes.Ecdsa.Secp256r1, messageHash, sigR, sigS, pubkeyX, pubkeyY)
180+
const result = op.ecdsaVerify(Ecdsa.Secp256r1, messageHash, sigR, sigS, pubkeyX, pubkeyY)
181181

182182
expect(result).toEqual(avmResult)
183183
})
184184
})
185185

186186
describe('ecdsaPkRecover', async () => {
187187
it('should be able to recover k1 public key', async () => {
188-
const testData = generateEcdsaTestData(internal.opTypes.Ecdsa.Secp256k1)
188+
const testData = generateEcdsaTestData(Ecdsa.Secp256k1)
189189
const a = testData.data
190190
const b = testData.recoveryId
191191
const c = testData.r
@@ -198,14 +198,14 @@ describe('crypto op codes', async () => {
198198
asUint8Array(c),
199199
asUint8Array(d),
200200
)
201-
const result = op.ecdsaPkRecover(internal.opTypes.Ecdsa.Secp256k1, a, b, c, d)
201+
const result = op.ecdsaPkRecover(Ecdsa.Secp256k1, a, b, c, d)
202202

203203
expect(result[0]).toEqual(avmResult[0])
204204
expect(result[1]).toEqual(avmResult[1])
205205
})
206206

207207
it('should throw unsupported error when trying to recover r1 public key', async () => {
208-
const testData = generateEcdsaTestData(internal.opTypes.Ecdsa.Secp256r1)
208+
const testData = generateEcdsaTestData(Ecdsa.Secp256r1)
209209
const a = testData.data
210210
const b = testData.recoveryId
211211
const c = testData.r
@@ -221,13 +221,13 @@ describe('crypto op codes', async () => {
221221
),
222222
).rejects.toThrow('unsupported curve')
223223

224-
expect(() => op.ecdsaPkRecover(internal.opTypes.Ecdsa.Secp256r1, a, b, c, d)).toThrow('Unsupported ECDSA curve')
224+
expect(() => op.ecdsaPkRecover(Ecdsa.Secp256r1, a, b, c, d)).toThrow('Unsupported ECDSA curve')
225225
})
226226
})
227227

228228
describe('ecdsaPkDecompress', async () => {
229229
it('should be able to decompress k1 public key', async () => {
230-
const v = internal.opTypes.Ecdsa.Secp256k1
230+
const v = Ecdsa.Secp256k1
231231
const testData = generateEcdsaTestData(v)
232232
const ecdsa = new ec(curveMap[v])
233233
const keyPair = ecdsa.keyFromPublic(testData.pubkeyX.concat(testData.pubkeyY).asUint8Array())
@@ -252,7 +252,7 @@ describe('crypto op codes', async () => {
252252
const c = internal.primitives.BytesCls.fromHex('3a2740da7a0788ebb12a52154acbcca1813c128ca0b249e93f8eb6563fee418d')
253253

254254
it('should throw not available error', async () => {
255-
expect(() => op.vrfVerify(internal.opTypes.VrfVerify.VrfAlgorand, a, b, c)).toThrow('vrfVerify is not available in test context')
255+
expect(() => op.vrfVerify(VrfVerify.VrfAlgorand, a, b, c)).toThrow('vrfVerify is not available in test context')
256256
})
257257

258258
it('should return mocked result', async () => {
@@ -265,7 +265,7 @@ describe('crypto op codes', async () => {
265265
)
266266
const mockedVrfVerify = (op as unknown as { mockedVrfVerify: Mock<typeof op.vrfVerify> }).mockedVrfVerify
267267
mockedVrfVerify.mockReturnValue([internal.primitives.BytesCls.fromCompat(new Uint8Array(avmResult[0])).asAlgoTs(), avmResult[1]])
268-
const result = mockedVrfVerify(publicOps.VrfVerify.VrfAlgorand, a, b, c)
268+
const result = mockedVrfVerify(VrfVerify.VrfAlgorand, a, b, c)
269269

270270
expect(asUint8Array(result[0])).toEqual(new Uint8Array(avmResult[0]))
271271
expect(result[1]).toEqual(avmResult[1])
@@ -274,29 +274,25 @@ describe('crypto op codes', async () => {
274274

275275
describe('EllipticCurve', async () => {
276276
it('should throw not available error', async () => {
277-
expect(() => op.EllipticCurve.add(internal.opTypes.Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
278-
'EllipticCurve.add is not available in test context',
279-
)
280-
expect(() => op.EllipticCurve.mapTo(internal.opTypes.Ec.BN254g2, Bytes(''))).toThrow(
281-
'EllipticCurve.mapTo is not available in test context',
282-
)
283-
expect(() => op.EllipticCurve.pairingCheck(internal.opTypes.Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
277+
expect(() => op.EllipticCurve.add(Ec.BN254g2, Bytes(''), Bytes(''))).toThrow('EllipticCurve.add is not available in test context')
278+
expect(() => op.EllipticCurve.mapTo(Ec.BN254g2, Bytes(''))).toThrow('EllipticCurve.mapTo is not available in test context')
279+
expect(() => op.EllipticCurve.pairingCheck(Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
284280
'EllipticCurve.pairingCheck is not available in test context',
285281
)
286-
expect(() => op.EllipticCurve.scalarMul(internal.opTypes.Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
282+
expect(() => op.EllipticCurve.scalarMul(Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
287283
'EllipticCurve.scalarMul is not available in test context',
288284
)
289-
expect(() => op.EllipticCurve.scalarMulMulti(internal.opTypes.Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
285+
expect(() => op.EllipticCurve.scalarMulMulti(Ec.BN254g2, Bytes(''), Bytes(''))).toThrow(
290286
'EllipticCurve.scalarMulMulti is not available in test context',
291287
)
292-
expect(() => op.EllipticCurve.subgroupCheck(internal.opTypes.Ec.BN254g2, Bytes(''))).toThrow(
288+
expect(() => op.EllipticCurve.subgroupCheck(Ec.BN254g2, Bytes(''))).toThrow(
293289
'EllipticCurve.subgroupCheck is not available in test context',
294290
)
295291
})
296292
})
297293
})
298294

299-
const generateEcdsaTestData = (v: internal.opTypes.Ecdsa) => {
295+
const generateEcdsaTestData = (v: Ecdsa) => {
300296
const ecdsa = new ec(curveMap[v])
301297
const keyPair = ecdsa.genKeyPair()
302298
const pk = keyPair.getPublic('array')

tests/pure-op-codes.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec'
2-
import { BigUint, bytes, Bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
2+
import { Base64, BigUint, bytes, Bytes, internal, Uint64, uint64 } from '@algorandfoundation/algorand-typescript'
33
import { afterEach, describe, expect, it, test } from 'vitest'
44
import { TestExecutionContext } from '../src'
55
import {
@@ -67,7 +67,7 @@ describe('Pure op codes', async () => {
6767
base64Encode(Bytes(Array(256).fill(0x00).concat([0xff]))),
6868
])('should decode standard base64 string', async (a) => {
6969
const avmResult = abiAsBytes(await getAvmResult({ appClient }, 'verify_base64_decode_standard', asUint8Array(a)))!
70-
const result = op.base64Decode(internal.opTypes.Base64.StdEncoding, a)
70+
const result = op.base64Decode(Base64.StdEncoding, a)
7171
expect(result).toEqual(avmResult)
7272
})
7373

@@ -77,7 +77,7 @@ describe('Pure op codes', async () => {
7777
await expect(getAvmResultRaw({ appClient }, 'verify_base64_decode_standard', asUint8Array(a))).rejects.toThrow(
7878
'illegal base64 data at input byte 0',
7979
)
80-
expect(() => op.base64Decode(internal.opTypes.Base64.StdEncoding, a)).toThrow('illegal base64 data')
80+
expect(() => op.base64Decode(Base64.StdEncoding, a)).toThrow('illegal base64 data')
8181
},
8282
)
8383

@@ -90,15 +90,15 @@ describe('Pure op codes', async () => {
9090
base64UrlEncode(Bytes(Array(256).fill(0x00).concat([0xff]))),
9191
])('should decode base64url string', async (a) => {
9292
const avmResult = abiAsBytes(await getAvmResult({ appClient }, 'verify_base64_decode_url', asUint8Array(a)))!
93-
const result = op.base64Decode(internal.opTypes.Base64.URLEncoding, a)
93+
const result = op.base64Decode(Base64.URLEncoding, a)
9494
expect(result).toEqual(avmResult)
9595
})
9696

9797
test.each([Bytes(Bytes(Array(256).fill(0x00).concat([0xff]))), asBigUintCls(BigUint(MAX_UINT512)).toBytes().asAlgoTs()])(
9898
'should throw error when input is not a valid base64url string',
9999
async (a) => {
100100
await expect(getAvmResultRaw({ appClient }, 'verify_base64_decode_url', asUint8Array(a))).rejects.toThrow('illegal base64 data')
101-
expect(() => op.base64Decode(internal.opTypes.Base64.URLEncoding, a)).toThrow('illegal base64 data')
101+
expect(() => op.base64Decode(Base64.URLEncoding, a)).toThrow('illegal base64 data')
102102
},
103103
)
104104
})

0 commit comments

Comments
 (0)