Skip to content

Commit 55611b7

Browse files
authored
Merge pull request #97 from algorandfoundation/refactor/bytes
refactor: allow fixed sized bytes to be created by Bytes factory methods by passing same options parameter
2 parents 1d574c1 + 315d53c commit 55611b7

File tree

10 files changed

+226
-95
lines changed

10 files changed

+226
-95
lines changed

examples/voting/contract.algo.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('VotingRoundApp', () => {
1616

1717
const createContract = () => {
1818
const contract = ctx.contract.create(VotingRoundApp)
19-
const snapshotPublicKey = Bytes(keyPair.publicKey).toFixed({ length: 32 })
19+
const snapshotPublicKey = Bytes(keyPair.publicKey, { length: 32 })
2020
const metadataIpfsCid = ctx.any.string(16)
2121
const startTime = ctx.any.uint64(Date.now() - 10_000, Date.now())
2222
const endTime = ctx.any.uint64(Date.now() + 10_000, Date.now() + 100_000)
@@ -52,7 +52,7 @@ describe('VotingRoundApp', () => {
5252
const account = ctx.any.account()
5353
const signature = nacl.sign.detached(toExternalValue(account.bytes), keyPair.secretKey)
5454
ctx.txn.createScope([ctx.any.txn.applicationCall({ sender: account })]).execute(() => {
55-
const preconditions = contract.getPreconditions(Bytes(signature).toFixed({ length: 64 }))
55+
const preconditions = contract.getPreconditions(Bytes(signature, { length: 64 }))
5656

5757
expect(preconditions.is_allowed_to_vote).toEqual(1)
5858
expect(preconditions.is_voting_open).toEqual(1)
@@ -75,11 +75,7 @@ describe('VotingRoundApp', () => {
7575
)
7676

7777
ctx.txn.createScope([ctx.any.txn.applicationCall({ appId: app, sender: account })]).execute(() => {
78-
contract.vote(
79-
ctx.any.txn.payment({ receiver: app.address, amount: voteMinBalanceReq }),
80-
Bytes(signature).toFixed({ length: 64 }),
81-
answerIds,
82-
)
78+
contract.vote(ctx.any.txn.payment({ receiver: app.address, amount: voteMinBalanceReq }), Bytes(signature, { length: 64 }), answerIds)
8379

8480
expect(contract.votesByAccount(account).value.bytes).toEqual(answerIds.bytes)
8581
expect(contract.voterCount.value).toEqual(13)

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
"vitest": "3.2.4"
6969
},
7070
"dependencies": {
71-
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.85",
72-
"@algorandfoundation/puya-ts": "1.0.0-alpha.85",
71+
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.87",
72+
"@algorandfoundation/puya-ts": "1.0.0-alpha.87",
7373
"elliptic": "^6.6.1",
7474
"js-sha256": "^0.11.0",
7575
"js-sha3": "^0.9.3",

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export const DEFAULT_GLOBAL_GENESIS_HASH = Bytes(
4545
133, 89, 181, 20, 120, 253, 137, 193, 118, 67, 208, 93, 21, 168, 174, 107, 16, 171, 71, 187, 109, 138, 49, 136, 17, 86, 230, 189, 59,
4646
174, 149, 209,
4747
]),
48-
).toFixed({ length: 32 })
48+
{ length: 32 },
49+
)
4950

5051
/** @internal
5152
* algorand encoded address of 32 zero bytes

src/impl/crypto.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ import { Bytes, BytesCls, Uint64Cls } from './primitives'
1616
export const sha256 = (a: StubBytesCompat): bytes<32> => {
1717
const bytesA = BytesCls.fromCompat(a)
1818
const hashArray = js_sha256.sha256.create().update(bytesA.asUint8Array()).digest()
19-
const hashBytes = Bytes(new Uint8Array(hashArray)).toFixed({ length: 32 })
19+
const hashBytes = Bytes(new Uint8Array(hashArray), { length: 32 })
2020
return hashBytes
2121
}
2222

2323
/** @internal */
2424
export const sha3_256 = (a: StubBytesCompat): bytes<32> => {
2525
const bytesA = BytesCls.fromCompat(a)
2626
const hashArray = js_sha3.sha3_256.create().update(bytesA.asUint8Array()).digest()
27-
const hashBytes = Bytes(new Uint8Array(hashArray)).toFixed({ length: 32 })
27+
const hashBytes = Bytes(new Uint8Array(hashArray), { length: 32 })
2828
return hashBytes
2929
}
3030

3131
/** @internal */
3232
export const keccak256 = (a: StubBytesCompat): bytes<32> => {
3333
const bytesA = BytesCls.fromCompat(a)
3434
const hashArray = js_sha3.keccak256.create().update(bytesA.asUint8Array()).digest()
35-
const hashBytes = Bytes(new Uint8Array(hashArray)).toFixed({ length: 32 })
35+
const hashBytes = Bytes(new Uint8Array(hashArray), { length: 32 })
3636
return hashBytes
3737
}
3838

3939
/** @internal */
4040
export const sha512_256 = (a: StubBytesCompat): bytes<32> => {
4141
const bytesA = BytesCls.fromCompat(a)
4242
const hashArray = js_sha512.sha512_256.create().update(bytesA.asUint8Array()).digest()
43-
const hashBytes = Bytes(new Uint8Array(hashArray)).toFixed({ length: 32 })
43+
const hashBytes = Bytes(new Uint8Array(hashArray), { length: 32 })
4444
return hashBytes
4545
}
4646

@@ -114,7 +114,7 @@ export const ecdsaPkRecover = (
114114

115115
const x = pubKey.getX().toArray('be')
116116
const y = pubKey.getY().toArray('be')
117-
return [Bytes(x).toFixed({ length: 32 }), Bytes(y).toFixed({ length: 32 })]
117+
return [Bytes(x, { length: 32 }), Bytes(y, { length: 32 })]
118118
}
119119

120120
/** @internal */
@@ -127,7 +127,7 @@ export const ecdsaPkDecompress = (v: Ecdsa, a: StubBytesCompat): readonly [bytes
127127

128128
const x = pubKey.getX().toArray('be')
129129
const y = pubKey.getY().toArray('be')
130-
return [Bytes(x).toFixed({ length: 32 }), Bytes(y).toFixed({ length: 32 })]
130+
return [Bytes(x, { length: 32 }), Bytes(y, { length: 32 })]
131131
}
132132

133133
/** @internal */

0 commit comments

Comments
 (0)