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

Add getStateProof() / getProofHash() methods #155

Merged
merged 2 commits into from
Feb 23, 2024
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
2 changes: 2 additions & 0 deletions __tests__/__snapshots__/ain.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Object {
}
`;

exports[`ain-js Database getProofHash 1`] = `"0x88496dfee3566db91f487aa4cbf69a0c42a3e2a5d0a65bfd4897d699e8734785"`;

exports[`ain-js Database matchFunction 1`] = `
Object {
"matched_config": Object {
Expand Down
22 changes: 22 additions & 0 deletions __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,28 @@ describe('ain-js', function() {
})
});

it('getStateProof', async function() {
await ain.db.ref('/values/blockchain_params').getStateProof()
.then(res => {
expect(res).not.toBeNull();
})
.catch(error => {
console.log("error:", error);
fail('should not happen');
})
});

it('getProofHash', async function() {
await ain.db.ref('/values/blockchain_params').getProofHash()
.then(res => {
expect(res).toMatchSnapshot();
})
.catch(error => {
console.log("error:", error);
fail('should not happen');
})
});

/*it('on and off', function(done) {
try {
ain.db.ref().on('value', (snap:any) => console.log)
Expand Down
25 changes: 25 additions & 0 deletions src/ain-db/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EvalRuleInput,
EvalOwnerInput,
MatchInput,
StateInfoInput,
GetOptions,
} from '../types';
import Ain from '../ain';
Expand Down Expand Up @@ -347,6 +348,30 @@ export default class Reference {
return this._ain.provider.send('ain_matchOwner', request);
}

/**
* Fetches the state proof of a global blockchain state path.
* @param {StateInfoInput} params The state info input.
* @returns {Promise<any>} The return value of the blockchain API.
*/
getStateProof(params?: StateInfoInput): Promise<any> {
const request = {
ref: Reference.extendPath(this.path, params ? params.ref : undefined)
}
return this._ain.provider.send('ain_getStateProof', request);
}

/**
* Fetches the proof hash of a global blockchain state path.
* @param {StateInfoInput} params The state info input.
* @returns {Promise<any>} The return value of the blockchain API.
*/
getProofHash(params?: StateInfoInput): Promise<any> {
const request = {
ref: Reference.extendPath(this.path, params ? params.ref : undefined)
}
return this._ain.provider.send('ain_getProofHash', request);
}

// TODO(liayoo): Add this function.
///**
// * Attaches an listener for database events.
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ export interface MatchInput {
is_global?: boolean;
}

/**
* An interface for state information input.
*/
export interface StateInfoInput {
ref?: string;
}

/**
* An interface for state usage information.
*/
Expand Down
Loading