|
| 1 | +import { strict as assert } from "node:assert"; |
| 2 | +import { |
| 3 | + Address, |
| 4 | + Keypair, |
| 5 | + Networks, |
| 6 | + buildAuthorizationEntryPreimage, |
| 7 | + buildWithDelegatesEntry, |
| 8 | + hash, |
| 9 | + xdr, |
| 10 | +} from "@stellar/stellar-sdk"; |
| 11 | +import { |
| 12 | + assertSignatureExpirationLedger, |
| 13 | + buildSignaturePayload, |
| 14 | + getAddressCredentials, |
| 15 | + usesAddressBoundPayload, |
| 16 | +} from "../src/kit"; |
| 17 | + |
| 18 | +function makeAccount(seedByte: number): string { |
| 19 | + return Keypair.fromRawEd25519Seed(Buffer.alloc(32, seedByte)).publicKey(); |
| 20 | +} |
| 21 | + |
| 22 | +function makeInvocation(): xdr.SorobanAuthorizedInvocation { |
| 23 | + return new xdr.SorobanAuthorizedInvocation({ |
| 24 | + function: xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeContractFn( |
| 25 | + new xdr.InvokeContractArgs({ |
| 26 | + contractAddress: xdr.ScAddress.scAddressTypeContract(hash(Buffer.from("contract"))), |
| 27 | + functionName: "do_it", |
| 28 | + args: [], |
| 29 | + }) |
| 30 | + ), |
| 31 | + subInvocations: [], |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +function makeAddressCredentials(address: string, expiration = 1): xdr.SorobanAddressCredentials { |
| 36 | + return new xdr.SorobanAddressCredentials({ |
| 37 | + address: Address.fromString(address).toScAddress(), |
| 38 | + nonce: xdr.Int64.fromString("7"), |
| 39 | + signatureExpirationLedger: expiration, |
| 40 | + signature: xdr.ScVal.scvVoid(), |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function makeAuthEntry(address: string): xdr.SorobanAuthorizationEntry { |
| 45 | + return new xdr.SorobanAuthorizationEntry({ |
| 46 | + credentials: xdr.SorobanCredentials.sorobanCredentialsAddress( |
| 47 | + makeAddressCredentials(address) |
| 48 | + ), |
| 49 | + rootInvocation: makeInvocation(), |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +const networkPassphrase = Networks.TESTNET; |
| 54 | + |
| 55 | +{ |
| 56 | + const account = makeAccount(1); |
| 57 | + const entry = makeAuthEntry(account); |
| 58 | + const credentials = getAddressCredentials(entry.credentials()); |
| 59 | + |
| 60 | + assert.equal(entry.credentials().switch().name, "sorobanCredentialsAddress"); |
| 61 | + assert.equal(Address.fromScAddress(credentials.address()).toString(), account); |
| 62 | + assert.equal(usesAddressBoundPayload(entry.credentials()), false); |
| 63 | +} |
| 64 | + |
| 65 | +{ |
| 66 | + const entry = makeAuthEntry(makeAccount(2)); |
| 67 | + const payload = buildSignaturePayload(networkPassphrase, entry, 123); |
| 68 | + const credentials = getAddressCredentials(entry.credentials()); |
| 69 | + const expectedEntry = xdr.SorobanAuthorizationEntry.fromXDR(entry.toXDR()); |
| 70 | + const expectedPreimage = buildAuthorizationEntryPreimage( |
| 71 | + expectedEntry, |
| 72 | + 123, |
| 73 | + networkPassphrase |
| 74 | + ); |
| 75 | + |
| 76 | + assert.deepEqual(payload, hash(expectedPreimage.toXDR())); |
| 77 | + assert.equal(credentials.signatureExpirationLedger(), 123); |
| 78 | +} |
| 79 | + |
| 80 | +{ |
| 81 | + const baseEntry = makeAuthEntry(makeAccount(3)); |
| 82 | + const credentials = getAddressCredentials(baseEntry.credentials()); |
| 83 | + const credentialFactory = xdr.SorobanCredentials as unknown as { |
| 84 | + sorobanCredentialsAddressV2: ( |
| 85 | + credentials: xdr.SorobanAddressCredentials |
| 86 | + ) => xdr.SorobanCredentials; |
| 87 | + }; |
| 88 | + const addressV2Entry = new xdr.SorobanAuthorizationEntry({ |
| 89 | + credentials: credentialFactory.sorobanCredentialsAddressV2(credentials), |
| 90 | + rootInvocation: baseEntry.rootInvocation(), |
| 91 | + }); |
| 92 | + const expectedEntry = xdr.SorobanAuthorizationEntry.fromXDR(addressV2Entry.toXDR()); |
| 93 | + const expectedPreimage = buildAuthorizationEntryPreimage( |
| 94 | + expectedEntry, |
| 95 | + 123, |
| 96 | + networkPassphrase |
| 97 | + ); |
| 98 | + |
| 99 | + assert.equal(addressV2Entry.credentials().switch().name, "sorobanCredentialsAddressV2"); |
| 100 | + assert.equal(usesAddressBoundPayload(addressV2Entry.credentials()), true); |
| 101 | + assert.deepEqual( |
| 102 | + buildSignaturePayload(networkPassphrase, addressV2Entry, 123), |
| 103 | + hash(expectedPreimage.toXDR()) |
| 104 | + ); |
| 105 | + assert.equal(credentials.signatureExpirationLedger(), 123); |
| 106 | +} |
| 107 | + |
| 108 | +{ |
| 109 | + const delegatedEntry = buildWithDelegatesEntry({ |
| 110 | + entry: makeAuthEntry(makeAccount(4)), |
| 111 | + validUntilLedgerSeq: 123, |
| 112 | + delegates: [{ address: makeAccount(5) }], |
| 113 | + }); |
| 114 | + const expectedEntry = xdr.SorobanAuthorizationEntry.fromXDR(delegatedEntry.toXDR()); |
| 115 | + const expectedPreimage = buildAuthorizationEntryPreimage( |
| 116 | + expectedEntry, |
| 117 | + 123, |
| 118 | + networkPassphrase |
| 119 | + ); |
| 120 | + |
| 121 | + assert.equal( |
| 122 | + delegatedEntry.credentials().switch().name, |
| 123 | + "sorobanCredentialsAddressWithDelegates" |
| 124 | + ); |
| 125 | + assert.equal(usesAddressBoundPayload(delegatedEntry.credentials()), true); |
| 126 | + assert.deepEqual( |
| 127 | + buildSignaturePayload(networkPassphrase, delegatedEntry, 123), |
| 128 | + hash(expectedPreimage.toXDR()) |
| 129 | + ); |
| 130 | + assert.equal(getAddressCredentials(delegatedEntry.credentials()).signatureExpirationLedger(), 123); |
| 131 | +} |
| 132 | + |
| 133 | +{ |
| 134 | + const fakeAddressV2Credentials = { |
| 135 | + switch: () => ({ name: "sorobanCredentialsAddressV2" }), |
| 136 | + } as unknown as xdr.SorobanCredentials; |
| 137 | + |
| 138 | + assert.throws( |
| 139 | + () => getAddressCredentials(fakeAddressV2Credentials), |
| 140 | + /ADDRESS_V2 credentials require an SDK with Protocol 27 credential support/ |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +{ |
| 145 | + const fakeDelegatedCredentials = { |
| 146 | + switch: () => ({ name: "sorobanCredentialsAddressWithDelegates" }), |
| 147 | + } as unknown as xdr.SorobanCredentials; |
| 148 | + |
| 149 | + assert.equal(usesAddressBoundPayload(fakeDelegatedCredentials), true); |
| 150 | + assert.throws( |
| 151 | + () => getAddressCredentials(fakeDelegatedCredentials), |
| 152 | + /ADDRESS_WITH_DELEGATES credentials require an SDK with Protocol 27 credential support/ |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +{ |
| 157 | + assert.doesNotThrow(() => assertSignatureExpirationLedger(0)); |
| 158 | + assert.doesNotThrow(() => assertSignatureExpirationLedger(0xffffffff)); |
| 159 | + assert.throws(() => assertSignatureExpirationLedger(-1), /uint32 integer/); |
| 160 | + assert.throws(() => assertSignatureExpirationLedger(0x100000000), /uint32 integer/); |
| 161 | + assert.throws(() => assertSignatureExpirationLedger(1.5), /uint32 integer/); |
| 162 | + assert.throws(() => assertSignatureExpirationLedger(Number.NaN), /uint32 integer/); |
| 163 | +} |
| 164 | + |
| 165 | +{ |
| 166 | + const entry = makeAuthEntry(makeAccount(4)); |
| 167 | + const credentials = getAddressCredentials(entry.credentials()); |
| 168 | + |
| 169 | + assert.throws( |
| 170 | + () => buildSignaturePayload(networkPassphrase, entry, 1.5), |
| 171 | + /Soroban signature expiration ledger must be a uint32 integer/ |
| 172 | + ); |
| 173 | + assert.equal(credentials.signatureExpirationLedger(), 1); |
| 174 | +} |
| 175 | + |
| 176 | +console.log("Protocol 27 auth helper tests passed"); |
0 commit comments